List of most used Docker commands

This is the current quick (but growing) reference for the Docker commands I’m using the most. They’re just here for my reference. But, if you’ve stumbled upon them as well, I hope they’re helpful.

I know, I know…many good IDEs will have these built in so you don’t *have to* go through all that command line BS. I use such an IDE, but when I’m learning a new technology, I don’t want to abstract away all of the details. I learn best when I can dig into the details.

One final note: I started writing this using very generic language…’image-name’ with brackets to denote options and such. That can sometimes add to confusion, so I used my own Docker information in most of the examples. I’ve also provided sample output from some commands. Anyway…on with the list!

Docker Images

Build a Docker image

docker build -t image-name[:tag] .

This builds an image, named with an optional tag, and based on a Dockerfile within the current directory (note the dot on the end). While the tag on the end is optional, it’s quite useful…especially for versioning. If you’re planning on pushing your images to Docker Hub, you’ll want to prefix your image name with your username. Here’s a recent example from one of my builds:

docker build -t bitsalt/jeffmoser-com:version1.7 .

List Docker images

docker images

No surprises here. This gives a list of all the images on the machine. Note the Image ID value, as that’s useful in other commands. Here’s some sample output:

bitsalt/jeffmoser-com version1.7.1 3048a699d504 22 hours ago 592MB
bitsalt/jeffmoser-com version1.7 de0115073985 22 hours ago 592MB
<none> <none> f48b524d9db4 26 hours ago 438MB
bitsalt/jeffmoser-com version1.6 e105036dc5a6 26 hours ago 592MB
ubuntu bionic d27b9ffc5667 2 days ago 64.2MB
python 3.6-slim 42ab29dc5406 8 days ago 151MB
nginx latest 2622e6cca7eb 4 weeks ago 132MB

If you noticed that <none> thrown in there, that’s the result of a test and an oversight on my part. We can fix that with the next command.

Rename or add tag to an image

docker tag f48b524d9db4 bitsalt/jeffmoser-com:version1.6.1

That <none> entry was for something unrelated, but let’s say that it was part of the jeffmoser-com build. I would use the Image ID value from the result of running ‘docker images’ (f48b524d9db4) to reference it, and then add the name and tag.

Remove an image

docker image rm jeffmoser-flask:latest

If you need to clean up images selectively, use this command. There are occasionally times when the optional ‘–force’ switch is needed. Perhaps I’ll go into that more later…

Docker Containers

Run a new Docker container

docker run -d --name jeffmoser-com -p 80:80 bitsalt/jeffmoser-com:version1.7.1

The ‘docker run’ command starts a new container from the given image. In this example, I’ve named the container ‘jeffmoser-com’ with the ‘–name’ option.

The ‘-d’ switch runs the process in the background–detached. Without that switch, you would then have the console as an interactive shell for the container.

The -p option publishes the container’s ports to the host format. In the example above, I’ve just instructed the host to map all requests on port 80 to the container’s port 80. The format is [host-port:container-port]. I could also have used a range of ports… -p [5000-5100:5000-5100]…or mapped to differing ports if, for instance, I wanted to direct incoming requests directly to a Flask app: -p[80:5000]. That’s not a good idea in production, by the way. So just…don’t.

There are a lot of potentially useful options with this. Full info on the ‘run’ command can be found in the Docker reference.

List running Docker containers

docker ps

This shows all currently running containers.

List all Docker containers

docker container ls

This shows all containers on the machine, running or not.


And…this list is already longer than I thought it would be. I’ll continue to add to it as more commands and/or switches become useful. The list below is what’s left to go through as of now. But…I need to get some work done.

docker system prune
docker images -a
docker images -f dangling=true
docker images purge
docker images -a |grep “none” | awk ‘{print $3}’ |xargs docker rmi –force
docker rmi $(docker images -a -q)
docker pull bitsalt/jeffmoser-com:version1.0
docker container restart 7320b7b86ad3
docker container stop 7320b7b86ad3
sudo systemctl restart docker.socket docker.service

Leave a Reply

%d bloggers like this: