Useful Docker Commands

Aug 2, 2020

Here I collect Docker commands that I found very useful but hard to remember. I don’t go into details here. It’s just a collection for lookup purposes. But there are also more concrete examples on this site.

Managing Containers

List all containers, including stopped ones:

docker ps -a

Attach Shell

Start and attach bash in a new container with mounting and port forwarding:

docker run -v $PWD/data:/home/data -p 8080:8080 -it ubuntu:latest /bin/bash

Start and attach to an existing container:

docker start -a -i $DOCKER_ID

Attach bash on a running container:

docker exec -it $DOCKER_ID /bin/bash

Alpine containers have ash instead of bash installed:

docker exec -it $DOCKER_ID /bin/ash

Images

Save image to a file:

docker save -o image_file.tar image/name

Load image from a file:

docker load -i image_file.tar

Files

Copy file from docker container to host system (and vice versa):

docker cp $DOCKER_ID:/etc/nginx/conf.d/default.conf /path/on/host/default.conf

Logs

Show logs of a docker container:

docker logs $DOCKER_ID

# follow log live on stdout:
docker logs --follow $DOCKER_ID

# show only last 10 entries:
docker logs --tail 10 $DOCKER_ID 

There are more interesting flags like --since, --until, --timestamps,… See docker logs --help for details.

You might also like