Top 10 Basic Docker Commands You Must Learn (2024)

DevOps is a natural evolution of software development. DevOps is not just a tool, a framework, or just automation. It is a combination of all these. DevOps aimed to align the Dev and Ops team with shared goals.

A developer builds an application and sends it to the tester. But, the environments of development and testing systems are different; thus, the code does not work. There are two solutions to this: Docker and Virtual Machines, but Docker is far better in terms of performance, scaling, and efficiency.

Docker is a platform that has eased application development for both developers and system managers. Dockers have been used widely in many DevOps toolchains. Docker provides flexibility for the operational team thereby reducing the risk overhead. Dockers platform provides numerous features which make them popular among developers. Some features include application isolation, portable, security management, Ease of software delivery, scalability, etc. Let's discuss the basic docker commands in more detail along with examples! You can refer to the best Docker courses for training related to docker and docker commands.

What is Docker?

Docker is a platform that enables creating, deploying, and running applications with the help of containers. A Container is a unit of software that packages the code and all its dependencies together such that the application becomes runnable irrespective of the environment. The container isolates the application and its dependencies into a self-contained unit that can run anywhere. Container removes the need for physical hardware allowing for more efficient use of computing resources. Containers provide operating-system-level virtualization. Moreover, developers can collaborate faster without worrying about which software dependency they need to install.

A container is a runnable instance of an image. An image is a read-only template with instructions for creating a Docker container. You can create, start, stop, move, or delete a container using the Docker API or CLI. One can connect a single container to one or more networks and can also attach storage to it. A new image can also be created based on the current state of the container. Containers can be shared thereby ensuring that everyone who communicates with it will get the same container and that works in the same way. Here is the list of 50+ basic docker commands:

List of 57 Essential Docker Commands

Here are the top 57 essential/ basic docker commands with descriptions to learn and use.

CommandUsage
docker attachAttach local standard input, output, and error streams to a running container
docker buildBuild an image from a Dockerfile
docker builderManage builds
docker checkpointManage checkpoints
docker commitCreate a new image from a container’s changes
docker configManage Docker configs
docker containerManage containers
docker contextManage contexts
docker cpCopy files/folders between a container and the local filesystem
docker createCreate a new container
docker diffInspect changes to files or directories on a container’s filesystem
docker eventsGet real time events from the server
docker execRun a command in a running container
docker exportExport a container’s filesystem as a tar archive
docker historyShow the history of an image
docker imageManage images
docker imagesList images
docker importImport the contents from a tarball to create a filesystem image
docker infoDisplay system-wide information
docker inspectReturn low-level information on Docker objects
docker killKill one or more running containers
docker loadLoad an image from a tar archive or STDIN
docker loginLog in to a Docker registry
docker logoutLog out from a Docker registry
docker logsFetch the logs of a container
docker manifestManage Docker image manifests and manifest lists
docker networkManage networks
docker nodeManage Swarm nodes
docker pausePause all processes within one or more containers
docker pluginManage plugins
docker portList port mappings or a specific mapping for the container
docker psList containers
docker pullPull an image or a repository from a registry
docker pushPush an image or a repository to a registry
docker renameRename a container
docker restartRestart one or more containers
docker rmRemove one or more containers
docker rmiRemove one or more images
docker runRun a command in a new container
docker saveSave one or more images to a tar archive (streamed to STDOUT by default)
docker searchSearch the Docker Hub for images
docker secretManage Docker secrets
docker serviceManage services
docker stackManage Docker stacks
docker startStart one or more stopped containers
docker statsDisplay a live stream of container(s) resource usage statistics
docker stopStop one or more running containers
docker swarmManage Swarm
docker systemManage Docker
docker tagCreate a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker topDisplay the running processes of a container
docker trustManage trust on Docker images
docker unpauseUnpause all processes within one or more containers
docker updateUpdate configuration of one or more containers
docker versionShow the Docker version information
docker volumeManage volumes
docker waitBlock until one or more containers stop, then print their exit codes

Let's understand a few of the above commands along with their usage in detail. The following are the most used docker basic commands for beginners and experienced docker professionals.

1. docker –version

This command is used to get the current version of the docker

Syntax

docker - -version [OPTIONS] 

By default, this will render all version information in an easy-to-read layout.

2. docker pull

Pull an image or a repository from a registry

Syntax

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

To download an image or set of images (i.e. A Repository) , Once can use docker pull command

