Useful docker and docker-compose commands

Mar 21, 2021 · 3 mins read · Post a comment
Useful docker and docker-compose commands

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. The words like services and containers will be used interchangeably.


  • 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 in 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 ommit 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

Official docker-compose commandline documentation.


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: if /bin/bash doesn’t work, try with /bin/sh.


  • -it or -i -t: allocating a tty for the container process, or
  • -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 '' <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)

Official docker commandline documentation.


I’m sure there’s a lot more commands to be added, so feel free to leave a comment below.