Docker CLI quick reference

A quick reference of commonly used docker commands.

The top five

Pull container image from Docker Hub

docker pull <image-name>:latest

This pulls the latest version of the image from Docker Hub.

Build container image

docker build .

The Dockerfile must exist in the path from where you are running this command.

List container images

docker image ls

Run container

docker run <image-name>:<tag>

docker run is an alias for docke container run.

List running containers

docker conatiner ls

Manage container images

Pull specific version of a container image from docker hub

docker pull <image>:<tag>

List all images

docker image ls

Build image with name and tag

docker build -t <image-name>:<tag> .

The Dockerfile must exist in the path from where you are running this command.

Build container image for x86 from M1 Macbook

docker buildx build --platform linux/amd64 .

The Dockerfile must exist in the path from where you are running this command.

Check Buildx availability in your Docker CLI

docker buildx version

The Buildx plugin is included in Docker Desktop on Windows, Mac, and Linux (if installed from a DEB or RPM package). If you are on any of the above platforms and if Buildx is still not available, try updating Docker to the latest version.

Or install Buildx manually by following the instructions here.

Push image to Docker Hub

Before pushing an image to Docker Hub, you must create a Docker Hub repository.

Login to Docker huub from Docker CLI.

docker login

Input the username and password when requested.

Push image.

docker tag <image-name>:<tag> <docker-hub-username>/<repo-name>:<tag>
docker push <docker-hub-username>/<repo-name>:<tag>

image-name and tag could be any image that you have built locally with docker build.

docker-hub-username is your username at Docker Hub.

repo-name is the name of the repository that you have created at Docker Hub.

Verify image architecture

docker image inspect <image-id> | grep -i architecture

Delete image

docker image rm <image-id>

Manage containers

Run container

docker run <image-name>:<tag>

Or

docker container run <image-name>:<tag>

This runs the container in a interactive session. (The shell does not return). Run in the detached mode for the shell to return.

Run container in detached mode

docker run -d <image-name>:<tag>

Or

docker container run -d <image-name>:<tag>

Publish container port to localhost port

docker run -p <localhost port>:<container-port> <image-name>:<tag>

The host will forward all traffic destined to localhost-port to the container-port in the running container.

Example:

We are running NGNIX web server on port 8080 in the container.

docker run -p 80:8080 nginx

Now, we can reach the HTTP server running in port 8080 on nginx via:

curl http://127.0.0.1/xyz...

List running containers

docker container ls

Stop container

docker container stop <container-id>

Delete container

docker container rm <container-id>

You must stop the container before deleting.

List all containers (including stopped)

docker container ls --all

List running containers where name contains a specific string

docker container ls -f "name=squid"

List all containers where name contains a specific string

docker container ls -f "name=squid" --all

Getting help

Get help about all available commands.

docker --help

Get specific details about a command.

docker <command> --help

Example: Getting help for container command.

docker container --help

Example: Getting help for container ls command.

docker container ls --help
Written by Indika
...