Top 20 Essential Docker Commands You Should Know in 2024 (2024)

Table of contents

      • Introduction
      • What is Docker?
      • Top 20 Docker Commands

Introduction

Are you new to Docker and wondering how to manage Docker containers effectively? Don’t worry, Docker commands can help you achieve that! Docker is a popular platform that allows developers to package their applications, dependencies, and configurations into containers that can run seamlessly across different environments.

Using Docker commands, you can create, run, stop, remove, and manage Docker containers easily. These commands can help you automate and streamline the process of deploying and managing your applications in a containerized environment.

Let’s explore some of the most commonly used Docker commands that can help you manage Docker containers effectively.

  1. docker run – This command is used to start a new Docker container from an image.
  2. docker ps – This command is used to list all the running Docker containers.
  3. docker stop – This command is used to stop a running container.
  4. docker rm – This command is used to remove a Docker container.
  5. docker images – This command is used to list all the Docker images that are currently available on your system.
  6. docker pull – This command is used to download a Docker image from a registry.
  7. docker exec – This command is used to execute a command in a running container.
  8. docker-compose – This command is used to manage multi-container Docker applications.

Using these commands, you can easily manage Docker containers, images, and applications. Whether you are a beginner or an experienced Docker user, mastering these commands can help you take your Docker skills to the next level.

Check out these courses and learn Docker for free.

What is Docker?

Docker is a free and open platform that offers an immersive experience to developers working on various aspects of software development. It is a containerization platform that allows developers to package code into various deployable units called containers. Docker Engine is the software that oversees the hosting of the containers. The subset of the Moby Project, Docker, helps developers build, run, and deploy containers on the server and the cloud. The container contains a builder, engine, and orchestrator to deliver a seamless application that runs in any environment.

Docker is preferred by developers working on individual projects as it offers a lightweight environment for testing. It comes with both free and premium tiers. Docker was initially released in 2013 and is written in Go.

Also Read: Know the difference between Docker vs Kubernetes

Top 20 Docker Commands

We have enlisted 20 docker commands to help you navigate through the docker engine seamlessly and get most of your work done. Let us get started.

  • Docker version
  • Docker search
  • Docker pull
  • Docker run
  • Docker ps
  • Docker stop
  • Docker restart
  • Docker kill
  • Docker exec
  • Docker login
  • Docker commit
  • Docker push
  • Docker network
  • Docker history
  • Docker rmi
  • Docker ps -a
  • Docker copy
  • Docker logs
  • Docker volume
  • Docker logout

Learn more with these free courses

Roadmap to becoming a Full Stack Developer Docker for Intermediate Level Monitoring in Docker

  1. Docker version

We usually start by finding the installed version of docker that we are working on. Here is how to find it –

greatlearning@greatlearning:/home/greatlearning$ docker --versionDocker version 18.09.6, build 481bc77
  1. Docker search

The “docker search” command searches for specific images through the Docker hub. This command returns the specific information, including image name, description, automated, official stars, etc. Here is how to use it –

docker search MySQL

You can use the Docker Hub website to search through the GUI-based option.

  1. Docker pull

As the name suggests, this command pulls a specific image from the Docker Hub. All you have to do is use the command ‘docker pull’ along with the name of the image. Here is an example of pulling an image without using the tag.

docker pull --platform Linux/x86_64 MySQLHere is an example that demonstrates how to pull an Apache HTTP server image from the Docker repository using the tag -Using default tag: latestlatest: Pulling from library/httpdf5d23c7fed46: Pull completeb083c5fd185b: Pull completebf5100a89e78: Pull complete98f47fcaa52f: Pull complete622a9dd8cfed: Pull completeDigest: sha256:8bd76c050761610773b484e411612a31f299dbf7273763103edbda82acd73642Status: Downloaded newer image for httpd: latestgreatlearning@greatlearning:/home/greatlearning$

The tags are used to identify the images inside the Docker hub. If you do not specify a tag, it will use the ‘: latest’ tag by default.

We can use the command ‘-all-tags’ to pull all the images from the repository for multiple images.

  1. Docker run

This command is used to create a container from an image. Here is how to do it –

docker run --env MYSQL_ROOT_PASSWORD=my-secret-pw --detach mysql

The ‘–detach’ option runs the container, and the ‘–env’ option is used to set the mandatory variable. If you do not use the ‘–name’ option, the docker will randomly assign a name to the container.

  1. Docker ps

This command is used to list all the running containers in the background. Here is how to do it –

docker ps --all
  1. Docker stop

The ‘docker stop’ command stops a container using the container name or its id. Here is how to do it –

docker stop f8c52bedeecc
  1. Docker restart

This command is used to restart the stopped container. It is recommended to use this after rebooting the system. Here is how to do it –

