Nedtes

My ideas, thoughts and questions, but mostly just software

Docker Cheat Sheet

Running a container in interactive mode

1
docker run IMAGE_NAME --name CONTAINER_NAME -it /bin/bash

Running health check on container

Check the localhost 80 port every 20s, consider it a failure after 5 times.

1
docker run  IMAGE_NAME --name CONTAINER_NAME --health-cmd="curl http://127.0.0.1 || exit 1" --health-interval=20s --health-retries=5

Freeze/Unfreeze a container

1
2
docker pause CONTAINER_NAME
docker unpause CONTAINER_NAME

Limit container CPU

When limiting CPU usage. 1024 means a full CPU.

1
docker run -it --c 512 IMAGE_NAME

Limit it to use only 2 CPUs and use only half of each one.

1
docker run -it --c 512 --cpus 2

Limit container memory

Limit the container memory to 1GB

1
docker run IMAGE_NAME -m 1073741824

Use another image as cache

Sometimes you are on a new machine and you don’t want your image to build from scratch. You can pull a previous image from your registry and use its cache. This will accelerate your build time and allow you to hit production as soon as possible.

1
2
docker pull IMAGE_TAG:latest
docker build -t IMAGE_TAG:COMMIT_HASH --cache-from IMAGE_TAG:latest .

Deleting all networks

1
docker network prune -f

Killing all exited containers

1
docker rm $(docker ps -f status=exited -q)