Introduction
Docker is an open source platform that gives developers everything they need to create, package, and deploy applications in a simplified way. With Docker container technology, you can bundle your applications and all their dependencies into a single self-contained unit that can easily move across different platforms and run seamlessly in containers. However, to take full advantage of what Docker offers, you should become familiar with its command line interface (CLI). In this article, we'll walk you through the Docker commands that every developer and sysadmin should have in their toolkit.
Why do you need Docker commands?
Docker commands are essential for managing and interacting with Docker containers and images. It includes creating, running, stopping, deleting containers, and creating images from Dockerfiles. Additionally, it allows you to perform tasks such as listing active containers, checking the status of containers, transferring files between the host machine and containers, and managing Docker networks and Docker volumes. In use, it is impossible to achieve the desired state of using Docker to contain applications, achieve portability, and facilitate deployment on multiple platforms.
How to use Docker commands?
Below are some common ways to use Docker commands:
- run a container:
docker run (OPTIONS) IMAGE(:TAG|@DIGEST) (COMMAND) (ARG...)
This command creates and starts a new container from the specified image. - List running containers:
docker ps
Lists all containers currently running. - stop a container:
docker stop CONTAINER_ID
Stops the running container specified by its ID or name. - Remove a container:
docker rm CONTAINER_ID
Remove the stopped container from the system. - Take an image:
docker pull IMAGE(:TAG|@DIGEST)
Downloads the specified image from a registry (for example, Docker Hub). - build an image:
docker build (OPTIONS) PATH | URL | -
Create a new image from the instructions in a Dockerfile. - List images:
docker images
Lists all images available on the local system. - Delete an image:
docker rmi IMAGE(:TAG|@DIGEST)
Removes the specified image from the local system. - Run a command in a container:
docker exec (OPTIONS) CONTAINER_ID COMMAND (ARG...)
Runs the specified command inside a running container. - View logs:
docker logs CONTAINER_ID
Gets the logs for the specified container.
These are just some examples. Below I have provided a list of docker commands. You can also explore more commands and their options by running docker --help
or refer to the official docker documentation.
Here is the list of main Docker commands
Dockable version
He docker version
The command displays the current version of Docker installed on your system. Provides information about Docker client and server versions, as well as other details such as operating system, architecture, and kernel version.
Use
docker version
docker search
He docker search
The command allows you to search for Docker images on Docker Hub, the official Docker image registry. You can search for images by name or use keywords to find relevant images.
Use
docker search <image_name>
docker pull
He docker pull
The command downloads a Docker image from a registry (such as Docker Hub) to your local machine. You must pull an image before creating a container.
Use
docker pull <image_name>:<tag>
running docker
He docker run
The command is one of the most used Docker commands. Creates a new container from a specified image and starts it. You can pass various options to customize the behavior of the container, such as exposing ports, mounting volumes, and setting environment variables.
Use
docker run (OPTIONS) <image_name>:<tag> (COMMAND) (ARG...)
docker ps
He docker ps
The command lists all containers that are currently running on your system. By default, it shows only running containers, but you can use the -a
flag to list all containers (running and stopped).
Use
docker ps
docker ps -a
stop docker
He docker stop
The command stops one or more running containers. You can specify the container by its name or ID.
Use
docker stop <container_name_or_id>
docker restart
He docker restart
The command restarts one or more running containers. It first stops the containers and then starts them again.
Use
docker restart <container_name_or_id>
dockworker massacre
He docker kill
The command forcibly stops a running container by sending a KILL signal. It should be used when the docker stop
The command fails to stop a container correctly.
Use
docker kill <container_name_or_id>
docker executive
He docker exec
The command executes a new command inside a running container. This is useful for inspecting or troubleshooting containers without starting a new shell.
Use
docker exec (OPTIONS) <container_name_or_id> (COMMAND) (ARG...)
login to docker
He docker login
The command authenticates you with a Docker registry, such as Docker Hub. You must be authenticated to submit images to a registry.
Use
docker login (OPTIONS) (SERVER)
docker commit
He docker commit
The command creates a new image from a container's changes. This is useful for capturing the state of a running container and creating a new image based on that state.
Use
docker commit (OPTIONS) <container_name_or_id> (REPOSITORY(:TAG))
docker push
He docker push
The command uploads an image to a Docker registry, such as Docker Hub. You must be authenticated at the registry before submitting an image.
Use
docker push <image_name>:<tag>
attachable network
He docker network
The command manages Docker networks. It allows you to create, inspect and manage networks for communication between containers.
Use
docker network (COMMAND) (ARG...)
docker history
He docker history
The command displays the history of an image, including the layers that make up the image and the commands used to create each layer.
Use
docker history <image_name>:<tag>
rmi coupler
He docker rmi
The command removes one or more images from your local system. You must stop and delete all containers based on the image before deleting the image itself.
Use
docker rmi <image_name>:<tag>
docker ps -a
He docker ps -a
The command lists all containers (running and stopped) on your system. It is a useful command to get an overview of all the containers on your machine.
Use
docker ps -a
dockable copy
He docker copy
The command copies files or directories between a container and the local file system.
Use
docker copy (OPTIONS) <container_name_or_id>:<src_path> <dest_path>
docker copy (OPTIONS) <src_path> <container_name_or_id>:<dest_path>
docker logs
He docker logs
The command retrieves log output from a container. It is an essential command for troubleshooting and debugging containers.
Use
docker logs (OPTIONS) <container_name_or_id>
docker volume
He docker volume
The command manages Docker volumes. Volumes are used to persist data generated by Docker containers.
Use
docker volume (COMMAND)
sign out of docker
He docker logout
The command logs out of a Docker registry.
Use
docker logout (SERVER)
Now you only know a few essential Docker commands, but Docker comes with many more commands and options that help you manage and work with containers. In the longer examples above, the Docker command-line interface provides a powerful and flexible way to interact with Docker containers and images. When pulling images from a registry, running containers, or managing networks and volumes, these Docker commands optimize your workflow and maximize the potential of container technology.
Also Read: Comprehensive Guide to Docker for Aspiring Data Engineers
Bonus: additional commands
docker images
Lists all Docker images in your local repository.
- Use:
docker images
- Production– Displays the image ID, repository name, tag, and size of each image.
attachable room
Remove one or more Docker containers.
- Use:
docker rm (container_id or container_name)
- Production: Deletes the specified containers.
docker build
Create a Docker image from a Dockerfile.
- Use:
docker build (options) (path)
- Options:
-t repository:tag
to specify the repository and tag of the created image.-f Dockerfile
to specify a Dockerfile other than the default in the build context.
Also Read: Docker Tutorial – Step by Step Tutorial for Beginners
Conclusion
In conclusion, these core Docker commands are designed to help manage containers, images, networks, logs, and other resources like volumes. Once you have learned how to use these commands, you will be able to perform multiple tasks, including running containers, viewing logs, managing images, and working with volumes. Try using these commands in your Docker projects to improve your work and get the most out of the Docker platform.
In the comments section, let us know how useful these Docker commands are for you. We would love to hear from you.