DEV Community

Brijesh Dobariya for CodedThemes

Posted on • Updated on

Top 31+ Docker Commands You Should Learn

Docker Commands

In the dynamic world of software development in 2024, Docker stands as a pivotal tool. Docker, a platform that simplifies the process of building, running, and managing applications in isolated environments known as containers, has revolutionised the way developers work. Its ability to ensure consistency across various environments has made it indispensable.

Understanding Docker Commands and Their Use Cases

Docker commands are the backbone of interacting with Docker. They allow you to manage the lifecycle of Docker containers and images. Understanding these commands and their specific use cases is crucial for efficient Docker management.

Detailed Exploration of Top Docker Commands

1. Docker Version

Use this command to check the currently installed Docker version. It’s essential for ensuring compatibility and leveraging the latest features. Here is an example –

Execution:

docker --version

Enter fullscreen mode Exit fullscreen mode

Output:

Docker version 20.10.7, build f0df350

Enter fullscreen mode Exit fullscreen mode
  • Docker version Indicates that the output is the Docker version information.
  • 20.10.7 Represents the version number of Docker installed on your system.
  • build f0df350 Specifies the unique identifier (hash) for the Docker build.

2. Docker Search

Search for Docker images in the Docker Hub. Useful for finding the right base images for your projects. Here is an example –

Execution:

docker search IMAGE_NAME

Enter fullscreen mode Exit fullscreen mode

Output:

$ docker search nginx
NAME DESCRIPTION
nginx Official build of Nginx.

Enter fullscreen mode Exit fullscreen mode
  • 1. Replace IMAGE_NAME with the actual name or keyword you want to search for.
  • 2. The search was performed for the keyword “nginx.” The output shows details about the official Nginx image available on the Docker Hub.

3. Docker Pull

Downloads an image from Docker Hub to your local machine. It’s the starting point for running containers. Here is an example –

Execution:

docker pull IMAGE_NAME[:TAG]

Enter fullscreen mode Exit fullscreen mode

Output:

$ docker pull nginx

Enter fullscreen mode Exit fullscreen mode
  • Replace IMAGE_NAME with the name of the Docker image and [:TAG] with an optional tag. If you don’t specify a tag, the latest version of the image is pulled by default.
  • This command pulls the official Nginx image from Docker Hub. If you don’t specify a tag, it will pull the latest version.

4. Docker Run

This command creates and starts a container from an image. It’s used to get your applications up and running. Here is an example –

Execution:

docker run OPTIONS IMAGE_NAME[:TAG]

Enter fullscreen mode Exit fullscreen mode

Output:

$ docker run -d -p 8080:80 --name mynginx nginx

Enter fullscreen mode Exit fullscreen mode
  • Replace OPTIONS with any configuration options you need, and replace IMAGE_NAME[:TAG] with the name of the Docker image you want to use.
  • This command creates and runs a detached (-d) Docker container named mynginx, mapping port 8080 on the host to port 80 on the container, based on the official Nginx image.

5. Docker Ps

Lists all running containers. Essential for monitoring and managing active containers. Here is an example –

Execution:

docker ps OPTIONS

Enter fullscreen mode Exit fullscreen mode

Output:

$ docker ps

Enter fullscreen mode Exit fullscreen mode
  • This command displays a list of currently running containers along with their details.

If you are a developer and looking to enhance your admin interface. Here is Django admin Dashboard, which helps to create stunning admin.

Django admin template


6. Docker Stop

Stops a running container. Use this to safely shut down containers without losing data. Here is an example –

Execution:

docker stop CONTAINER_ID or CONTAINER_NAME [...]

Enter fullscreen mode Exit fullscreen mode

Output:

$ docker stop mynginx

Enter fullscreen mode Exit fullscreen mode
  • Replace CONTAINER_ID or CONTAINER_NAME with the ID or name of the container you want to stop.
  • This command stops the Docker container with the name mynginx.

7. Docker Restart

Restarts a container. Ideal for applying updates or configuration changes. Here is an example –

Execution:

docker restart CONTAINER_ID or CONTAINER_NAME [...]

Enter fullscreen mode Exit fullscreen mode

Output:

$ docker restart mynginx

Enter fullscreen mode Exit fullscreen mode
  • Replace CONTAINER_ID or CONTAINER_NAME with the ID or name of the container you want to restart.
  • This command restarts the Docker container with the name mynginx.

8. Docker Kill:

Forcefully stops a container. Necessary when a container doesn’t respond to the standard stop command. Here is an example –

Execution:

docker kill CONTAINER_ID or CONTAINER_NAME [...]

Enter fullscreen mode Exit fullscreen mode

Output:

$ docker kill mynginx

Enter fullscreen mode Exit fullscreen mode
  • Replace CONTAINER_ID or CONTAINER_NAME with the ID or name of the container you want to kill.
  • This command forcefully terminates the Docker container with the name mynginx.

9. Docker Exec

Execute a command in a running container. It’s crucial for debugging and interactive processes. Here is an example –

Execution:

docker exec OPTIONS CONTAINER_ID or CONTAINER_NAME COMMAND [ARG...]

Enter fullscreen mode Exit fullscreen mode

Output:

$ docker exec -it mynginx ls /usr/share/nginx/html

Enter fullscreen mode Exit fullscreen mode
  • Replace OPTIONS, CONTAINER_ID or CONTAINER_NAME, and COMMAND [ARG...] with the appropriate values.
  • This command lists the contents of the /usr/share/nginx/html directory inside the running container named mynginx.

10. Docker Login

Log in to a Docker registry. Essential for pushing and pulling private images. Here is an example –

Execution:

docker login [OPTIONS] REGISTRY

Enter fullscreen mode Exit fullscreen mode

Output:

$ docker login myregistry.example.com

Enter fullscreen mode Exit fullscreen mode
  • Replace [OPTIONS] and REGISTRY with the appropriate values.
  • This command prompts you for your username and password to log in to the Docker registry located at myregistry.example.com.

11. Docker Commit

Create a new image from a container’s changes. Useful for versioning and snapshots. Here is an example –

Execution:

docker commit [OPTIONS] CONTAINER_ID or CONTAINER_NAME REPOSITORY[:TAG]

Enter fullscreen mode Exit fullscreen mode

Output:

$ docker commit -m "Added custom configuration" mycontainer mycustomimage:latest

Enter fullscreen mode Exit fullscreen mode
  • Replace [OPTIONS], CONTAINER_ID or CONTAINER_NAME, and REPOSITORY[:TAG] with the appropriate values.
  • This command creates a new Docker image named mycustomimage with the tag latest based on the changes made to the container with the name mycontainer. The -m option is used to provide a commit message.

12. Docker Push

Uploads your custom image to Docker Hub or another registry. Key for sharing and collaboration. Here is an example –

Execution:

docker push REGISTRY_IMAGE

Enter fullscreen mode Exit fullscreen mode

Output:

$ docker push registry.example.com/myimage:tag

Enter fullscreen mode Exit fullscreen mode
  • Replace REGISTRY_IMAGE with the name and tag of the image you want to push.
  • This command pushes the local Docker image with the name myimage and tag tag to the Docker registry located at registry.example.com.

13. Docker Network

Manage Docker’s network settings. Crucial for setting up communication between containers. Here is an example –

Create a New Bridge Network:

Example:

docker network create mynetwork

Enter fullscreen mode Exit fullscreen mode

This command creates a new bridge network named mynetwork.

14. Docker History

Shows the history of an image, including its layers. Useful for understanding build processes and optimizations. Here is an example –

Execution:

docker history IMAGE_NAME[:TAG]

docker history ubuntu:latest

Enter fullscreen mode Exit fullscreen mode

Output:

IMAGE CREATED BY CREATED SIZE
sha256:abcd1234… /bin/sh -c #(nop) CMD ["/bin/bash"] 2 weeks ago 73.9MB
sha256:efgh5678… /bin/sh -c #(nop) LABEL org.label-schema… 2 weeks ago 0B
sha256:ijkl9012… /bin/sh -c #(nop) ADD file:a0b1c2d3e4f5g6… 2 weeks ago 72.9MB

Enter fullscreen mode Exit fullscreen mode
  • Replace IMAGE_NAME[:TAG] with the name and optional tag of the Docker image you want to inspect.
  • This command displays the history of the ubuntu image with the latest tag.

15. Docker Rmi

Removes images. Helps in maintaining a clean local development environment. Here is an example –

Command Structure:

docker rmi [OPTIONS] IMAGE [IMAGE...]

Enter fullscreen mode Exit fullscreen mode

Example:

