Docker

Docker Basic Command Line Tips and Tricks

Docker is an open-source well-liked project development and deployment platform. It introduces the concept of containers to build, ship, and execute projects. It is mostly used in the DevOps field. The Docker platform’s main building blocks are Docker images, Docker registry, Docker containers, Docker engine, Docker Server, and Docker client.

This blog will demonstrate:

Prerequisite: Install Docker on Windows

On Windows OS, users can use both the GUI and CLI version of Docker. The Docker GUI version is simpler and easier to manage applications. However, many users prefer to utilize Docker from the command line. The Docker Desktop Windows tool supports both Docker GUI and CLI versions. To use Docker from the command line, users are required to install Docker on the system. For this purpose, navigate to our associated article.

Basic Command Line Tips and Tricks

Docker CLI is the easiest and most effective way to play with the Docker platform. To get started with the Docker command line, the user must have basic knowledge of Docker elements and Docker basic commands. For this reason, we have provided some basic commands to use Docker for beginners:

Command 1: “docker info”

The “docker info” command is utilized to view the detailed information of the installed Docker version:

> docker info

Command 2: “docker –version”

To check out the Docker version you are using on the system, the “docker –version” command is executed:

> docker --version

Command 3: “docker build”

The “docker build” command is widely utilized to create or build the Docker image from the user-defined Dockerfile. This command supports the different options to build images in different ways, such as “–force-rm” which is utilized to build and run images to execute containers. Then, it will delete the container automatically. The “-f” option is used to provide the Dockerfile name along with the path, and “-t” specifies the image tag:

> docker build -t docker-img .

Command 4: “docker run”

This command executes the image in Docker to generate and execute the container. It also supports different options, such as “-d” or “–detach” which is utilized to run the container in detached mode, “-p” define the exposed port, “-i” execute the container interactively, and many more:

> docker run -d -p 8080:8080 docker-img

Command 5: “docker create”

The “docker create” command is frequently utilized to create the containers from Docker images. The container’s name is specified by the “–name” option:

> docker create --name docker-container -p 5000:5000 docker-img

Command 6: “docker start”

After creating the Docker container, users are required to start the container. For this purpose, the “docker start” command is utilized. You can start the container either by container id or container name:

> docker start docker-container

Command 7: “docker stop”

To stop the executing container, the “docker stop” command can be utilized:

> docker stop docker-container

Command 8: “docker commit”

The “docker commit” command is another Docker command line utility used to save the changes within a Docker and commit the Docker container. This command generates a copy of the container in the form of a Docker image. We can say that it is used to generate the image from the container:

> docker commit docker-container

Command 9: “docker login”

The “login” command is utilized to login to the Docker registry. The Docker registries are an essential component of Docker that manages, stores, and publishes Docker images. Docker users can log in to either the Docker private registry or Docker official registry using the “docker login” command. While using the “login” command, it is to provide the username and password credentials:

> docker login

Command 10: “docker tag”

The “docker tag” command is frequently used to define the versions of the Docker images. It can also be utilized to rename the Docker image. To specify the tag or specific version of the image, use the “docker tag <old-img> <new-img>:<tag>” command. In order to publish the image in the Docker registry, specify the username as follows:

> docker tag docker-img rafia098/docker-img:3.6

Command 11: “docker push”

The “docker push” command pushes or publishes the image on the remote registry. For instance, we have pushed the tagged image to the “Docker Hub” Docker official registry:

> docker push rafia098/docker-img:3.6

Command 12: “docker pull”

To download the image from the remote registry to the local registry, utilize the “docker pull” command. This command pulls the Docker image from either the private registry or the Docker Hub registry:

> docker pull rafia098/docker-img:3.6

Command 13: “docker logout”

In order to log out of the Docker registry, use the “docker logout” command as shown below:

> docker logout

Command 14: “docker ps”

The “docker ps” command is another Docker cli that is utilized to display all Docker containers. To view a list of all containers, the “-a” option is utilized along with the command:

> docker ps -a

Command 15: “docker rename”

Sometimes, you may accidentally enter the wrong name for the container, or maybe the container is generated automatically by the Docker image. In such scenarios, developers usually need to change the container name. To rename the container, the “docker rename <old-name> <new-name>” command is used:

> docker rename docker-container docker-new-container

Command 16: “docker kill”

The “docker kill” command is used to stop and kill the running containers in Docker:

> docker kill docker-new-container

Command 17: “docker rm”

The “docker rm” is a widely used Docker cli to remove the Docker containers:

> docker rm docker-new-container

Command 18: “docker images”

The “docker images” is another management command used to manage the Docker images. To view detailed information of the image, simply utilize the “docker images <image-name>” command. In order to view all images, utilize the “docker images” command with the “-a” option:

> docker images

 

Command 19: “docker rmi”

To remove the images from the Docker command line, the “docker rmi” command is utilized. The “-f” is used to remove the image forcefully if the image is associated with any container:

> docker rmi -f docker-img

In order to find or search any image from the Docker Hub registry, simply use the “docker search <image-name>” command. For instance, we have searched for the “python” image:

> docker search python

Command 21: “docker history”

The “docker history” command shows the Docker image history. Basically, it shows all references or changes for the image or inspects the Docker image:

> docker history docker-img

We have discussed the basic commands to use Docker from the command line interface.

Conclusion

The Docker community provides both GUI and CLI versions for the Docker platform. To use Docker from the command line, we have demonstrated the basic command line tips and commands that Docker users must know, such as “docker build”, “docker run”, “docker create”, “docker start”, “docker push”, and “docker pull”. This write-up has provided basic command line tips and tricks.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.