Useful Docker Commands Worth Saving

By Alex Hyett on in Software Development

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.

Was this post useful?
If you found this post useful and would like to support me, you can do so by buying me a coffee. Donations help support the running of this blog.


Dealing with Imposter Syndrome as a Software Developer

Dealing with Imposter Syndrome as a Software Developer

  • 28 May 2021
I have been a professional software developer for over a decade and I have been writing code for over 25 years. However, sometimes I still…
Using for Integration Testing

Using for Integration Testing

  • 21 May 2021
Last week I showed you how you can use Wiremock in a docker container to mock API calls that your application uses. This week is on a…
Mocking API calls using WireMock

Mocking API calls using WireMock

  • 14 May 2021
It is rare in software development that you are building something in complete isolation from everything else. Generally, you are going to…
Using ngrok to test local websites and APIs

Using ngrok to test local websites and APIs

  • 07 May 2021
Often when I am creating a new website, I want to see how it is going to look on an actual device like my phone or tablet. You can use…
Using GitHub Actions to Deploy to S3

Using GitHub Actions to Deploy to S3

  • 26 March 2021
Recently I went through the process of setting up Drone CI on my Raspberry Pi. The plan was to use my Raspberry Pi as a build server for…
Getting Started with AWS Step Functions

Getting Started with AWS Step Functions

  • 12 March 2021
I have recently been looking into AWS Step Functions. For those not familiar with them, Step Functions are Amazon’s way of providing a state…
Grafana Monitoring on a Raspberry Pi

Grafana Monitoring on a Raspberry Pi

  • 28 January 2021
As you might have seen from my last few posts I have quite a lot running on my Raspberry Pi. I am currently using a Raspberry Pi 2 B which…

Alex Hyett

Alex Hyett

Software Developer, Entrepreneur, Father, and Husband. Engineering Lead at

Want to get in touch? You can find me here:

Join the Newsletter

Subscribe to get my latest content by email.

    I won't send you spam. Unsubscribe at any time.