docker and docker-compose - list of most useful commands (common cmd)

4 points
Created by:

Hi, today I'd like to share with you list of most useful docker and docker-compose commands.

I find it very useful to have this kind of cheat sheet / shortcuts.

docker list most useful commands

# list all images
$ docker images

# list all images (with dangling images)
$ docker images -a

# remove all images
$ docker system prune -a

# remove 1 docker image
$ docker rmi put_image_id_here
# eg:
$ docker rmi e1d7dc9731da

# kill container
$ docker kill put_container_id_here
# eg:
$ docker kill 4f2135f4f315

# list all containers
$ docker ps

# build current project where we have Dockerfile
$ docker build .

Commnad combinations

# stop all
docker stop $(docker ps -aq)

# remove all containers
docker rm $(docker ps -aq)

# remove all images
docker rmi $(docker images -a -q)

# docker ps [OPTIONS] - list all containers
# --all , -a   Show all containers (default shows just running)
# --quiet , -q   Only display container IDs

Remove more then 1 docker image in single line:

# list all docker images
$ docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
mysql                    latest              e1d7dc9731da        2 weeks ago         544MB
docker/getting-started   latest              1f32459ef038        2 months ago        26.8MB
openjdk                  8-jdk-alpine        a3562aa0b991        16 months ago       105MB
# remove 2 images in single line
$ docker rmi e1d7dc9731da && docker rmi 1f32459ef038

docker-compose list most useful commands

# start based on docker-compose.yml in current directory
$ docker-compose up

$ docker-compose stop
$ docker-compose down

$ docker-compose ps

# print docker-compose with .env imported
$ docker-compose config

# show last 10 lines of logs from each docker container
$ docker-compose logs -t -f --tail 10

Other useful commands

# restart docker service
service docker restart

Build java project with mvn + skip unit tests

mvn clean install -Dmaven.test.skip=true


References docker docs

Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé