Docker Commands You Must Know

I’ve been using Docker extensively over the past few years and I’ve compiled a list of the commands that I use most frequently. Docker can be intimidating at first glance, but it’s actually quite simple. I honestly believe if you focused on the commands in this list, you will have all that you need to be up and running with Docker. Wether you are a Docker novice or Docker expert, these are the top commands you must know.

To try these commands out, install docker. I also recommend signing up for a free account on Docker Hub so you can pull and push images to your own repo. Let’s get going!

Basic Commands

Command Description
docker --version Docker version info for client and sever
docker info Detailed Docker info about your Docker setup
docker login If you have a private repo, use this command to login
docker logout To logout
docker pull nginx Pull an image
docker images List local images
docker run -p 80:80 nginx Run a container from an image and map the container port to a host port
docker run -d -p 80:80 nginx Run a container in the background
docker ps List running containers
docker ps -a List running and stopped containers
docker logs <container_id> View container logs
docker logs -f <container_id> Tail container logs
docker exec -it <container_id> sh Jump into a running container with an iteractive shell
docker stats Get memory and I/O stats on running containers
docker inspect nginx Get info on a docker object (image, container, etc.)
docker stop <container_id> Stop a running container
docker kill <container_id> Kill a running container
docker kill -s HUP <container_id> Send a signal to running container
docker search <search_string> --limit 5 Search Docker Hub for images
docker network create my-net Create a user-defined network
docker network ls List networks
docker run --network=my-net -p 80:80 nginx Run a container on your user-defined network

Cleanup Commands

Command Description
docker rmi -f <image_id> Remove an image
docker rm -f <container_id> Remove a container
docker rm $(docker ps -a -q) Remove all containers
docker volume ls -qf dangling=true | xargs -r docker volume rm Remove all docker volumes
docker rmi $(docker images -q) Remove all docker images
docker rmi -f $(docker images | grep "<none>"| awk "{print \$3}") Remove all untagged images
docker images -q --filter=dangling=true | xargs docker rmi -f Remove all untagged images
docker volume prune Remove unused volumes
docker network rm <network_id> Remove a Network

Running Commands on all containers

Command Description
docker stop $(docker ps -q) Stop all containers
docker kill $(docker ps -q) Kill all containers

Commands when working with a Dockerfile

Here are the commands you’ll use when working with a Dockerfile. Let’s say we have a directory named node with the following contents:

A Dockerfile

FROM node
RUN mkdir -p /usr/src/app
COPY index.js /usr/src/app
CMD [ "node", "/usr/src/app/index" ]

An index.js file

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(`<h1>Hello World!</h1>`);
    res.end();
}).listen(8090);

Your directory should now look like this:

$ ll node
total 16
-rw-r--r--   1 ralphmcneal  staff  503 May 27 07:45 index.js
-rw-r--r--   1 ralphmcneal  staff  100 May 27 07:45 Dockerfile
Command Description
docker build -t hello-node node/. Build an image
docker tag hello-node hello-node:1.0.0 Add a custom tag
docker run -d -p 8090:8090 hello-node:1.0.0 Run a container using your image

Pushing to a repo

Command Description
docker tag hello-node <repo_name>/hello-node:1.0.0 To push the image to a repo, you must tag it with the repo name
docker login <repo_name> Login to your repo
docker push <repo_name>/hello-node:1.0.0 Now we can push the image to the repo with the push command

Summary

This is a good list of the Docker commands you’ll use on a daily basis. There are many more commands but those will be used infrequently. At the point where you have a good understanding of the commands listed here, their variants, and when to use them, I’d say you are on your way to being a Docker pro. Please post any commands that I may have missed. Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *