July 2, 2024
Tutorials

An Essential Commands for Docker

An-Essential-Docker-Commands

Docker is a lightweight and portable containerization platform that has completely changed how developers create, ship, and run applications. Whether you are an experienced developer or just getting started with Docker, understanding essential commands is important for effectively using this powerful tool.

We will go through in this tutorial the essential Docker commands that are fundamentals to use containers in Dockers. Make sure Docker is already installed on your Linux system. if it is not installed, follow this guide.

Basic Docker Commands: How to Use Docker?

In this section, we will list and discuss the basic commands that you should know to run Docker on your Linux system.

1. Check Docker Installation

Before going into depth about Docker container management, first, it’s important to verify your Docker installation. To view the currently installed docker version, use the `docker version` command that displays detailed information about the installed Docker version, while docker info offers system-wide details, helping in troubleshooting and optimization.

$ docker version

$ docker info

2. Search For a Docker Image

You can search for a specific application image using the `docker search ‘ command.

$ sudo docker search [image-name]

For example, we want to search for the ‘mariadb’ image. The above command will be modified in this way:

$ sudo docker search mariadb

3. Download Docker Images

Docker images serve as the blueprints for containers. To download a Docker image, use the `docker pull` command to download an image from a registry. The syntax for using this command is given below:

$ docker pull <image_name>

For example, to download a test ‘hello world’ Docker image, you will use the below command:

$ sudo docker pull hello-world

4. Build Docker Images

Building custom images is a common practice in Docker. The docker build command allows you to create an image from a Docker file.

$ docker build -t <image_name> <path_to_Dockerfile>

5. List Local Docker Images

To view all locally available images, use the docker images command.

$ docker images

6. Manage Containers in Docker

Containers are instances of images. docker ps lists running containers, while docker ps -a includes stopped ones.

$ docker ps
$ docker ps -a

6. Run Docker Image

The docker run command creates and starts a container from a specified image.

$ docker run <options> <image_name>:<tag>

For example, we want to run the downloaded ‘hello-world’ Docker image. To run this image, the above command will be used in the following way:

$ sudo docker run hello-world

7. List Docker Container Log

To list all docker container log or running state, use the below command:

$ sudo docker container ls -a

8. Run Commands Inside Containers

Execute commands within a running container using the docker exec command.

$ sudo docker container start <container_image_name>
$ sudo docker container start hardcore_allen

$ docker exec -it <container_id_or_name> <command>

You can stop and remove containers. To stop a running container use `docker stop` and remove it with the `docker rm` command.

$ docker stop <container_id_or Image_name>
$ docker rm <container_id_or image_name>

9. Remove Docker Images

Unused images can consume storage. Remove them using the docker rmi command.

$ docker rmi <image_name>:<tag>

For example, to remove the ‘hello-world’ image, use the below command:

10. Managing Networks and Volumes

You can list all networks and volumes using `docker network ls` and `docker volume ls` commands respectively.

$ docker network ls

$ docker volume ls

Conclusion

In this tutorial, we have learned the basic docker commands that are necessary to manage Docker containers and images. These commands not only run the docker image but, also you can easily manage your containerized environment using the above basic Docker commands. I hope the above command will give you the complete understanding about Docker Fundamentals. To learn more about Docker, you can visit its official Docs.

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video