docker basic command

Posted by mikebr on Sun, 20 Feb 2022 17:21:11 +0100


Since bloggers use docker in their daily development work, they specially find some materials for in-depth study of docker technology, and introduce it to you in combination with their commonly used docker commands in their daily work. Docker commands can be roughly divided into three categories: startup commands, image commands and container commands. These three basic commands are introduced in detail below.

I Start class Command

1. Start docker

systemctl start docker

2. Stop docker

systemctl stop docker

3. Restart docker

systemctl restart docker

4. Check docker running status

systemctl status docker

5. Start docker

systemctl enable docker

6. View docker profile

docker info

7.docker specific command -- help help document. Using this command can help us query the basic instructions of the command

docker run --help  //View the description of docker run command

II Mirror command

1. List all images of the local warehouse

docker images

2. Find an image. If the image TAG is not specified, the default is to pull latest

docker search [Image name:TAG] 

as docker search tomcat

3. Pull the image from the warehouse. If the local warehouse does not have the image, it will automatically pull it from the remote warehouse

docker pull [Image name:TAG]

as:docker pull redis  If not specified TAG All are used by default latest label

4. View the space occupied by images, containers and data volumes

docker system df 

5. Delete one or more mirrors

docker rmi [image id Or image name:tag]  image id Or the image name can be multiple

as:docker rmi tomcat

III Container command

1. Create and start a container

Provided that the local warehouse has downloaded the corresponding image, - it is started in interactive mode, - d is started in background mode, - p specifies the mapping between host port and container port, - name specifies the name of container. For more naming parameter descriptions, you can use docker run --help to check

docker run -it Image name or image id

as:docker run -it ubuntu bash  //Start a container of a ubuntu image in foreground interactive mode

as:docker run -d redis  //Create and start a container of redis image in the background mode

as:docker run -d -it -p 8080:8080 tomcat//Create and start tomcat image container mapping host and container port 8080

2. List all currently running containers and container instances that have run in history

docker ps  //View currently running container instances

docker ps -a  //View container instances that have run in history

docker ps -aq //View all containers and display only the container id

3. Restart the container, start the container that has stopped operation, stop the container and forcibly stop the container, and delete the stopped container

docker restart container id Or container name //Restart container

docker start container id Or container name  //Start stopped container

docker stop container id Or container name // Stop container

docker kill container id Or container name //Force stop container

docker rm container id Or container name [multiple can be taken]  //Delete the stopped container docker rm -f indicates forced deletion

4. There are two ways to exit the container

exit  //If the container is created by run, after entering the container and using exit to exit the container, the container will stop

Ctrl+p+q // If it is a container created by run, exit after entering the container, and the container does not stop

5. Check the container log, running process and internal details

docker log container id Or name    //View container log

docker inspect container id  //View container interior details

docker top container id   //View the processes that the container is running

6. Enter the running container and interact with the command line

The difference between exec and attach is that exec enters the container by opening a new terminal and starting a new process. exit will not stop the container, while attach is a direct start command. The terminal will not start a new process. exit will stop the container

docker exec -it container id bash

docker attach container id

7. import and export containers export the contents of the exported container as a tar archive file. import creates a new file system from the contents of the tar package and then imports it as an image

docker export container id >file name.tar  //Export container

cat file name.tar | docker import - Image name:tag

8. Copy files from the container to the host

docker cp container id:In container path destination host path

For example: docker cp be00d78e6bab:/tmp/data  /root/hyx/mydata

9. Push local image to image warehouse

docker push [Image name:TAG]

as:docker push mytomcat:1.1.3

Well, that's the end of the local! Have you learned the basic command of docker!! The above introduction is a simple introduction to me in the process of learning docker and using many commands in combination with my daily work. Take this blog post to record it!!!

Topics: Operation & Maintenance Docker Container