DevOps Buzz
Search…
Bash / Shell
Bitbucket
Distros
Elasticsearch
General
Guidelines / Standards
microk8s
Prometheus
RabbitMQ
VirtualBox
Cheat Sheet
Docker useful commands.

Container management

Avoid container termination after executing CMD or Entrypoint

If you have a Dockerfile similar to this one:
1
FROM nginx
2
COPY my-custom-script.sh /
3
ENTRYPOINT ["/my-custom-script.sh"]
4
# OR
5
# CMD /my-custom-script.sh
Copied!
And after my-custom-script.sh exits, docker stops the container, you can fix the problem as follows.
Fist, make sure you are using COPY instead of ADD in your Dockerfile to copy your script.
Edit my-custom-script.sh and add the following line at the end:
1
...
2
# Run all command line arguments
Copied!
Then make sure to use a command when running your container, for example /bin/bash
1
docker run \
2
-tid \
3
-p 8080:8080 \
4
--name test \
5
my-custom-image \
6
/bin/bash
Copied!

Bash TTY connect

1
docker exec -ti CONTAINER-NAME-HERE /bin/bash
Copied!

Run container

1
docker run \
2
-tid \
3
--name test \
4
-p 8080:8080 \
5
-e MY_VAR='MY-VALUE' \
6
nginx
Copied!

Start container on boot

Run your container with --restart=always flag.
Or add the the flag to an existing container:
1
docker update --restart=always <container>
Copied!

CLI tricks

Attach

1
docker attach CONTAINER-NAME
Copied!
If you press CTRL+C to exit it will stop the container. To exit without stopping the container press CTRL+P then CTRL+Q

Image management

Create image from container

1
docker commit CONTAINER-NAME NEW-IMAGE-NAME
Copied!

Create image from Dockerfile

1
docker build -t NEW-IMAGE-NAME .
Copied!

Create image from AWS AMI

Get your AMI snapshot ID

Access your AWS console and go to EC2, AMIs. Find your AMI and check its Block Devices, for example:
1
/dev/sda1=snap-081e01478359d5bec:30:true:gp2
Copied!
snap-081e01478359d5bec is the AMI snapshot ID, which will be used as the source for a new device.

Create a new volume from your AMI snapshot ID

In your AWS console go to Snapshots. Filter using your snapshot ID, for example: snap- 081e01478359d5bec.
Right click on it, then select Create Volume. Add a tag key Name and input your new volume’s name.

Attach your new volume to an instance

Launch a new instance or use an existing one.
Keep in mind the instance must have enough space to store a temporary tar le from your AMI volume.
In your AWS console go to Volumes. Filter by your Volume Name (you created previously).
Right click on it, then select Attach Volume.
Select your instance and choose a Device, for example: /dev/sdf
Click on Attach.

Mount your new volume

SSH connect to your instance and mount the volume you just created.
1
mount /dev/xvdf1 /mnt
Copied!
Note your Device was renamed to /dev/xvdf

Install Docker

Install Docker in your instance.

Create Docker image

SSH connect to your instance and mount the volume you just created.
1
tar -c -C /mnt/ . | docker import - YOUR-IMAGE-NAME
Copied!
Replace YOUR-IMAGE-NAME
This command might take a while depending on the size of your volume.

Testing

List your docker images. Find YOUR-IMAGE-NAME and get its Image ID.
Run a container from your new image.
1
docker run -tid dd5935d36306 /bin/bash
Copied!

Cleanup

If everything is OK you can umount your volume:
1
umount /mnt
Copied!
Then go to your AWS Console, Volumes. Find the volume you created previously, right click on it then select Detach Volume. Then right click on it again then select Delete Volume.

References

Cleanup dangling images

1
docker rmi -f $(docker images -f "dangling=true" -q)
Copied!
Or.. the docker system prune command will remove all stopped containers, all dangling images, and all unused networks:
1
docker system prune
Copied!

Inspect

List run command used

1
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
2
assaflavie/runlike YOUR-CONTAINER
Copied!

Show mounted volumes

1
docker inspect -f '{{ .Mounts }}' containerid
Copied!