DEV Community

Cover image for Useful Docker Commands Worth Saving
Alex Hyett
Alex Hyett

Posted on • Originally published at alexhyett.com on

Useful Docker Commands Worth Saving

I use docker every day. All the applications I write at work or at home end up in docker containers. Most of the time though, I am only running docker-compose up so when I need to do something more complicated I have to look it up.

So this post is a resource for me but I am hoping these commands will be useful for you too.

Cleaning up after Docker

If you have been using docker for a while you are going to end up with quite a few docker images hanging around. These commands should help clear them up.

Delete old containers

Most of the cleanup commands that are shared will clean out all of your containers. This command cleans up just those you haven’t used in a while:

docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs docker rm
Enter fullscreen mode Exit fullscreen mode

Delete untagged images

If an image isn’t tagged then you likely aren’t using it so we can get rid of these as well.

docker images | grep "<none>" | awk '{print $3}' | xargs docker rmi
Enter fullscreen mode Exit fullscreen mode

Clean slate

If you really want to get rid of everything not currently running, including volumes and images you can use this one:

docker system prune -a --volumes
Enter fullscreen mode Exit fullscreen mode

Building docker images

Most of you will already be familiar with docker build here are some of the less used docker commands.

Rebuild all docker-compose images with no cache

By default doing a docker-compose build will use cached images. If you want to make sure they are built from scratch and pull down images you need:

docker-compose build --no-cache
Enter fullscreen mode Exit fullscreen mode

Rebuild just one service in your docker-compose

This one is especially useful on my Raspberry Pi where builds are a bit slower. If you know only one service has changed you can rebuild just that one.

docker-compose build --no-cache elasticsearch
Enter fullscreen mode Exit fullscreen mode

Connecting to a docker container

If you need to debug why a container isn’t working you often need to connect to the running container.

Connect to a running container

To use this command you need to run docker ps to get a list of running containers, then use the container ID in the command below.

docker exec -it 0c7b2063b2a2 /bin/bash
Enter fullscreen mode Exit fullscreen mode

If your container doesn’t have bash you might be able to use sh.

docker exec -it 0c7b2063b2a2 /bin/sh
Enter fullscreen mode Exit fullscreen mode

Connecting to a stopped container

If you want to connect to a stopped container it is a little more complicated.

We first need to use docker ps -a to see the stopped container. Then you need to copy the container ID of the stopped container and commit it to a new image.

docker commit 0488e172aa70 test/image
Enter fullscreen mode Exit fullscreen mode

You can then run it and connect to it using:

docker run -it --rm test/image /bin/bash
Enter fullscreen mode Exit fullscreen mode

If the docker container has a faulty script at startup you may need to use this:

docker run -it --rm --entrypoint /bin/bash test/image
Enter fullscreen mode Exit fullscreen mode

Copy files between container and host

Occasionally you might want to copy files to and from a running container. Using the Container ID again you can use the following commands.

Copy files FROM container

If you just want one file, for example the hosts file you can use:

docker cp 0c7b2063b2a2:/etc/hosts hosts
Enter fullscreen mode Exit fullscreen mode

Or if you want a whole folder you can do this (where etc is a folder in the working directory on the host):

docker cp 0c7b2063b2a2:/etc/. etc
Enter fullscreen mode Exit fullscreen mode

Copy files TO container

Similarly you can copy to the container to just by switching the parameters around:

docker cp fstab 0c7b2063b2a2:/etc/fstab
Enter fullscreen mode Exit fullscreen mode

Final words

If I come across any other useful commands I will post them here. If you have any useful commands you use then please share them in the comments.

Top comments (0)