Useful Docker Commands Worth Saving

Useful Docker Commands Worth Saving

by Alex Hyett | 4 min read

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.

🚀 Are you looking to level up your engineering career?

You might like my free weekly newsletter, The Curious Engineer, where I give career advice and tackle complex engineering topics.

📨 Don't miss out on this week's issue

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 helpful? If you want to say thanks, I love coffee ☕️ , any support is appreciated.


ALSO ON ALEXHYETT.COM

What is Event Driven Architecture?

What is Event Driven Architecture?

  • 14 April 2023
One of the leading architecture patterns used with microservices is event-driven architecture. Event-driven architecture has many benefits…
Hosting n8n for Free with Railway

Hosting n8n for Free with Railway

  • 30 January 2023
I have been using n8n for a couple of months now, and it has allowed me to automate so much of my daily workflow. These are some of the…
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…
How to set up Drone CI on Raspberry Pi (and why you shouldn't)

How to set up Drone CI on Raspberry Pi (and why you shouldn't)

  • 27 January 2021
I wanted to put together my home build server using my Raspberry Pi. After looking at the options I picked Drone CI, it has a nice interface…
Traefik vs Nginx for Reverse Proxy with Docker on a Raspberry Pi

Traefik vs Nginx for Reverse Proxy with Docker on a Raspberry Pi

  • 20 January 2021
I use my Raspberry Pi as my own personal home server. Up until recently, I have been using nginx as a reverse proxy for my docker containers…
Hosting a Secure Static Website on AWS S3 using Terraform (Step By Step Guide)

Hosting a Secure Static Website on AWS S3 using Terraform (Step By Step Guide)

  • 14 January 2021
By the time you finish reading this article, you will know how to get your static websites up and running securely on AWS using Terraform…