20+ Docker Commands for Building, Running, and Managing Containers

Introduction

Docker is an open-source platform that gives developers everything they need to create, package, and deploy applications in a streamlined way. With Docker’s container technology, you can bundle your applications and all their dependencies into a single, self-contained unit that can easily be moved across different platforms and run seamlessly in containers. However, to make the most of what Docker offers, you must get comfortable with its command-line interface (CLI). In this article, we’ll walk you through the must-know Docker commands every developer and system administrator should have in their toolkit.

Docker Commands

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. In addition, it enables the ability to run tasks such as listing live containers, checking container status, 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 utilizing Docker in containerizing applications, achieving portability and making it easy to deploy it across various platforms.

How to Use Docker Commands?

Here are some common ways to use Docker commands:

  1. Run a containerdocker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...] This command creates and starts a new container from the specified image.
  2. List running containersdocker ps Lists all currently running containers.
  3. Stop a containerdocker stop CONTAINER_ID Stops the running container specified by its ID or name.
  4. Remove a containerdocker rm CONTAINER_ID Removes the stopped container from the system.
  5. Pull an imagedocker pull IMAGE[:TAG|@DIGEST] Downloads the specified image from a registry (e.g., Docker Hub).
  6. Build an imagedocker build [OPTIONS] PATH | URL | - Builds a new image from the instructions in a Dockerfile.
  7. List imagesdocker images Lists all available images on the local system.
  8. Remove an imagedocker rmi IMAGE[:TAG|@DIGEST] Removes the specified image from the local system.
  9. Run a command in a containerdocker exec [OPTIONS] CONTAINER_ID COMMAND [ARG...] Runs the specified command within a running container.
  10. View logsdocker logs CONTAINER_ID Fetches the logs of the specified container.

These are just a few 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 Top Docker Commands

Docker Version

The docker version command displays the current version of Docker installed on your system. It provides information about the Docker client and server versions as well as various other details such as the operating system, architecture, and kernel version.

Usage

docker version

docker search 

The docker search command allows you to search for Docker images on Docker Hub, the official registry for Docker images. You can search for images by name or use keywords to find relevant images.

Usage

docker search <image_name>

docker pull 

The docker pull command downloads a Docker image from a registry (such as Docker Hub) to your local machine. You need to pull an image from it before creating a container.

Usage

docker pull <image_name>:<tag>

docker run

The docker run command is one of the most frequently used Docker commands. It creates a new container from a specified image and starts it. You can pass various options to customize the container’s behavior, such as exposing ports, mounting volumes, and setting environment variables.

Usage

docker run [OPTIONS] <image_name>:<tag> [COMMAND] [ARG...]

docker ps 

The docker ps command lists all the currently running containers on your system. By default, it shows only the running containers, but you can use the -a flag to list all containers (running and stopped).

Usage

docker ps
docker ps -a

docker stop

The docker stop command stops one or more running containers. You can specify the container by its name or ID.

Usage

docker stop <container_name_or_id>

docker restart 

The docker restart command restarts one or more running containers. It first stops the container(s) and then starts them again.

Usage

docker restart <container_name_or_id>

docker kill

The docker kill command forcibly stops a running container by sending a KILL signal. It should be used when the docker stop command fails to stop a container gracefully.

Usage

docker kill <container_name_or_id>

docker exec 

The docker exec command runs a new command inside a running container. This is useful for inspecting or troubleshooting containers without starting a new shell.

Usage

docker exec [OPTIONS] <container_name_or_id> [COMMAND] [ARG...]

docker login 

The docker login command authenticates you with a Docker registry, such as Docker Hub. You need to be authenticated to push images to a registry.

Usage

docker login [OPTIONS] [SERVER]

docker commit

The docker commit 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.

Usage

docker commit [OPTIONS] <container_name_or_id> [REPOSITORY[:TAG]]

docker push

The docker push command uploads an image to a Docker registry, such as Docker Hub. You need to be authenticated with the registry before pushing an image.

Usage

docker push <image_name>:<tag>

docker network

The docker network command manages Docker networks. It allows you to create, inspect, and manage networks for communication between containers.

Usage

docker network [COMMAND] [ARG...]

docker history

The docker history command shows the history of an image, including the layers that make up the image and the commands used to create each layer.

Usage

docker history <image_name>:<tag>

docker rmi

The docker rmi command removes one or more images from your local system. You need to stop and remove all containers based on the image before removing the image itself.

Usage

docker rmi <image_name>:<tag>

docker ps -a

The docker ps -a command lists all containers (running and stopped) on your system. It’s a useful command for getting an overview of all the containers on your machine.

Usage

docker ps -a

docker copy

The docker copy command copies files or directories between a container and the local filesystem.

Usage

docker copy [OPTIONS] <container_name_or_id>:<src_path> <dest_path>
docker copy [OPTIONS] <src_path> <container_name_or_id>:<dest_path>

docker logs

The docker logs command retrieves log output from a container. It’s an essential command for troubleshooting and debugging containers.

Usage

docker logs [OPTIONS] <container_name_or_id>

docker volume

The docker volume command manages Docker volumes. Volumes are used to persist data generated by Docker containers.

Usage

docker volume [COMMAND]

docker logout

The docker logout command logs out from a Docker registry.

Usage

docker logout [SERVER]

Now, you know just 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 offers a powerful and flexible method 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: End-to-End Guide to Docker for aspiring Data Engineers

Bonus: Additional Commands

docker images

Lists all Docker images in your local repository.

  • Usage: docker images
  • Output: Displays image ID, repository name, tag, and size of each image.

docker rm

Removes one or more Docker containers.

  • Usage: docker rm [container_id or container_name]
  • Output: Deletes the specified container(s).

docker build

Builds a Docker image from a Dockerfile.

  • Usage: docker build [options] [path]
  • Options:
    • -t repository:tag to specify the repository and tag for the built image.
    • -f Dockerfile to specify a Dockerfile other than the default one in the build context.

Also read: Docker Tutorial: Step-by-Step Tutorial for Beginners

Conclusion

In conclusion, these top Docker commands are designed to help manage containers, images, networks, logs, and other resources such as volumes. Once you have learned how to use these commands, you can accomplish multiple tasks, including running containers, watching 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 comment section, please let us know how useful these Docker commands are for you. We would love to hear from you.

Source link

Picture of quantumailabs.net
quantumailabs.net

Leave a Reply

Your email address will not be published. Required fields are marked *