docker rmi myimage:tag

Enter fullscreen mode Exit fullscreen mode
  • IMAGE: Replace this with the name or ID of the Docker image you want to remove.
  • [OPTIONS]: Various options can be specified to customize the removal process.
  • This command removes the Docker image named myimage with the specified tag.

16. Docker Ps -a

Lists all containers, both running and stopped. Essential for complete container management. Here is an example –

Command Structure:

docker ps -a [OPTIONS]

Enter fullscreen mode Exit fullscreen mode

Example:

docker ps -a

Enter fullscreen mode Exit fullscreen mode
  • [OPTIONS]: Various options can be specified to customize the output or filter the list of containers.
  • This command lists all containers, both running and stopped, along with their details.

17. Docker Copy (Cp)

Copy files/folders between a container and the local file system. Useful for data migration and backup. Here is an example –

Command Structure:

docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH

Enter fullscreen mode Exit fullscreen mode

Examples:

docker cp /path/to/local/file.txt mycontainer:/path/in/container/

Enter fullscreen mode Exit fullscreen mode
  • SRC_PATH: The source path on the local filesystem or inside the container.
  • CONTAINER: The name or ID of the container.
  • DEST_PATH: The destination path inside the container or on the local filesystem.

18. Docker Logs:

Displays logs of a container. Key for troubleshooting and monitoring. Here is an example –

Command Structure:

docker logs [OPTIONS] CONTAINER

Enter fullscreen mode Exit fullscreen mode

Examples:

docker logs mycontainer

Enter fullscreen mode Exit fullscreen mode
  • CONTAINER: Replace this with the name or ID of the container whose logs you want to view.
  • This command displays the logs of the running container named mycontainer.

19. Docker Volume

Manages persistent storage for containers. Essential for data persistence beyond the container lifecycle.

Creating a Docker Volume:

Command Structure:

docker volume create [OPTIONS] [VOLUME]

Enter fullscreen mode Exit fullscreen mode

Example:

docker volume create myvolume

Enter fullscreen mode Exit fullscreen mode
  • This command creates a Docker volume named myvolume.

20. Docker Logout

Logs out from a Docker registry. Important for security and switching between different registries. Here is an example –

Command Structure:

docker logout [REGISTRY]

Enter fullscreen mode Exit fullscreen mode

Example:

docker logout

Enter fullscreen mode Exit fullscreen mode
  • REGISTRY: (Optional) The URL of the Docker registry from which to log out. If not specified, it logs out from the default registry (Docker Hub).
  • This command logs out from the default Docker registry (Docker Hub).

21. Docker System

Offers a wide range of system-level information. Useful for diagnostics and system optimization. Here is an example –

Displaying Docker System Information:

Command Structure:

docker system info

Enter fullscreen mode Exit fullscreen mode

Example:

docker system info

Enter fullscreen mode Exit fullscreen mode
  • This command displays detailed information about the Docker system, including resource usage, storage driver, and other relevant details.

22. Docker Context

Manages the context for Docker commands. Helpful for working with multiple Docker environments. Here is an example –

Displaying Available Docker Contexts:

Example:

docker context ls

Enter fullscreen mode Exit fullscreen mode
  • This command lists the available Docker contexts along with their details, such as the current context, type, and name.

23. Docker Pause/Unpause

Temporarily suspends a container. Useful for resource management. Here is an example –

Pause a Docker Container:

Command Structure:

docker pause CONTAINER

Enter fullscreen mode Exit fullscreen mode

Example:

docker pause mycontainer

Enter fullscreen mode Exit fullscreen mode
  • This command pauses the processes inside the running container named mycontainer.

Unpause a Docker Container:

Command Structure:

docker unpause CONTAINER

Enter fullscreen mode Exit fullscreen mode

Example:

docker unpause mycontainer

Enter fullscreen mode Exit fullscreen mode

This command resumes the processes inside the paused container named mycontainer.

24. Docker Plugin Install

Adds new functionalities to Docker. Enhances Docker capabilities. Here is an example –

Command Structure:

docker plugin install PLUGIN [KEY=VALUE...]

Enter fullscreen mode Exit fullscreen mode

Example:

docker plugin install --grant-all-permissions vieux/sshfs

Enter fullscreen mode Exit fullscreen mode
  • This command installs the vieux/sshfs Docker plugin. The --grant-all-permissions flag is optional and is used to automatically grant all required permissions to the plugin.