greatlearning@greatlearning:/home/greatlearning$docker restart f8c52bedeecc
  1. Docker kill

This command is used to stop the container immediately by killing its execution. While the ‘docker stop’ command helps shut down the container in its own time, the ‘docker kill’ command stops it at once. Here is to use it –

greatlearning@greatlearning:/home/greatlearning$docker kill 09ca6feb6efcTo check whether the container is stopped or killed, use the following command -greatlearning@greatlearning:/home/greatlearning$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  1. Docker exec

This command is used to access the container that is running. Here is how to use it –

docker exec -it test_db bashmysql -uroot -pmy-secret-pwSHOW DATABASES;

You have to either provide the name or the id of the container, which is ‘test_db’ in this case. The ‘-i’ and ‘-t’ options are used to access the interactive mode.

  1. Docker login

This command helps you to log into your docker hub. As you try to log in, you will be asked to give your docker hub credentials.

greatlearning@greatlearning:/home/greatlearning$ docker loginLogin with your Docker ID to push and pull images from Docker Hub. Suppose you don’t have a Docker ID, head over to https://hub.docker.com to create one.Username: greatlearningPassword:Configure a credential helper to remove this warning. See: https://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded
  1. Docker commit

This command is used to create or save an image of the edited container on the local system.

Here is how to use it –

greatlearning@greatlearning:/home/greatlearning$docker commit 09ca6feb6efc greatlearning/httpd_imagesha256:d1933506f4c1686ab1a1ec601b1a03a17b41decbc21d8acd893db090a09bb31cHere, ‘greatlearning’ is the username and ‘httpd_image’ is the image name of the edited container. 
  1. Docker push

This command helps push or upload a docker image on the repository or the docker hub. Here is how to use it –

greatlearning@greatlearning:/home/greatlearning$ docker push greatlearning/httpd_imageThe push refers to the repository [docker.io/greatlearning/httpd_image]734d9104a6a2: Pushed635721fc6973: Mounted from library/httpdbea448567d6c: Mounted from library/httpdbfaa5f9c3b51: Mounted from library/httpd9d542ac296cc: Mounted from library/httpdd8a33133e477: Mounted from library/httpdlatest: digest: sha256:3904662761df9d76ef04ddfa5cfab764b85e3eedaf10071cfbe2bf77254679ac size: 1574
  1. Docker network

The ‘docker network’ command is used to know the details of the list of networks in the cluster. Here is how to use it –

greatlearning@greatlearning:/home/greatlearning$ docker network lsNETWORK ID NAME DRIVER SCOPE85083e755f04 bridge bridge localf51d1f2379e0 host host local5e5d9a154c00 none null localApart from this, you can also use the following docker network commands to manage networks -Connect - to connect a container to a networkDisconnect - to disconnect a container from a networkCreate - to create a networkLs - to list all networksInspect - to display detailed information on one or more networksprune - to remove all unused networksrm - to remove one or more networksDocker history This command is used to check the history of a docker image. The image name has to be mentioned while using the command. Here is how to use it -greatlearning@greatlearning:/home/greatlearning$ docker history httpdIMAGE CREATED CREATED BY SIZE COMMENTee39f68eb241 3 days ago /bin/sh -c #(nop) CMD ["httpd-foreground"] 0B<missing> 3 days ago /bin/sh -c #(nop) EXPOSE 80 0B<missing> 3 days ago /bin/sh -c #(nop) COPY file:c432ff61c4993ecd… 138B<missing> 2 days ago /bin/sh -c set -eux; savedAptMark="$(apt-m… 49.1MB<missing> 2 days ago /bin/sh -c #(nop) ENV HTTPD_PATCHES= 0B<missing> 2 days ago /bin/sh -c #(nop) ENV HTTPD_SHA256=b4ca9d05… 0B<missing> 2 days ago /bin/sh -c #(nop) ENV HTTPD_VERSION=2.4.39 0B<missing> 2 days ago /bin/sh -c set -eux; apt-get update; apt-g… 35.4MB<missing> 5 days ago /bin/sh -c #(nop) WORKDIR /usr/local/apache2 0B<missing> 5 days ago /bin/sh -c mkdir -p "$HTTPD_PREFIX" && chow… 0B<missing> 5 days ago /bin/sh -c #(nop) ENV PATH=/usr/local/apach… 0B<missing> 5 days ago /bin/sh -c #(nop) ENV HTTPD_PREFIX=/usr/loc… 0B<missing> 4 days ago /bin/sh -c #(nop) CMD ["bash"] 0B<missing> 4 days ago /bin/sh -c #(nop) ADD file:71ac26257198ecf6a… 69.2MB
  1. Docker rmi

This command is used to free up some disk space. The image id is used to remove the image while using this command.

docker rmi eb0e825dc3cf
  1. Docker ps -a

This command is used to know the details of all the running, stopped, or exited containers. Here is how to use it –

