Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

List out the checklist of Basics docker commands

1. Build an Image:
Build an image from the Dockerfile in the current directory.

docker build -t myimage:1.0 .
Enter fullscreen mode Exit fullscreen mode

2. Run a Container:
Run a container from an image.

docker run -d --name mycontainer myimage:1.0
Enter fullscreen mode Exit fullscreen mode

3. List Running Containers:
List running containers.

docker ps
Enter fullscreen mode Exit fullscreen mode

4. List All Containers:
List all containers, including stopped ones.

docker ps -a
Enter fullscreen mode Exit fullscreen mode

5. Stop a Container:
Stop a running container.

docker stop mycontainer
Enter fullscreen mode Exit fullscreen mode

6. Remove a Container:
Remove a stopped container.

docker rm mycontainer
Enter fullscreen mode Exit fullscreen mode

7. Remove an Image:
Remove an image.

docker rmi myimage:1.0
Enter fullscreen mode Exit fullscreen mode

8. Inspect Container:
Inspect detailed information about a container.

docker inspect mycontainer
Enter fullscreen mode Exit fullscreen mode

9. View Container Logs:
View the logs of a running container.

docker logs mycontainer
Enter fullscreen mode Exit fullscreen mode

10. Execute Command in Running Container:

  • Run a command inside a running container.
 docker exec -it mycontainer bash

 docker exec it   mycontainer   /bin/bash
Enter fullscreen mode Exit fullscreen mode

11. Prune Unused Resources:

  • Remove stopped containers, unused networks, and dangling images.
 docker system prune
Enter fullscreen mode Exit fullscreen mode

12. Pull an Image:

  • Download an image from a registry.
 docker pull nginx:latest
Enter fullscreen mode Exit fullscreen mode

13. Push an Image:

  • Push an image to a registry.
 docker push myimage:1.0
Enter fullscreen mode Exit fullscreen mode

14. Build and Run in One Command:

  • Build an image and run a container from it.
 docker build -t myapp:1.0 . && docker run -d --name myapp-container myapp:1.0
Enter fullscreen mode Exit fullscreen mode

15. Volume Mounting:

  • Mount a local directory into a container.
 docker run -v /path/on/host:/path/in/container myimage:1.0
Enter fullscreen mode Exit fullscreen mode

16. Network Inspection:

  • List Docker networks and inspect details.
 docker network ls
 docker network inspect bridge
Enter fullscreen mode Exit fullscreen mode

17. Docker Compose:

  • Use Docker Compose to define and run multi-container applications.
 docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

18. Environment Variables:

  • Pass environment variables to a container.
 docker run -e MY_VARIABLE=value myimage:1.0
Enter fullscreen mode Exit fullscreen mode

19. Container Stats:

  • View real-time resource usage statistics for a container.
 docker stats mycontainer
Enter fullscreen mode Exit fullscreen mode

20. Docker Swarm (Service Deployment):

  • Initialize a Docker Swarm and deploy a service.
 docker swarm init
 docker service create --name myservice myimage:1.0
Enter fullscreen mode Exit fullscreen mode

To check whether Docker is running and gather information about the Docker installation, you can use the following Docker commands

docker version
Enter fullscreen mode Exit fullscreen mode

output

Client:
 Version:      20.10.7
 API version:  1.41
 Go version:   go1.16.7
 Git commit:   f0df350
 Built:        Wed Aug  4 22:49:47 2021
 OS/Arch:      linux/amd64
 Context:      default
 Experimental: true

Server:
 Engine:
  Version:      20.10.7
  API version:  1.41 (minimum version 1.12)
  Go version:   go1.16.7
  Git commit:   b0f5bc3
  Built:        Wed Aug  4 22:48:34 2021
  OS/Arch:      linux/amd64
  Experimental: false
 containerd:
  Version:      1.4.6
  GitCommit:    d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc:
  Version:      1.0.0-rc95
  GitCommit:    b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:      0.19.0
  GitCommit:    de40ad0
Enter fullscreen mode Exit fullscreen mode

How to get Docker installation, including the number of containers and images, storage driver used, and other system-related information

docker info
Enter fullscreen mode Exit fullscreen mode

output

Client:
 Context:    default
 Debug Mode: false

Server:
 Containers: 2
  Running: 1
  Paused: 0
  Stopped: 1
 Images: 3
 Server Version: 20.10.7
 Storage Driver: overlay2
  Backing Filesystem: extfs
