Top 20 Essential Docker Commands: How They Work and When to Use Them

Learn 20 essential Docker commands with practical examples to manage containers, images, networks, volumes, and Docker Compose. A useful guide for developers learning Docker.

20 essential Docker commands for managing containers and images

Docker has become an essential tool for building, packaging, and running applications in consistent environments. 

Whether you're a beginner learning containerization or a developer managing production applications, understanding the most commonly used Docker commands can make your workflow faster and more efficient.

Docker commands help you download images, create and manage containers, troubleshoot application issues, build custom images, and manage networking and storage. 

Instead of relying entirely on graphical interfaces, developers can use the Docker command-line interface (CLI) to perform these tasks directly.

In this guide, we'll cover 20 essential Docker commands, with their syntax, practical examples, and explanations of when to use them.

1. docker --version

The docker --version command displays the version of the Docker command-line interface (CLI) installed on your system. It is often one of the first commands developers run when setting up Docker.

Syntax:

bash
docker --version

How it works: Docker prints the installed CLI version without requiring you to start a container or communicate with the Docker Engine.

Example:

bash
docker --version

Sample output:

output
Docker version 28.0.1, build ...

The output depends on your installed version.

When to use it:

  • When checking whether the Docker CLI is installed.
  • When troubleshooting version-related compatibility issues.
  • Before following a Docker tutorial that requires a particular version.

Note that this command checks the CLI version, not whether the Docker Engine is running.

Docker containers often rely on networking and storage to work effectively. Learn more about Docker volumes, bind mounts, networking, and Docker Compose through Great Learning Academy’s Intermediate Level Docker course. 

2. docker pull

The docker pull command downloads a Docker image from a container registry, such as Docker Hub. An image contains the filesystem and configuration used to create containers.

Syntax:

bash
docker pull IMAGE_NAME

How it works: Docker contacts the configured registry, retrieves the image manifest and required image layers, and stores them locally. Existing layers can be reused rather than downloaded again.

Example:

bash
docker pull nginx

This downloads the default-tag Nginx image.

When to use it:

  • Before running an application from an image that is not available locally.
  • When obtaining a newer or specific version of an image.
  • When preparing a development environment using an existing image.

You can specify a tag to select a version:

bash
docker pull nginx:1.28

Use a tag appropriate to your application's compatibility requirements.

3. docker images

The docker images command lists Docker images stored in your local image store.

Syntax:

bash
docker images

How it works: Docker displays locally available images and their repository names, tags, image IDs, creation information, and sizes.

Example:

bash
docker images

Sample output:

output
REPOSITORY   TAG      IMAGE ID       SIZE
nginx        latest   abc123def456   192MB
ubuntu       latest   def456abc789   78MB

These values are illustrative; actual IDs and sizes vary.

When to use it:

  • To check whether an image has already been downloaded.
  • To find the image name or tag needed for docker run.
  • To identify images that may no longer be needed.

For example, after running docker pull nginx, use docker images to confirm that the image is available locally.

4. docker run

The docker run command creates a new container from an image and starts it.

Syntax:

bash
docker run [OPTIONS] IMAGE_NAME

How it works: Docker creates a container with its own writable layer and configured resources, then starts the main process defined by the image or overridden by the command. The container continues running while its main process remains active.

Example:

bash
docker run -d -p 8080:80 --name my-nginx nginx

Here's what each part does:

  • -d runs the container in the background.
  • -p 8080:80 maps host port 8080 to container port 80.
  • --name my-nginx assigns a convenient name.
  • nginx identifies the image to use.

If the container starts successfully and the port is accessible, you can visit http://localhost:8080 to access the Nginx server.

When to use it:

  • When starting a new application container.
  • When testing an image locally.
  • When running services such as web servers, databases, or development tools.

For example, you can launch a temporary Ubuntu environment with docker run -it ubuntu bash, provided the image supports Bash.

5. docker ps

The docker ps command lists containers that are currently running.

Syntax:

bash
docker ps

How it works: Docker queries the Docker Engine and displays information about active containers, including their IDs, images, status, port mappings, and names.

Example:

bash
docker ps

Sample output:

output
CONTAINER ID   IMAGE   STATUS         PORTS
a1b2c3d4e5f6   nginx   Up 2 minutes   0.0.0.0:8080->80/tcp

When to use it:

  • To verify that a container started successfully.
  • To identify a container before stopping or inspecting it.
  • To check published ports and running status.

For example, after using docker run, execute docker ps to confirm that your container is running.