Example:

$ docker pull dockerimage 

3. docker run

This command is used to create a container from an image

Syntax

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

The docker run command creates a writeable container layer over the specified image and then starts it using the specified command.

The docker run command can be used with many variations, One can refer to the following documentation docker run.

4. docker ps

This command is used to list all the containers

Syntax

docker ps [OPTIONS]

The above command can be used with other options like - all or –a

docker ps -all: Lists all containers

Example:

$ docker ps 
$ docker ps -a

5. docker exec

This command is used to run a command in a running container

Syntax

docker exec [OPTIONS] CONTAINER COMMAND [ARG...] 

Docker exec command runs a new command in a running container.

Refer to the following article for more detail regarding the usage of the docker exec command docker exec.

6. docker stop

This command is used to stop one or more running containers.

Syntax:

docker stop [OPTIONS] CONTAINER [CONTAINER...] 

The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL. The first signal can be changed with the STOPSIGNAL instruction in the container’s Dockerfile, or the --stop-signal option to docker run.

Example:

$ docker stop my_container 

7. docker restart

This command is used to restart one or more containers.

Syntax: docker restart [OPTIONS] CONTAINER [CONTAINER...]

Example:

$ docker restart my_container 

8. docker kill

This command is used to kill one or more containers.

Syntax: docker kill [OPTIONS] CONTAINER [CONTAINER...]

Example:

$ docker kill my_container 

9. docker commit

This command is used to create a new image from the container image.

Syntax: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Docker commit command allows users to take an existing running container and save its current state as an image

There are certain steps to be followed before running the command

  • First , Pull the image from docker hub
  • Deploy the container using the image id from first step
  • Modify the container (Any changes ,if needed)
  • Commit the changes

Example:

$ docker commit c3f279d17e0a dev/testimage:version3.

10. docker push

This command is used to push an image or repository to a registry.

Syntax: docker push [OPTIONS] NAME[: TAG]

Use docker image push to share your images to the Docker Hub registry or to a self-hosted one.

Example:

$ docker image push registry-host:5000/myadmin/rhel-httpd:lates 

Apart from the above commands, we have other commands for which the detailing can be found in the following link Docker reference.

One can become DevOps certified by referring to DevOps Certification courses.

Docker Use Cases

Let's understand a few of the docker use cases:

Use case 1: Developers write their code locally and can share it using docker containers.

Use case 2: Fixing the bugs and deploying them into the respective environments is as simple as pushing the image to the respective environment.

Use case 3: Using docker one can push their application to the test environment and execute automated and manual tests

Use case 4: One can make their deployment responsive and scalable by using docker since docker can handle dynamic workloads feasibility.

Let us take an example of an application,

When a company wants to develop a web application they need an environment where they have a tomcat server installed. Once the tester set up a tomcat environment and test the application, it is deployed into a production environment. Once again the tomcat has to be setup in production environment to host the java web application There are some issues with this approach:

  • Loss of time and effort.
  • Developer and tester might use a different tomcat versions.

Now, let's see how the Docker container can be used to prevent this loss.

In order to overcome the issues, docker will be used by a developer to create a docker image using a base image which is already existing in Docker hub. Docker hub has some base images available for free. Now this image can be used by developer, tester and the system admin to deploy a tomcat environment. In this way, Docker container solves the problem.

Docker Architecture

Docker architecture generally consists of a Docker Engine which is a client-server application with three major components:

  1. Generally, docker follows a client-server architecture
  2. The client communicates with the daemon, which generally takes up the task of building,running, and shipping the docker containers.
  3. The client and daemon communicate using REST API calls. These calls act as an interface between the client and daemon
  4. A command-line interface, Docker CLI runs docker commands. Some basic docker commands with examples are listed in the next section.
  5. Registry stores the docker images

Top 10 Basic Docker Commands You Must Learn (1)

Conclusion

DevOps (development + operations) is an evolution born between developers and system administrators. One of the main tasks of DevOps is the automation and centralization of software development and deployment. One of the most popular tools that help in solving this task is Docker. To get a better understanding of docker enroll in Docker Kubernetes training.

Three main features of Docker products are the most distinguishing:

  • Quick deployment in a variety of environments
  • Greatly facilitated testing
  • Possibility of using Docker as a development environment.
Top 10 Basic Docker Commands You Must Learn (2024)
Top Articles
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 5968

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.