Languages
[Edit]
EN

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

4 points
Created by:
Diana
720

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 .

 

Docker logs

# follow short
docker logs -f <container_id>
# follow full
docker logs --follow <container_id>

# since
docker logs --since=2h <container_id>

# to get <container_id> we use:
docker ps

 

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

 

Export docker images

# export docker image to tar file
docker save -o my-image-name.tar my-image-name
# NOTE:
# my-image-name can be get from running docker images (REPOSITORY column)


# docker export image to tar with image version
docker save -o my-image-name.tar my_prefix/my-image-name:1.0.1


# docker export image to tar without image version
# it will export all versions available (list all images - docker images)
docker save -o my-image-name.tar my_prefix/my-image-name


# docker load docker tar image under windows with git bash:
docker load -i my-docker-image.tar
# note - if docker image has more then 1 version of image exported, all of them will be imported

 

Docker - remove images older than some specific period of time

# delete images older than: 1 day (24h)
docker image prune --all --filter "until=24h"

# delete images older than: 168h == 7 days == 1 week
docker image prune --all --filter "until=168h"

# delete images older than: 336h == 14 days == 2 weeks
docker image prune --all --filter "until=336h"

# delete images older than: 504h == 21 days == 3 weeks
docker image prune --all --filter "until=504h"

# delete images older than: 720h == 30 days == 4 weeks ~= 1 month
docker image prune --all --filter "until=720h"

 

Docker - remove images by tag name

# will print all images
docker images | grep "MY_TAG_HERE" | awk '{print $1 ":" $2}'

# will remove all images - test above command 
docker images | grep "MY_TAG_HERE" | awk '{print $1 ":" $2}' | xargs docker rmi

 

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

References docker docs

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Docker - useful wiki posts

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.

❤️💻 🙂

Join