Docker Commands

Docker commands are used to build images, run containers, inspect logs, manage volumes and networks, clean unused resources, and work with registries such as Docker Hub. This tutorial lists the commonly used Docker CLI commands with examples and explains when each command is useful.

The examples below use the Docker CLI format docker [command] [options]. Some older tutorials show commands such as docker ps and docker images; those still work. Docker also provides grouped command forms such as docker container ls and docker image ls, which are easier to read in scripts and documentation.

Docker Commands Cheat Sheet for Images, Containers, Volumes, and Networks

Use this quick list when you already know what you want to do and only need the command syntax. Detailed examples are provided in the sections that follow.

TaskDocker commandWhat it does
Check Docker installationdocker versionShows Docker client and server version details.
Show Docker environment summarydocker infoDisplays storage driver, containers, images, and daemon information.
List imagesdocker imagesLists locally available Docker images.
Build imagedocker build -t image-name .Builds an image from the Dockerfile in the current directory.
Run containerdocker run image-nameCreates and starts a new container from an image.
Run container in backgrounddocker run -d image-nameStarts the container in detached mode.
List running containersdocker psShows containers that are currently running.
List all containersdocker ps -aShows running and stopped containers.
View container logsdocker logs container-namePrints logs from a container.
Open shell in running containerdocker exec -it container-name shRuns an interactive shell inside a running container.
Stop containerdocker stop container-idStops a running container gracefully.
Remove containerdocker rm container-idRemoves a stopped container.
Remove imagedocker rmi image-nameRemoves an image from the local system.
List volumesdocker volume lsShows Docker-managed volumes.
List networksdocker network lsShows Docker networks.
Clean unused resourcesdocker system pruneRemoves unused containers, networks, images, and build cache after confirmation.

Quick list of Docker Commands

  • docker version – Echoes Client’s and Server’s Version of Docker
  • docker images – List all Docker images
  • docker build <image> – Builds an image form a Docker file
  • docker save <path> <image> – Saves Docker image to .tar file specified by path
  • docker run – Runs a command in a new container.
  • docker start – Starts one or more stopped containers
  • docker stop <container_id> – Stops container
  • docker rmi <image> – Removes Docker image
  • docker rm <container_id> – Removes Container
  • docker pull – Pulls an image or a repository from a registry
  • docker push – Pushes an image or a repository to a registry
  • docker export – Exports a container’s filesystem as a tar archive
  • docker exec – Runs a command in a run-time container
  • docker ps – Show running containers
  • docker ps -a – Show all containers
  • docker ps -l – Show latest created container
  • docker search – Searches the Docker Hub for images
  • docker attach – Attaches to a running container
  • docker commit – Creates a new image from a container’s changes

Docker Commands for Checking Docker CLI and Engine

Following are examples for each of the docker commands

Docker Version

</>
Copy
$ docker version
root@arjun-VPCEH26EN:~# docker version
Client:
 Version:      17.05.0-ce
 API version:  1.29
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:10:54 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.05.0-ce
 API version:  1.29 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:10:54 2017
 OS/Arch:      linux/amd64
 Experimental: false

The docker version command is useful when you want to confirm that both the Docker client and Docker Engine are installed and able to communicate. If the client details appear but the server details fail, the Docker daemon may not be running or your user may not have permission to access it.

Docker System and Help Commands

Before working with images and containers, these commands help you inspect the Docker installation and command syntax.

</>
Copy
docker --help
docker info
docker system df
docker system prune
  • docker --help lists Docker command groups and global options.
  • docker info shows daemon, storage, registry, and runtime details.
  • docker system df shows disk usage by images, containers, volumes, and build cache.
  • docker system prune removes unused Docker objects after confirmation. Use it carefully on development machines that contain stopped containers or unused images you still need.

Docker Image Commands for Building, Listing, Saving, and Removing Images

A Docker image is a read-only package used to create containers. Image commands are used to download images, list local images, build images from Dockerfiles, tag images, push them to registries, and remove images that are no longer required.

List all Docker Images

</>
Copy
$ docker images
root@arjun-VPCEH26EN:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              latest              26acbad26a2c        5 days ago          690MB
java                8                   d23bdf5b1b1b        8 months ago        643MB
hello-world         latest              c54a2cc56cbb        14 months ago       1.85kB

The same result can also be requested with the grouped command form:

</>
Copy
docker image ls

The repository and tag identify the image name, while the image ID identifies the image content. The latest tag is only a tag name; it does not always mean the most recent version of an application.

Create a Docker Image

</>
Copy
$ docker build -t <application_name> .

The directory should contain Dockerfile, from which you are running the command in Terminal.

root@arjun-VPCEH26EN:/home/arjun/workspace/docker/java-application# docker build -t java-application .
Sending build context to Docker daemon  3.072kB
Step 1/5 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/5 : COPY . /home/arjun/workspace/docker/java
 ---> 81af33906fe4
Removing intermediate container 7568d6b873a2
Step 3/5 : WORKDIR /home/arjun/workspace/docker/java
 ---> 8da95950d05c