Enter fullscreen mode Exit fullscreen mode

Explanation of all commands

1. Build an Image:
Build an image from the Dockerfile in the current directory.

docker build -t myimage:1.0 .
Enter fullscreen mode Exit fullscreen mode

The docker build command is used to build a Docker image from a specified Dockerfile. The -t flag is used to tag the image with a name and an optional tag. In your example, the image is tagged as myimage with the tag 1.0. The . at the end of the command specifies the build context, which is the path to the directory containing the Dockerfile and any files needed for the build process.

Image description

Image description

Example Dockerfile (Dockerfile)

Image description

Example Requirements File (requirements.txt):

Image description

Image description

Image description

2. Run a Container:
Run a container from an image.

docker run -d --name mycontainer myimage:1.0
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

Image description

3. List Running Containers:
List running containers.

docker ps
Enter fullscreen mode Exit fullscreen mode

The docker ps command is used to list the currently running Docker containers. By default, it shows a list of active containers along with information such as their container ID, names, status, ports, and more.

Here's an example of using the docker ps command:

Assuming you have a running container named "mycontainer" that you started with the following command:

Image description

Image description

Image description

In this example, you can see that the "mycontainer" is running (STATUS is "Up"), and it is mapped to port 4000 on the host (PORTS information).

If your container is not listed, it means there are no running containers. To see all containers, including those that have exited, you can use the -a flag:

Image description

4. List All Containers:
List all containers, including stopped ones.

docker ps -a
Enter fullscreen mode Exit fullscreen mode

The docker ps -a command is used to list all Docker containers, including both running and stopped containers. This command provides a comprehensive view of all containers on your system.

Here's an example:

Assuming you have run a container named "mycontainer" with the following command:

Image description

Now, if you use the docker ps -a command:

Image description

Image description

Image description

5. Stop a Container:
Stop a running container.

docker stop mycontainer
Enter fullscreen mode Exit fullscreen mode

The docker stop command is used to stop a running Docker container. When you stop a container, it gracefully terminates the processes running inside the container and then stops the container itself.

Here's an example using the container named "mycontainer":

This command stops the "mycontainer" container. If the container is running a process or application, that process will be given a chance to perform any necessary cleanup before the container is halted.

After running this command, you can use docker ps to check the list of running containers. The output should no longer include "mycontainer" because it has been stopped.

Example output of docker ps after stopping the container:

Image description

In this example, you can see that "mycontainer" is no longer marked as "Up" in the "STATUS" column; instead, it shows "Exited (0)." The "(0)" indicates that the container exited without any errors (exit code 0).

If you want to remove the stopped container, you can use the docker rm command:

docker rm mycontainer
Enter fullscreen mode Exit fullscreen mode

This will remove the stopped container from your system. Be cautious with this command, as it permanently deletes the container and its associated filesystem.

Remember to replace "mycontainer" with the actual name or ID of the container you want to stop.

Docker Run command

This command is used to run a container from an image. The docker run command is a combination of the docker create and docker start commands. It creates a new container from the image specified and starts that container. if the docker image is not present, then the docker run pulls that.

$ docker run <image_name>
To give name of container
$ docker run --name <container_name> <image_name>
Enter fullscreen mode Exit fullscreen mode

Image description

Docker Pull

This command allows you to pull any image which is present in the official registry of docker, Docker hub. By default, it pulls the latest image, but you can also mention the version of the image.

$ docker pull <image_name>
Enter fullscreen mode Exit fullscreen mode

Image description

Docker PS

This command (by default) shows us a list of all the running containers. We can use various flags with it.

-a flag: shows us all the containers, stopped or running.
-l flag: shows us the latest container.
-q flag: shows only the Id of the containers

$ docker ps [options..]
Enter fullscreen mode Exit fullscreen mode

Image description

Docker Stop

This command allows you to stop a container if it has crashed or you want to switch to another one.

$ docker stop <container_ID>
Enter fullscreen mode Exit fullscreen mode

Docker rm

To delete a container. By default when a container is created, it gets an ID as well as an imaginary name such as confident_boyd, heuristic_villani, etc. You can either mention the container name or its ID.

Some important flags:

-f flag: remove the container forcefully.
-v flag: remove the volumes.
-l flag: remove the specific link mentioned.
$ docker rm {options}

docker remove an image

Image description

Docker RMI
To delete the image in docker. You can delete the images which are useless from the docker local storage so you can free up the space

docker rmi <image ID/ image name>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)