Docker Cheat Sheet

Listing images, containers and volumes

List all of your images:

docker image ls

or to list images used by the created containers:

docker-compose images

List all of your running containers:

docker ps

List all of your containers (including stopped containers):

docker ps -a

or:

docker-compose ps

List services launched from your docker-compose.yml:

docker-compose config --services

List all of your volumes:

docker volume ls

Inspect a volume, including its Mountpoint:

docker volume inspect <volume_name>

List named volumes referenced in your docker-compose.yml:

docker-compose config --volumes

Building a Docker image from a Dockerfile

To build an image from a Dockerfile:

docker build

or if you have a docker-compose.yml:

docker-compose build

Running an interactive shell inside a container

You can either launch a new container to run an interactive shell allowing you to browse the contents of an image, or you can launch a shell inside an already running container.

To launch a new container which runs an interactive bash shell from an existing image, the method depends on whether the image has an entrypoint, and whether you want to override it.

If the image (e.g. monashmerc/mytardis_django) has an entrypoint, you can run /bin/bash instead of the image’s default entrypoint script and then look at the entrypoint script from the container you just created:

$ docker run -it --entrypoint /bin/bash monashmerc/mytardis_django
root@47d3e3917ed2:/usr/src/app# cat /docker-entrypoint.sh

If the image (e.g. nginx) doesn’t have an entrypoint, you can create a container to run bash as follows:

docker run -it IMAGE /bin/bash
# e.g. docker run -it nginx /bin/bash

To launch a bash shell inside an already running container:

docker exec -it CONTAINER /bin/bash
# e.g. docker exec -it mytardis_celery_1 /bin/bash

To launch a bash shell inside a service (listed by docker-compose config –services):

docker-compose exec SERVICE /bin/bash
# e.g. docker-compose exec celery /bin/bash

Pulling in images required in your docker-compose.yml

To pull images (from DockerHub) required in your docker-compose.yml:

docker-compose pull

Starting up services defined in docker-compose.yml

To start up the services / containers defined in docker-compose.yml:

docker-compose up -d

where ‘-d’ is “Detached mode” for running containers in the background.

To check the logs of services / containers launched with docker-compose up -d:

docker-compose logs -f

where ‘-f’ means “Follow log output” and will continue displaying updates to logs until you press Control-C.

To stop services / containers launched with docker-compose up -d:

docker-compose down

Deleting all images, containers and volumes

WARNING: The commands below are destructive and are intended to be used when you want to clean up a test Docker environment and you don’t have any valuable Docker images or volumes you want to keep.

Remove all stopped containers, and receive an error for each container which can’t be removed because it is still running:

docker rm $(docker ps -aq)

Remove all containers, even those which are still running:

docker rm -f $(docker ps -aq)

Remove all volumes not being used by a container:

docker volume prune

Remove an image:

docker rmi <image_id>

Remove all images:

docker rmi $(docker image ls -q)

If attempting to remove images gives an error like image is referenced in multiple repositories (because you have created multiple tags from an image), you can add -f to remove them forcefully:

docker rmi -f $(docker image ls -q)