6. docker ps -a

The docker ps -a command lists all containers, including running, stopped, and exited containers.

Syntax:

bash
docker ps -a

How it works: Unlike docker ps, which filters the output to running containers, the -a option includes containers that have finished or stopped.

Example:

bash
docker ps -a

You might see a container with the status Exited (1). This means its main process exited with status code 1, but the output alone does not explain why.

When to use it:

  • When a container disappears from the running-container list.
  • When investigating an application that failed to start.
  • When locating stopped containers that you want to restart or remove.

To investigate an exited container, use docker logs CONTAINER_NAME to review its available logs.

7. docker start

The docker start command starts an existing stopped container.

Syntax:

bash
docker start CONTAINER_NAME

How it works: Docker starts the existing container using its saved configuration, including its image, environment settings, and resource configuration. It does not create a new container.

Example:

bash
docker start my-nginx

If my-nginx is a stopped container, Docker starts it again.

When to use it:

  • When restarting a container that was previously stopped.
  • When resuming work without creating another container.
  • When preserving the existing container's writable-layer state.

Important distinction: Use docker run to create and start a new container. Use docker start to start an existing one.

8. docker stop

The docker stop command stops a running container gracefully.

Syntax:

bash
docker stop CONTAINER_NAME

How it works: Docker sends the container's main process a termination signal, normally SIGTERM, and waits for a grace period. If the process does not stop in time, Docker forcefully terminates it.

Example:

bash
docker stop my-nginx

When to use it:

  • When shutting down a development container.
  • Before removing a container.
  • During maintenance or troubleshooting.
  • When stopping a service without immediately deleting its container.

Graceful shutdown is particularly important for applications that need time to finish requests or close connections.

9. docker restart

The docker restart command stops and starts an existing container.

Syntax:

bash
docker restart CONTAINER_NAME

How it works: Docker stops the container, waits for the configured shutdown grace period, and starts it again.

Example:

bash
docker restart my-nginx

When to use it:

  • When an application process needs to be restarted.
  • When troubleshooting a temporary application issue.
  • When restarting a service without creating a new container.

Restarting a container does not rebuild its image or automatically apply changes made to its Dockerfile. If the image needs updating, you generally need to build or pull the updated image and recreate the container.

10. docker rm

The docker rm command removes a container from Docker.

Syntax:

bash
docker rm CONTAINER_NAME

How it works: Docker removes the specified container and its writable container layer. A running container generally must be stopped first unless you explicitly use force removal.

Example:

bash
docker stop my-nginx
docker rm my-nginx

The first command stops the container, and the second removes it.

When to use it:

  • When removing temporary test containers.
  • When cleaning up containers that are no longer required.
  • When replacing an old container with a newly configured one.

Important: Removing a container does not ordinarily remove its image or separately stored named volumes. However, data saved only in the container's writable layer can be lost.

11. docker rmi

The docker rmi command removes a Docker image from local storage.

Syntax:

bash
docker rmi IMAGE_NAME

How it works: Docker removes the image reference and reclaims image data when it is no longer needed by other references or dependencies. Removal may be blocked if containers depend on the image.

Example:

bash
docker rmi nginx

When to use it:

  • When cleaning up unused images.
  • When freeing disk space.
  • When removing an image version that is no longer required.

If Docker refuses to remove the image, inspect the containers that reference it before attempting cleanup.

Difference between docker rm and docker rmi: docker rm removes a container; docker rmi removes an image.

12. docker exec

The docker exec command runs a new process inside a running container.

Syntax:

bash
docker exec -it CONTAINER_NAME COMMAND

How it works: Docker asks the Engine to start an additional process inside the existing container. This process runs separately from the container's main process.

Example:

bash
docker exec -it my-nginx sh

This opens a shell inside the running container if sh is available.

The options mean:

  • -i keeps standard input open.
  • -t allocates a terminal.
  • sh starts the shell.

When to use it:

  • To inspect files inside a container.
  • To run diagnostic commands.
  • To troubleshoot an application without stopping it.
  • To access a shell when the image includes one.

For example, you can use docker exec to inspect configuration files or check the processes inside a running development container.

13. docker logs

The docker logs command displays logs available from a container's main process.

Syntax:

bash
docker logs CONTAINER_NAME

How it works: Docker retrieves the container's available standard output and standard error logs, subject to the configured logging driver and logging setup.

Example:

bash
docker logs my-nginx

To follow new log entries continuously:

bash
docker logs -f my-nginx