greatlearning@greatlearning:/home/greatlearning$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES09ca6feb6efc httpd "httpd-foreground" 51 seconds ago Up 49 seconds 80/tcp sane_trell2f6fb3381078 sequenceiq/hadoop-docker:2.7.0 "/etc/bootstrap.sh -d" 2 weeks ago Exited (137) 9 days ago better_naman9f397feb3a46 sequenceiq/hadoop-docker:2.7.0 "/etc/bootstrap.sh -…" 2 weeks ago Exited (255) 2 weeks ago 2122/tcp, 8030-8033/tcp, 8040/tcp, 8042/tcp, 8088/tcp, 19888/tcp, 49707/tcp, 50010/tcp, 50020/tcp, 50070/tcp, 50075/tcp, 50090/tcp determined_sarah9b6343d3b5a0 hello-world "/hello" 2 weeks ago Exited (0) 2 weeks ago mega_steven
  1. Docker copy

This command copies a file from docker to the local system. Here is how to use it –

greatlearning@greatlearning:/home/greatlearning$ sudo docker cp 09ca4feb7tfc:/usr/local/apache2/logs/httpd.pid /home/greatlearning/[sudo] password for greatlearning:In the above example, we used the docker container with id 09ca4feb7tfc to copy the file ‘http.pid.’ To check whether the file is copied or not, run this command -greatlearning@greatlearning:/home/greatlearning$ lsDesktop Documents example examples.desktop httpd.pid nginx_new.yml nginx.yml
  1. Docker logs

This command is used to check the logs of all the docker containers with the corresponding contained id mentioned in the command. Here is how to use it –

greatlearning@greatlearning:/home/greatlearning$ docker logs 09ca6feb6efcAH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName’ directive globally to suppress this messageAH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName’ directive globally to suppress this message[Mon Jul 16 14:01:55.400472 2019] [mpm_event:notice] [pid 1:tid 140299791516800] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations[Mon Jul 16 14:01:55.400615 2019] [core:notice] [pid 1:tid 140299791516800] AH00094: Command line: 'httpd -D FOREGROUND'[Mon Jul 16 14:08:36.798229 2019] [mpm_event:notice] [pid 1:tid 140299791516800] AH00491: caught SIGTERM, shutting downAH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName’ directive globally to suppress this messageAH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName’ directive globally to suppress this message[Mon Jul 15 14:08:38.259870 2019] [mpm_event:notice] [pid 1:tid 139974087980160] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations[Mon Jul 15 14:08:38.260007 2019] [core:notice] [pid 1:tid 139974087980160] AH00094: Command line: 'httpd -D FOREGROUND'[Mon Jul 15 14:09:01.540647 2019] [mpm_event:notice] [pid 1:tid 139974087980160] AH00491: caught SIGTERM, shutting downAH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName’ directive globally to suppress this messageAH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName’ directive globally to suppress this message[Mon Jul 16 14:10:43.782606 2019] [mpm_event:notice] [pid 1:tid 140281554879616] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations[Mon Jul 16 14:10:43.782737 2019] [core:notice] [pid 1:tid 140281554879616] AH00094: Command line: 'httpd -D FOREGROUND'AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName’ directive globally to suppress this messageAH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using 172.17.0.2. Set the ‘ServerName’ directive globally to suppress this message[Mon Jul 16 14:14:08.270906 2019] [mpm_event:notice] [pid 1:tid 140595254346880] AH00489: Apache/2.4.39 (Unix) configured -- resuming normal operations[Mon Jul 16 14:14:08.272628 2019] [core:notice] [pid 1:tid 140595254346880] AH00094: Command line: 'httpd -D FOREGROUND'
  1. Docker volume

This command creates a volume so that the docker container can use it to store data. Here is how to use it –

greatlearning@greatlearning:/home/greatlearning$ docker volume create7e7bc886f69bb24dbdbf19402e31102a25db91bb29c56cca3ea8b0c611fd9ad0To check whether this command created the volume or not, run the following command -greatlearning@greatlearning:/home/greatlearning$ docker volume lsDRIVER VOLUME NAMElocal 7e7bc886f69bb24dbdbf19402e31102a25db91bb29c56cca3ea8b0c611fd9ad0
  1. Docker logout

This command will log you out of the docker hub. Here is how to use it –

greatlearning@greatlearning:/home/greatlearning$ docker logoutRemoving login credentials for https://index.docker.io/v1/

Conclusion

You can try out the docker commands in your lab environment to practice them for source development. We hope these 20 docker commands will help you get started with docker and learn to perform basic functions without getting overwhelmed.Get your hands on the free Docker for Windows and brush up on your Docker skills.

Top 20 Essential Docker Commands You Should Know in 2024 (2024)
Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 6015

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.