github gitlab twitter
Docker commands I frequently use
Jun 10, 2022
2 minutes read

I’m currently helping onboard a new colleague on my team, who recently joined Mozilla as a Senior Test Engineer. She will be working with us on test engineering for Firefox Services and I couldn’t be more excited to have her join the team. 🦊

As part of her onboarding, we’re looking into Docker and docker compose. I thought I’d publish this short article as a reference for my team and readers of my blog.

Docker use cases

We use Docker quite extensively for automated tests. For instance we use it for running distributed load tests on Google Kubernetes Engine or starting services, performing test setups, and running contract tests both locally and on CI.

Contract tests architecture

Docker commands

I often find myself using the following commands and CLI options or a combination of these commands when working with Docker. Listed in no particular order. 📋

Do you know other useful Docker and docker compose commands? I would love to hear from you! You can message me on twitter or send me an email.

Containers

Show all containers:

docker ps -a

Remove all containers:

docker rm $(docker ps -a -q)

Images

List images (hide intermediate images):

docker images

Remove all images:

docker rmi $(docker images -q)

Build image with tag:

docker build -t app:build .

Volumes

Remove all unused local volumes:

docker volume prune

Compose

Stop and remove containers as well as networks specified in a compose configuration file that is located at a sub-directory (for example contract-tests):

docker compose \
 -f contract-tests/docker-compose.yml \
 down

Create and start containers, networks and mount volumes. Stop all containers if any container exits (in our projects, that’s typically the container running the tests):

docker compose \
  -f contract-tests/docker-compose.yml \
  up --abort-on-container-exit

Specifiy multiple compose configuration files. Use docker-compose.yml as the base configuration and overwrite specific values from docker-compose.204.yml:

docker compose \
 -f contract-tests/docker-compose.yml \
 -f contract-tests/docker-compose.204.yml \
 up --abort-on-container-exit

Return the exit code of the selected service container (for example hello_world):

docker compose \
  -f contract-tests/docker-compose.yml \
  -f contract-tests/docker-compose.init_error.yml \
  up --abort-on-container-exit --exit-code hello_world

The above is particularly useful when running in a shell script on CI to verify that a container exits with the expected exit code:

hello_world_exit_code=$?
if [ "${hello_world_exit_code}" -eq 0 ]; then
 echo "Expected non-zero exit_code from Hello-World service"
else
 echo "Hello-World service exit_code: ${hello_world_exit_code}"
fi

Create and start containers, networks and mount volumes. Scale one of the services to multiple instances (for example locust_worker):

docker compose \
 -f test-engineering/load-tests/docker-compose.yml \
 up --scale locust_worker=4

Back to posts