Jun 23, 2023
3 Min read

Docker CLI quick reference

A quick reference of commonly used docker commands.

The top five commands

Pull container image from Docker Hub.

docker pull <image-name>:latest

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

Build a container image.

docker buildx build .

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

List container images.

docker image ls

Run a container.

docker run <image-name>:<tag>

docker run is an alias for docker 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 buildx build -t <image-name>:<tag> .

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

Build container image for specific platform.

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>

The docker run command is an alternative to docker container run.

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

This runs the container in a interactive session. (The shell does not return).

Run container in detached mode.

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 -d -p 8080:80 nginx

Now, we can reach the HTTP server running in the Nginx container via port 8080 on the localhost.

curl http://127.0.0.1:8080

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 the stopped containers.

docker container ls --all

List running containers where the name contains a specific string.

docker container ls -f "name=squid"

List all containers where the name contains a specific string.

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

Delete stopped containers.

docker container prune

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