Just some Docker commands tips & tricks I documented for myself.
Misc
Start interactive shell in Alpine image
Alpine uses the ash shell instead of the bash shell. This by the way also overrules the default CMD
in the image.
$ docker run -it --entrypoint=/bin/ash image-id
Connect to a running container
What if you started a shell in the background and you want to see the stdout
and stderr
output? Connect to the running container.
$ docker attach container-id
Connect to a running container with an interactive shell
So what if you want to connect to a running container and inspect its contents? Just attach and start an interactive shell 🙂
$ docker exec -it container-id /bin/ash
Inspect image, volume or running container
It can be handy to inspect the settings of images, volumes or running containers. To do this use the following commands.
docker image inspect image-id docker volume inspect volume-id docker container inspect container-id
Build
Traditionally, the Dockerfile is called Dockerfile
and located in the root of the context directory. You use the -f
flag with docker build to point to a Dockerfile anywhere in your file system.
IMPORTANTÂ when pointing to a Dockerfile not located in the context directory, you must add a period (.) at the end of the statement.
$ docker build -t my-label -f /path/to/a/Dockerfile .
Volumes
Volumes make it possible to persist data between container restarts and share data between containers.
Create a volume.
$ docker volume create logs
List files in a volume.
$ docker run -it --rm -v logs:/logs alpine ls -l /logs
Display the contents of a file in a volume.
$ docker run -it --rm -v logs:/logs alpine cat /logs/access.log
Interactive access to the files in a volume.
$ docker container run -ti -v logs:/logs alpine sh -c 'cd /logs; exec "${SHELL:-sh}'
Using tail with -f option to view changes to the contents of a file in realtime.
$ docker run -it --rm -v logs:/logs alpine tail -f -n 25 /logs/access.log