Alex Hyett
Alex Hyett

Alex Hyett

Useful Docker Commands Worth Saving

Useful Docker Commands Worth Saving

Alex Hyett's photo
Alex Hyett
·Feb 12, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

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

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

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

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

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

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

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

docker exec -it 0c7b2063b2a2 /bin/sh

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

You can then run it and connect to it using:

docker run -it --rm test/image /bin/bash

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

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

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

Copy files TO container

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

docker cp fstab 0c7b2063b2a2:/etc/fstab

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.

Share this