To show only the most recent 100 lines:

bash
docker logs --tail 100 my-nginx

When to use it:

  • When a container fails to start.
  • When diagnosing application errors.
  • When checking startup messages or request-related output.
  • When monitoring a service during testing.

Remember that docker logs may not show all application logs if the application writes them only to internal files rather than standard output or standard error.

14. docker build

The docker build command creates a Docker image from a Dockerfile and its build context.

Syntax:

bash
docker build -t IMAGE_NAME .

How it works: Docker processes the instructions in the Dockerfile, such as selecting a base image, copying application files, installing dependencies, and configuring the startup command. The result is a new image that can be used to create containers.

Example:

bash
/docker build -t my-app .

Here, -t my-app assigns a name to the image, and . specifies the current directory as the build context.

A simple Dockerfile might look like this:

output
FROM python:3.12-slim
WORKDIR /app
COPY . .
CMD ["python", "app.py"]

This example uses a Python base image, sets a working directory, copies files into the image, and defines the default command.

When to use it:

  • When packaging your own application.
  • When creating repeatable development environments.
  • When preparing an application image for deployment.
  • When updating the image after changing application dependencies or build instructions.

Use a suitable .dockerignore file to exclude unnecessary files from the build context.

15. docker tag

The docker tag command creates a new tag that refers to an existing image.

Syntax:

bash
docker tag SOURCE_IMAGE TARGET_IMAGE

How it works: Docker assigns an additional name or tag to the image. This does not rebuild the image or change the files inside it.

Example:

bash
docker tag my-app my-app:v1.0

The image can now be referenced using the my-app:v1.0 tag.

You can also prepare an image for a registry:

bash
docker tag my-app myusername/my-app:v1.0

When to use it:

  • When assigning version labels to images.
  • When preparing an image for publishing.
  • When maintaining development, testing, and release tags.

For production workflows, version-specific tags or immutable image digests can help avoid confusion caused by tags that are later reassigned.

16. docker push

The docker push command uploads a Docker image to a container registry.

Syntax:

bash
docker push REGISTRY/USERNAME/IMAGE_NAME:TAG

How it works: Docker authenticates with the registry when required and uploads the image layers and manifest that are not already present in the target repository.

Example:

bash
docker tag my-app myusername/my-app:v1.0
docker push myusername/my-app:v1.0

You need the appropriate registry permissions and must use a repository name that matches the destination registry.

When to use it:

  • When publishing an application image.
  • When sharing images with teammates.
  • When preparing images for deployment on servers or cloud platforms.
  • When integrating image publishing into a CI/CD pipeline.

For private repositories, confirm that authentication and access permissions are configured correctly.

17. docker network ls

The docker network ls command lists the Docker networks available on your system.

Syntax:

bash
docker network ls

How it works: Docker displays networks and details such as their names, drivers, and scope. Containers connected to compatible networks can communicate according to the network configuration and applicable security rules.

Example:

bash
docker network ls

You may see networks such as bridge, host, and none, along with networks created by your applications.

When to use it:

  • When troubleshooting container connectivity.
  • When checking networks created for multi-container applications.
  • When investigating how services communicate with one another.

For example, if a web application cannot connect to its database container, inspecting the network configuration is a useful troubleshooting step.

18. docker volume ls

The docker volume ls command lists Docker-managed volumes.

Syntax:

bash
docker volume ls

How it works: Docker displays volumes that can be mounted into containers to store data separately from a container's writable layer. Named volumes can persist even when the containers using them are removed.

Example:

bash
docker volume ls

To create a named volume:

bash
docker volume create app_data

You can then mount it into a container:

bash
docker run -d --name app-container -v app_data:/data nginx

This mounts the named volume at /data inside the container.

When to use it:

  • When managing persistent application data.
  • When working with databases in containers.
  • When checking which volumes are available before cleanup.
  • When separating application data from the container lifecycle.

Important: Removing a volume can permanently delete the data it contains. Always verify whether a volume is still needed before deleting it.

19. docker inspect

The docker inspect command displays detailed information about a Docker object, including containers, images, networks, and volumes.

Syntax:

bash
docker inspect OBJECT_NAME

How it works: Docker returns a JSON document containing configuration and runtime details for the specified object.

Example:

bash
docker inspect my-nginx

The output may include:

  • Container configuration and state.
  • Network settings and IP addresses.
  • Mounted volumes and bind mounts.
  • Environment variables.
  • Image and startup-command information.

