docker,

Useful Docker and docker-compose commands

Mar 21, 2021 · 3 mins read · Post a comment

Working with Docker Compose in a development environment can be challenging sometimes for the newcomers, unless you got the time on your hands to google everything from stack overflow and save it somewhere locally. In this post, I’ll share with you some commands that might help you from the start.

Services and containers are used interchangeably.

Prerequisites

  • Docker
  • Docker Compose

Docker Compose

Build and start Docker Compose stack in the background

docker-compose up --build -d

This command will build or rebuild services (if you have changes in a service Dockerfile or in it’s build directory)in the docker-compose stack and run them in the background. This is usually useful when trying to deploy the stack for the first time. -d stands for detached mode.

Stop and remove all docker containers from the Docker Compose stack

docker-compose down

This command will stop and remove all services (containers) specified in the Docker Compose stack. If you want to remove the volumes and images used by any service add -v and --rmi all parameters as well.

Restart a single Docker Compose service

docker-compose restart <service_name>

If you omit a service name, it will restart all the services.

Start Docker Compose stack with a scaled up service

docker-compose up -d --scale <service_name>=5

Update containers that have newer image than currently running

docker-compose up -d --no-deps <service_name>

Get Docker Compose to pull the latest images from repositories

1. docker-compose down
2. docker-compose pull (for remote repositories) and/or docker-compose build --pull (for local build images)
3. docker-compose up -d

Docker

Run alpine docker container in a detached mode

docker run -dit --name test-container alpine

Get into the docker container shell

docker exec -it <container_name_or_id> /bin/bash

Note(s): If /bin/bash doesn’t work, try with /bin/sh.

Parameters:

  • -it or -i -t: Allocates a tty for the container process.
  • -i: Keeps stdin open even if it’s not attached.
  • -t: Allocate a pseudo-tty.

Get info about the docker image or container

docker inspect <image_name_or_container_name>

Show docker container logs

docker logs <container_name_or_id>

If you want to stream the logs:

docker logs --follow <container_name_or_id>

If you want to display only the latest lines:

docker logs --tail 100 <container_name_or_id>

Get docker container IP address


docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_id>

Clean out docker images, builds, volumes, containers, …

docker system prune --volumes

This command will remove all stopped containers, build cache, dangling images, networks that are not used by at least one container, and not used volumes.

Remove all custom networks only not used by at least one container:

docker network prune

Remove all volumes only not used by at least one container:

docker volume prune

Remove all stopped containers:

docker container prune

Remove all containers including its volumes use (YOLO):

docker rm -vf $(docker ps -aq)

Remove all images (YOLO):

docker rmi -f $(docker images -aq)

Conclusion

I’m sure there’s a lot more commands to be added, so feel free to leave a comment below. On a side note, follow our official channel on Telegram.