Docker CLI Cheat Sheet

March 2017 · 2 minute read

Docker Banner

Terminology

Throughout this post, I’ll be using <container> to mean container name or container ID, and <image> to mean image name or image ID.

Containers

docker ps
docker exec -it <container> bash

This lets you run shell commands inside <container>

docker stop <container>
dockre restart <container>

There’s also a docker start command, which starts a stopped container, but you can use docker restart instead most of the time. The only difference between the two commands is what happens when a container is already running; docker start doesn’t do anything to it, and docker restart stops it and starts it again.

docker rm <container>

To copy from your local filesystem to a container:

docker cp /path/to/local/file <container>:/path/to/container/dir/

To copy from a container to your local filesystem:

docker cp <container>:/path/to/container/file /path/to/local/dir
docker attach <container>

Using CTRL-c will kill the container—to detach and leave the container running, press CTRL-p CTRL-q. If you’d rather use CTRL-c to detach without killing the container, run the above command with the flag --sig-proxy=false before <container>.

docker logs <container>

Add --tail 5 before <container to show 5 the last 5 lines from the log. Replace 5 with any number.

docker rename <container> <new_name>

Images

docker images
docker rmi <image>
docker build -t <user/image:tag> .

Note the . at the end of the command above. If your Dockerfile has a custom name, replace the . with -f <Dockerfile_name>