Docker cheat sheet

Aditya pratap singh
Congruence Labs
Published in
3 min readFeb 20, 2019
Pic credit: Medium.com

While working with Docker is amazing, it’s a tedious task to remember all the handy commands you need since you don’t use them very frequently as a Developer, unless you also do Devops on a regular basis. Depending on the complexity of the use case, I found and compiled the below list of common commands to be very useful in such situations:

  1. Run docker containers
  2. Check docker resources
  3. Update docker resources
  4. Cleanup docker resources
  5. Mount and Copy files or volumes

Apart from the above 4 categories, a useful trick to know is the API for Docker@17.0 and later which makes the commands very declarative, similar to Kubernetes API

Run docker containers

// Run docker image in interactive mode
docker run -it <image name>:<image tag>

// Run docker image in interactive mode with host port 8080 mapped to container port 80
docker run -it -p 8080:80 <image name>:<image tag>

// Run docker with the 3001 port exposed,
docker run -it --expose=3001 <image name>:<image tag>

// Run docker with an environment variable set
docker run -it \
-e RUST_BACKTRACE=1 \
<image name>:<image tag>

// Run a docker command in container and remove container after finish
docker run --rm -it --expose=3001 <image name>:<image tag>
// Run docker container as a daemon process
docker run -d <image name>:<image tag>
// Run shell with docker alpine image
docker run -it --rm alpine /bin/ash
docker run -it --rm alpine /bin/sh
docker run -it --rm alpine ash
docker run -it --rm alpine sh
// Start and stop a container by name
docker start <container name>
docker stop <container name>
// Run commands inside a running docker container. e.g. below shows `ls` in a running container
docker exec -it 4a140740c577 ls

Check docker resources

// Check docker images
docker images
docker image ls
// Check docker containers
docker ps -a
docker container ls
// Check the process running inside the container
docker top <container name or id>

// Check resource utilization
docker stats

// Check docker logs with multiple time options
docker logs -f --since <1s|5m|1d|UTC date time> <container name>
// Inspect container configurations
docker inspect
// docker system information
docker system df (Show disk info)
docker system events (Show system events)
docker system info (Show system info)
docker system prune (Prune unused data)
// Snoop network settings of a running container
docker inspect $container_id | $container_name
// Check docker image content, if entrypoint is provided
docker run -it --entrypoint sh <image name>

Update docker resources

// Update container configurations
docker update --restart=always <container name>
docker update --kernel-memory 80M <container name>

Cleanup docker resources

// Cleanup stopped containers
docker rm $(docker ps -q -f 'status=exited')
// Cleanup dangling images
docker rmi $(docker images -q -f "dangling=true")
// Cleanup dangling volumes
docker volume rm $(docker volume ls -qf dangling=true)
// Cleanup images with a certain name (https://stackoverflow.com/questions/40084044/how-to-remove-docker-images-based-on-name)
docker images | grep <pattern> | xargs docker rmi
// Use `prune` subcommand to cleanup respective docker resources// Cleanup all unused docker artifacts
docker system prune
// or same as above would be all 4 below
docker container prune
docker image prune
docker network prune
docker volume prune

Mount and Copy files or volumes

// Mount volume myvol2 into docker container at /app
docker run -it -v myvol2:/app <image name>:<image version>
// Copy file `foo.txt` to docker container `mycontainer`
docker cp foo.txt mycontainer:/foo.txt
// Copy file `foo.txt` from docker container `mycontainer` to host machine
docker cp mycontainer:/foo.txt foo.txt

Docker 17.0 and later API

With the new docker re-organization of modules, all commands are re-structured similar to Kubernetes. E.g.

Below 2 are same:

docker run -it -e RUST_BACKTRACE=1 <image name>:<image tag>
docker container run -it -e RUST_BACKTRACE=1 <image name>:<image tag>

And so are the below commands to list containers,

// List docker containers
docker ps -a
docker container ls

The above list is not exhaustive but was useful for me as a developer to not be aware of docker as much as a Devops person and still be able to navigate through in using docker related issues. Hopefully it helps few of you!

--

--

Aditya pratap singh
Congruence Labs

Engineering manager, GraphQL and edge routing platforms @ZalandoTech interested in web, JS, GraphQL, Go, TS, Rust and reliable, high scale distributed systems