25. Docker Image

Manages Docker images. Central to Docker usage for building, listing, and removing images. Here is an example –

Pulling a Docker Image:

Command Structure:

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Enter fullscreen mode Exit fullscreen mode

Example:

docker pull nginx:latest

Enter fullscreen mode Exit fullscreen mode
  • This command pulls the nginx image from Docker Hub with the latest tag.

26. Docker Import

Creates an image from a tarball. Useful for importing pre-built environments. Here is an example –

Importing a Docker Image:

Command Structure:

docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Enter fullscreen mode Exit fullscreen mode

Example:

docker import myarchive.tar.gz myrepo/myimage:mytag

Enter fullscreen mode Exit fullscreen mode
  • This command creates a Docker image named myrepo/myimage with the tag mytag from the contents of the myarchive.tar.gz file.

27. Docker Node

Manages Swarm nodes. Important for Docker cluster management. Here is an example –

Initialize a Swarm on a Manager Node:

docker swarm init --advertise-addr <MANAGER-IP>

Enter fullscreen mode Exit fullscreen mode
  • This command initializes a new Docker Swarm on the manager node and provides a token for worker nodes to join.
  • Take your admin panel experience to the next level with Django Templates

28. Docker Save

Saves an image to a tar archive. Useful for image distribution and backups. Here is an example –

Saving Docker Images:

**Command Structure:**

docker save [OPTIONS] IMAGE [IMAGE...]

Enter fullscreen mode Exit fullscreen mode

Example:

docker save -o my_images.tar.gz ubuntu:latest alpine:3.14

Enter fullscreen mode Exit fullscreen mode
  • This command saves the ubuntu:latest and alpine:3.14 images to a tarball archive named my_images.tar.gz.

29. Docker Tag

Tags an image to a repository. Crucial for image versioning and organization. Here is an example –

Tagging a Docker Image:

Command Structure:

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Enter fullscreen mode Exit fullscreen mode

Example:

docker tag myimage:latest myrepository/myimage:v1.0

Enter fullscreen mode Exit fullscreen mode
  • This command tags the local image myimage:latest with the new name myrepository/myimage:v1.0.

30. Docker Login (Detailed)

In-depth usage of the login command for various registries. Here is an example –

Logging into a Docker Registry:

Command Structure:

docker login [OPTIONS] [SERVER]

Enter fullscreen mode Exit fullscreen mode

Example:

docker login myregistry.example.com

Enter fullscreen mode Exit fullscreen mode
  • This command prompts you to enter your username and password to log in to the Docker registry located at myregistry.example.com.

31. Docker Create

Creates a container without starting it. Useful for setting up containers in advance. Here is an example –

Creating a Docker Container:

Command Structure:

docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

Enter fullscreen mode Exit fullscreen mode

Example:

docker create --name mycontainer -p 8080:80 nginx:latest

Enter fullscreen mode Exit fullscreen mode
  • This command creates a new container named mycontainer based on the nginx:latest image. It maps port 8080 on the host to port 80 on the container.

32. Docker Checkpoint

Manages checkpoints in container development. Useful for testing and rollbacks. Here is an example –

Checkpoint the Container:

criu dump -t $(docker inspect --format '{{.State.Pid}}' mycontainer) --leave-stopped -v4 -o dump.log

Enter fullscreen mode Exit fullscreen mode
  • This command uses CRIU to create a checkpoint of the specified process (container) and stores the checkpoint data in a directory.

33. Docker Cp (Detailed)

Detailed usage of copying files/folders to and from containers. Here is an example –

Copying Files Between a Docker Container and the Local Filesystem:

Command Structure:

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- # Copy from container to local
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH # Copy from local to container

Enter fullscreen mode Exit fullscreen mode

Examples:

Copy from a container to the local filesystem:

docker cp mycontainer:/path/in/container /path/on/host

Enter fullscreen mode Exit fullscreen mode

Copy from the local filesystem to a container:

docker cp /path/on/host mycontainer:/path/in/container

Enter fullscreen mode Exit fullscreen mode

Conclusion

Mastering these top 31+ Docker commands is vital for anyone working in software development, DevOps, or related fields. Understanding each command’s specific use case enhances your ability to effectively manage Docker containers and images, streamlining your development and deployment processes.

Top comments (0)