Docker Commands: A Comprehensive List
Docker is a powerful containerization platform that allows developers to package and deploy their applications in a portable way. However, mastering Docker requires familiarity with a wide range of commands. In this article, we will explore a comprehensive list of Docker commands that can help you manage containers, images, networks, and volumes.
Table of Contents
-
Process Management
-
Volumes & Ports
- List volumes
- Create a volume
- Delete a volume
- Show volume metadata
- Delete all volumes not attached to a container
- Mount a local directory to your container
- Copy file or folder from a docker container to host machine
- Copy file or folder from local machine onto a container
- Map a local port to a docker instance
- List the ports a docker container is running on
-
Docker Compose
-
Images/Repository
-
Troubleshooting
- Show the logs of a container
- Follow/tail the logs of a container
- Show timestamps on docker logs
- Show details/metadata of a container
- Show a 'top' view of processes running on a container
- Show a 'top' view of all docker containers
- Show any files that have changed since startup
- Connect to an already running container
- Execute a command on a container
- Show docker system wide information
- Show docker disk space used
Docker Commands
-
Process Management
-
Show all running docker containers
<code class="language-sh">docker ps
-
Show all docker containers
<code class="language-sh">docker ps -a
-
Run a container
<code class="language-sh">docker run <image>:<tag>
-
Run a container and connect to it
<code class="language-sh">docker run -it <image>:<tag>
-
Run a container in the background
<code class="language-sh">docker run -d <image>:<tag>
-
Stop a container
<code class="language-sh">docker stop <container>
-
Kill a container
<code class="language-sh">docker kill <container>
-
-
Volumes & Ports
-
List volumes
<code class="language-sh">docker volume ls
-
Create a volume
<code class="language-sh">docker volume create <volume>
-
Delete a volume
<code class="language-sh">docker volume rm <volume>
-
Show volume metadata
<code class="language-sh">docker volume inspect <volume>
-
Delete all volumes not attached to a container
<code class="language-sh">docker volume prune
-
Mount a local directory to your container
<code class="language-sh">docker run -v <local_dir>:<container_dir> <image>
-
Copy file or folder from a docker container to host machine
<code class="language-sh">docker cp container>:<container_dir> <local_dir>
-
Copy file or folder from local machine onto a container
<code class="language-sh">docker cp <local_dir> <container>:<container_dir>
-
Map a local port to a docker instance
<code class="language-sh">docker run -d -p 127.0.0.1:<local_port>:<docker_port> <image>
-
List the ports a docker container is running on
<code class="language-sh">docker port <container>
-
-
Docker Compose
-
Start your docker-compose defined resources in detached mode
<code class="language-sh">docker-compose up -d -f <docker_compose_yaml>
-
Stop all docker-compose resources
<code class="language-sh">docker-compose stop
-
Destroy all docker-compose resources
<code class="language-sh">docker-compose down
-
Show docker-compose processes
<code class="language-sh">docker-compose ps
-
Show docker-compose logs
<code class="language-sh">docker-compose logs
-
Show docker-compose resource consumption
<code class="language-sh">docker-compose top
-
-
Images/Repository
-
List available local images
<code class="language-sh">docker images
-
Search for docker images
<code class="language-sh">docker search <image>
-
Pull a docker image
<code class="language-sh">docker pull <image>
-
Build an image with a dockerfile
<code class="language-sh">docker build -t <image>:<tag> <run_directory> -f <dockerfile>
-
Login to a remote repository
<code class="language-sh">docker login <repository>
-
Push an image to your remote repository
<code class="language-sh">docker push <image>:<tag>
-
Remove a local docker image
<code class="language-sh">docker rmi <image>:<tag>
-
Show metadata for an image
<code class="language-sh">docker inspect <image>
-
Remove all unused docker images
<code class="language-sh">docker image prune
-
-
Troubleshooting
-
Show the logs of a container
<code class="language-sh">docker logs <container>
-
Follow/tail the logs of a container
<code class="language-sh">docker logs -f <container>
-
Show timestamps on docker logs
<code class="language-sh">docker logs -t <container>
-
Show details/metadata of a container
<code class="language-sh">docker inspect <container>
-
Show a 'top' view of processes running on a container
<code class="language-sh">docker top <container>
-
Show a 'top' view of all docker containers
<code class="language-sh">docker stats
-
Show any files that have changed since startup
<code class="language-sh">docker diff <container>
-
Connect to an already running container
<code class="language-sh">docker attach <container>
-
Execute a command on a container
<code class="language-sh">docker exec -it <container_id> /bin/bash
-
Show docker system wide information
<code class="language-sh">docker system info
-
Show docker disk space used
<code class="language-sh">docker system df
-
Understanding these commands will help you streamline your workflow, improve productivity, and make the most out of Docker's containerization platform.