You can also extract a specific field using a Go template:

bash
docker inspect -f '{{.State.Status}}' my-nginx

This prints the container's state status.

When to use it:

  • When investigating container configuration.
  • When troubleshooting network or storage problems.
  • When verifying environment settings.
  • When scripting tasks that require specific container details.

Because the output can contain sensitive configuration values, avoid sharing it publicly without reviewing it first.

20. docker compose up

The docker compose up command creates and starts the services defined in a Docker Compose configuration file.

Syntax:

bash
docker compose up

How it works: Docker Compose reads the configuration, prepares the required networks and volumes, creates or updates the defined services, and starts their containers.

Example:

bash
docker compose up -d

The -d option runs the services in the background.

For example, a Compose file might define a web application and a database:

bash
services:
  web:
    image: nginx
    ports:
      - "8080:80"

  database:
    image: postgres:17
    environment:
      POSTGRES_PASSWORD: example

This is a simplified demonstration. Real applications should use appropriate database credentials and secure secret management.

To check the services:

bash
docker compose ps

To stop and remove the Compose-managed containers and networks:

bash
docker compose down

When to use it:

  • When running applications that need multiple containers.
  • When setting up repeatable development environments.
  • When coordinating application services and dependencies.
  • When managing local integration-testing environments.

Quick reference: Which Docker command should you use?

If you want to…Use this command
Check Docker CLI versiondocker --version
Download an imagedocker pull
List local imagesdocker images
Create and start a containerdocker run
List running containersdocker ps
List all containersdocker ps -a
Start a stopped containerdocker start
Stop a containerdocker stop
Restart a containerdocker restart
Remove a containerdocker rm
Remove an imagedocker rmi
Run a command in a containerdocker exec
View container logsdocker logs
Build an imagedocker build
Add an image tagdocker tag
Upload an imagedocker push
List networksdocker network ls
List volumesdocker volume ls
Inspect Docker configurationdocker inspect
Start multiple servicesdocker compose up

Conclusion

These 20 essential Docker commands cover the core tasks developers perform when working with containers, images, networking, persistent storage, and multi-service applications.

Understanding not just the syntax but also how each command works and when to use it helps you choose the right command for the task. Start with running containers, checking their status, and reading logs. 

Then practice building images, managing volumes and networks, and running applications with Docker Compose.

With regular practice, these commands can become a natural part of your development workflow and help you troubleshoot Docker-based applications more confidently.

To put your knowledge into practice, explore Great Learning Academy’s Docker Projects. It offers hands-on experience deploying a Spring Boot application and deploying and monitoring an online store using Docker.

Frequently Asked Questions About Docker Commands

1. How can you learn Docker best practices and improve your Docker skills?

You can improve your Docker skills by learning container management, Dockerfile optimization, image creation, and security best practices. Great Learning Academy offers a free Docker best practices course for beginners, with a 2.25-hour learning duration and practical topics to help you use Docker more effectively

2. How do you check Docker disk usage?

Use the docker system df command to check how much disk space Docker images, containers, volumes, and build cache consume. It helps identify storage usage and determine whether cleanup is necessary.

3. How do you remove unused Docker resources?

Run docker system prune to remove stopped containers, unused networks, dangling images, and build cache. This Docker command helps free disk space, but review the cleanup prompt carefully before confirming.

4. How do you copy files to and from a Docker container?

Use the docker cp command to transfer files between your host machine and a Docker container. For example, docker cp app.txt my-container:/app/app.txt copies a local file into the container.

5. How do you monitor Docker container resource usage?

Run docker stats to monitor real-time CPU usage, memory consumption, network activity, and disk I/O for running containers. This Docker command helps identify resource-intensive applications and investigate performance issues.

6. How do you rename a Docker container?

Use the docker rename OLD_NAME NEW_NAME command to change an existing container's name without recreating it. Renaming makes containers easier to identify and manage when working with multiple applications.

7. How do you run a Docker container interactively?

Use docker run -it ubuntu bash to launch an interactive Ubuntu container. The -i option keeps standard input open, while -t allocates a terminal for executing Linux commands.

8. How do you check the health status of a Docker container?

Use docker inspect CONTAINER_NAME to examine a container's health status when a health check is configured. Docker reports healthy, unhealthy, or starting, helping developers assess application health beyond its running state.

Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.

Go Beyond Learning. Get Job-Ready.

Build in-demand skills for today's jobs with free expert-led courses and practical AI tools.

Explore All Courses
Scroll to Top