Removing intermediate container 839d56d42bdf
Step 4/5 : RUN javac HelloWorld.java
 ---> Running in 9c9eb847d3e1
 ---> 7b122f467725
Removing intermediate container 9c9eb847d3e1
Step 5/5 : CMD java HelloWorld
 ---> Running in 8fccd7a65ce1
 ---> 0be5de5c6f51
Removing intermediate container 8fccd7a65ce1
Successfully built 0be5de5c6f51
Successfully tagged java-application:latest
root@arjun-VPCEH26EN:/home/arjun/workspace/docker/java-application# 

The -t option gives the image a name and optional tag. The final dot . tells Docker to use the current directory as the build context.

</>
Copy
docker build -t my-app:1.0 .
docker build --no-cache -t my-app:clean .
docker build -f Dockerfile.prod -t my-app:prod .

Use .dockerignore to exclude files such as build output, local dependencies, logs, and secret files from the build context.

Run a Docker image

</>
Copy
$ docker run <docker_image_name>
root@arjun-VPCEH26EN:~# docker run java-application
HelloWorld from Java Application running in Docker.

The docker run command creates a new container from an image and starts it. If you run the same command again, Docker creates another new container. To restart an existing stopped container, use docker start instead.

</>
Copy
docker run --name web1 nginx
docker run -d --name web2 -p 8080:80 nginx
docker run --rm alpine echo "temporary container"
docker run -it ubuntu bash
  • --name assigns a readable container name.
  • -d runs the container in detached mode.
  • -p 8080:80 maps host port 8080 to container port 80.
  • --rm removes the container automatically after it exits.
  • -it is commonly used for interactive terminal sessions.

Save Docker Image to .tar file

</>
Copy
$ docker save -o <complete_tar_file_path> <docker_image_name>
root@arjun-VPCEH26EN:~# docker save -o /home/arjun/java-application.tar java-application
root@arjun-VPCEH26EN:~# cd /home/arjun/
root@arjun-VPCEH26EN:/home/arjun# ls java-appl*
java-application.tar

Use docker save when you need to move an image to another machine without pulling it from a registry. On the target machine, load the image with docker load.

</>
Copy
docker save -o my-app.tar my-app:1.0
docker load -i my-app.tar

Do not confuse docker save with docker export. docker save saves an image with its metadata and layers. docker export exports a container filesystem as a tar archive.

Remove a Docker image

</>
Copy
$ docker rmi <docker_image_id>

When you list the docker images, you get the image id under IMAGE ID column.

root@arjun-VPCEH26EN:/home/arjun# docker rmi java-application
Untagged: java-application:latest
Deleted: sha256:0be5de5c6f51ffceb18078c42d6e11d94cf844ac1e06841c9bf993a9718cc325
Deleted: sha256:7b122f46772561d5ccd27c5d5a9524c9de4071f3a22234fb2eaa709d42e874b8
Deleted: sha256:2de3fc280f40633a189f24c154faa7c136ba8afbffb9238b8342f06b8bb5d461

If an image is used by a container, Docker may not remove it until the dependent container is removed. List containers with docker ps -a before using a force remove option.

Pull, Tag, Search, and Push Docker Images

Images are commonly pulled from a registry, tagged for a repository, and pushed to a registry after login.

</>
Copy
docker search nginx
docker pull nginx:latest
docker tag my-app:1.0 username/my-app:1.0
docker login
docker push username/my-app:1.0

Use specific version tags in production workflows when possible. A precise tag makes deployments easier to reproduce than a moving tag such as latest.

Docker Container Commands for Running, Inspecting, Stopping, and Removing Containers

A Docker container is a running or stopped instance of an image. Container commands help you list containers, start and stop them, inspect their configuration, view logs, execute commands inside them, and remove containers that are no longer needed.

Show Running Containers

</>
Copy
$ docker ps
root@arjun-VPCEH26EN:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

docker ps shows only running containers. The grouped form is docker container ls.

Show All Containers

</>
Copy
$ docker ps -a
root@arjun-VPCEH26EN:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
743028133ea0        java-application    "java HelloWorld"   2 minutes ago       Exited (0) 2 minutes ago                       awesome_hopper
3b76a109a28f        java-application    "java HelloWorld"   5 minutes ago       Exited (0) 5 minutes ago                       nostalgic_colden

Use docker ps -a when a container appears to be missing. It may have exited immediately after the command inside the container finished.

Show Latest Created Container

</>
Copy
$ docker ps -l
root@arjun-VPCEH26EN:~# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
743028133ea0        java-application    "java HelloWorld"   4 minutes ago       Exited (0) 4 minutes ago                       awesome_hopper

docker ps -l is useful after running a quick test container because it shows the most recently created container.

Start and Restart Docker Containers

Use docker start for a stopped container that already exists. Use docker restart when you want to stop and start the same container again.

</>
Copy
docker start container-name
docker restart container-name

Stop Container

</>
Copy
$ docker stop <container_id>
root@arjun-VPCEH26EN:/home/arjun# docker stop 743028133ea0
743028133ea0

docker stop sends a stop signal and waits before terminating the container. If a container does not stop normally, docker kill can be used, but it should be reserved for cases where a graceful stop is not working.

Remove Container

</>
Copy
$ docker rm <container_id>
root@arjun-VPCEH26EN:/home/arjun# docker rm 743028133ea0
743028133ea0

A container must normally be stopped before it is removed. To remove multiple stopped containers at once, use container pruning after checking that you do not need them.

</>
Copy
docker container prune

View Docker Container Logs and Run Commands Inside a Container

These commands are frequently used while debugging containers.

</>
Copy
docker logs container-name
docker logs -f container-name
docker exec container-name env
docker exec -it container-name sh
docker inspect container-name
  • docker logs prints container logs.
  • docker logs -f follows logs as new lines are written.
  • docker exec runs a command in an already running container.
  • docker inspect returns detailed configuration and runtime information in JSON format.

Some images include bash, while smaller images may include only sh. If docker exec -it container-name bash fails, try sh.

Check Docker Container CPU and Memory Usage

Use docker stats to view live CPU, memory, network, and block I/O usage for running containers.

</>
Copy
docker stats
docker stats container-name
docker update --cpus 1.5 --memory 512m container-name

docker update can change resource limits for supported running containers. Resource behavior can vary by host operating system and Docker configuration.

Docker Volume and Network Commands for Persistent Data and Container Communication

Containers are disposable by design. Use volumes for persistent data and networks for communication between containers.

Docker Volume Commands for Persistent Container Data

</>
Copy
docker volume ls
docker volume create app-data
docker volume inspect app-data
docker run -d --name db -v app-data:/var/lib/mysql mysql
docker volume rm app-data

A named volume such as app-data is managed by Docker and can be reused by containers. Do not remove a volume unless you are sure the data is no longer needed.

Docker Network Commands for Connecting Containers

</>
Copy
docker network ls
docker network create app-net
docker run -d --name web --network app-net nginx
docker network inspect app-net
docker network rm app-net

User-defined bridge networks make container-to-container communication easier because containers can resolve each other by name on the same network.

Docker Compose Commands for Multi-Container Applications

Docker Compose is used when an application needs multiple services such as a web server, database, cache, and worker. Compose commands are run in the directory that contains the Compose file, usually named compose.yaml or docker-compose.yml.

</>
Copy
docker compose up
docker compose up -d
docker compose ps
docker compose logs
docker compose logs -f
docker compose down
docker compose down -v

Use docker compose down -v carefully because it can remove volumes created by the Compose project. Use it only when you intentionally want to remove persistent project data.

Docker Command Options Beginners Should Know

OptionUsed withMeaning
-ddocker runRun container in detached mode.
-itdocker run, docker execOpen an interactive terminal session.
--namedocker runAssign a container name.
-pdocker runPublish a container port to the host.
-vdocker runMount a named volume or host path.
-edocker runSet an environment variable.
--rmdocker runRemove the container automatically after it exits.
-fdocker logs, docker compose logsFollow log output.
-adocker psShow all containers, including stopped containers.

Docker Commands Workflow for a Simple Application

A typical local Docker workflow is to build an image, run a container, inspect it, view logs, stop the container, and clean up when finished.

</>
Copy
docker build -t demo-app .
docker run -d --name demo-app -p 8080:8080 demo-app
docker ps
docker logs -f demo-app
docker exec -it demo-app sh
docker stop demo-app
docker rm demo-app
docker rmi demo-app

This sequence is enough for many beginner-level Docker tasks. For production use, also review image size, secrets handling, non-root users, health checks, logging, networking, and persistent storage.

Docker Commands QA Checklist for Editorial Review

  • Check that each Docker command name is still valid in the current Docker CLI documentation.
  • Keep legacy commands such as docker ps and grouped commands such as docker container ls clearly explained.
  • Verify that examples using docker run distinguish new containers from docker start for existing containers.
  • Warn readers before commands that delete images, containers, volumes, or build cache.
  • Use language-bash for new command-line examples and output for terminal output-only blocks.
  • Prefer official Docker documentation as the reference when adding or changing command behavior.

Docker Commands FAQ

What is the command to list all Docker containers?

Use docker ps -a to list all containers, including running and stopped containers. Use docker ps when you only want running containers.

What is the difference between docker run and docker start?

docker run creates a new container from an image and starts it. docker start starts an existing stopped container without creating a new one.

How do I open a shell inside a Docker container?

Use docker exec -it container-name sh or docker exec -it container-name bash. The shell available depends on what is installed inside the image.

How do I remove unused Docker images and containers?

Use docker container prune to remove stopped containers and docker image prune to remove unused images. Use docker system prune only after checking what will be removed, because it can delete multiple types of unused Docker objects.

Which Docker command shows container logs?

Use docker logs container-name to view logs from a container. Add -f as in docker logs -f container-name to follow the log output continuously.

Conclusion

In this Docker Tutorial – Docker Commands, we have learnt about all docker commands with examples.

For regular Docker work, start with the basic commands for images and containers: docker build, docker run, docker ps, docker logs, docker exec, docker stop, and docker rm. Then add volume, network, registry, and Compose commands as your application setup becomes more involved.