Docker ------ first command

Keywords: Docker Container

preface

Following the content of the previous blog, let's learn about the types of docker commands

1, Mirror operation

1. View docker version

docker version

2. Search for images

Format: docker search keyword
docker search		//Find images from Docker Hub

docker search nginx
NAME	DESCRIPTION	 STARS	OFFICIAL	AUTOMATED

#Parameter Description:
NAME: Name of the mirror warehouse source
DESCRIPTION: Description of the image
OFFICIAL: whether docker Official release
stars: similar Github Inside star,I like it.
AUTOMATED: Auto build.

3. Download Image

Format: docker pull Warehouse name[:label]
#If you do not specify a label when downloading an image, the latest version of the image in the warehouse will be downloaded by default, that is, the label selected is the latest label.
docker pull nginx

4. View the image list

[root@docker ~]# docker images            #List local mirrors
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    ea335eea17ab   8 days ago     141MB

#explain
REPOSITORY    //Mirror warehouse
TAG           //Mirror label
IMAGE ID      //Mirror ID
CREATED       //Image creation time
SIZE          //Mirror size

#option
-a 				//List all local mirrors (including the intermediate image layer. By default, the intermediate image layer is filtered out);
--digests 		//Display the summary information of the image;
-f 				//Display the images that meet the conditions;
--format 		//Specify the template file of the return value;
--no-trunc 		//Display complete image information;
-q 				//Only the mirror ID is displayed

5. View image information

[root@docker ~]# docker inspect nginx / / obtain the metadata of the container image

6. Add image label

Format: docker tag name:[label] New name:[new label]
#Example:
[root@docker ~]# docker tag nginx:latest nginx:web
[root@docker ~]# docker images / / when you view it again, you find one more image
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    ea335eea17ab   8 days ago     141MB
nginx         web       ea335eea17ab   8 days ago     141MB

7. Delete image

Format:
docker rmi Warehouse name:label				#When a mirror has multiple labels, only the specified labels are deleted
 perhaps
docker rmi image ID number						#The mirror is completely deleted
#Optional
-f :Force delete
 Note: if the image has been used by a container, the correct way is to delete all containers that depend on the image before deleting the image.
#Example:
[root@docker ~]# docker rmi nginx:web
Untagged: nginx:web
[root@docker ~]# docker images to view the deleted web tag
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    ea335eea17ab   8 days ago     141MB
#Delete all:
docker rmi `docker images -qa`

#If you just want to delete nginx
docker rmi `docker images |grep nginx:web`

8. Export image

(save the image as a local file)

Format: docker save -o Storage file name storage image 
#-o is output to a file
docker save -o nginx nginx:latest			#The saved image is named nginx and exists in the current directory
ls -lh

9. Image import

#Load image: imports the image file into the image library
 Format:
docker load < Saved files
 perhaps
docker load -i Saved files

docker load < nginx

2, Container operation

1. Create container

This is the process of loading the image into the container.
The newly created container is stopped by default and does not run any program. A process needs to be initiated to start the container.

Format: docker create [option] image
 Common options:
-i: Keep the input of the container open
-t: Give Way Docker Assign a pseudo terminal

[root@docker ~]# docker create -it nginx:latest /bin/bash
a198863ca3e02e9adceb55d09ca14b12187eeaebb66cd02987f8f6a334ee533c

2. Check the operation status of the container

docker ps -a

#Options:
-a :			Displays all containers, including those that are not running.
-f :			Filter the displayed content by criteria.
--format :		Specifies the template file for the return value.
-l :			Displays recently created containers.
-n :			List recently created n A container.
--no-trunc :	Do not truncate output.
-q :			In silent mode, only the container number is displayed.
-s :			Displays the total file size.

3. Start the container

Format: docker start Container ID/name
docker start 8b0a7be0ff58
docker ps -a

4. Create and start the container

You can directly execute the docker run command, which is equivalent to executing the docker create command first and then the docker start command.
Note: the container is a terminal that exists together with the shell command running in it. The command runs, the container runs, the command ends, and the container exits.

  • When using docker run to create containers, the standard running process of Docker in the background is:
    (1) Check whether the specified image exists locally. When the image does not exist, it will be downloaded from the public warehouse;
    (2) Create and start a container using the image;
    (3) Allocate a file system to the container and mount a read-write layer outside the read-only image layer;
    (4) Bridging a virtual machine interface from the bridge interface configured by the host to the container;
    (5) Assign an IP address in an address pool to the container;
    (6) Execute the application specified by the user. After execution, the container is terminated.
docker run centos:7 /usr/bin/bash -c ls /    #Run centos:7 once and view the root
docker ps -a					#You will find that you have created a new container and started executing a shell command, and then stopped


#Continuously run the container created by docker run in the background
 Need in docker run Add after command -d Option let Docker The container runs in the background as a daemon, and the program running by the container cannot end.

[root@docker ~]# docker run -d centos:7 /usr/bin/bash -c "while true;do echo hello;done"
862fe688fde7bcb8db00f2c098b420f43097bee7e285cdec5c6338766efc1781
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
862fe688fde7   centos:7       "/usr/bin/bash -c 'w..."   3 seconds ago    Up 2 seconds                          adoring_perlman


docker run -itd --name test1 centos:7 /bin/bash

-a stdin : 	Specifies the standard input / output content type, optional STDIN/STDOUT/STDERR Three;

-d : 	Run the container in the background and return to the container ID;

-i : 	Run the container in interactive mode, usually with -t Simultaneous use;

-P : 	Random port mapping: the internal port of the container is randomly mapped to the port of the host

-p :	 Specify the port mapping in the format: host(host)port:Container port

-t : 	Reassign a pseudo input terminal to the container, usually with -i Simultaneous use;

--name="nginx-lb" :			 Specify a name for the container;

--dns 8.8.8.8 :	 			Specifies the container to use DNS Server, which is consistent with the host by default;

--dns-search example.com :	 Specify container DNS Search the domain name, which is consistent with the host by default;

-h "mars" : 				Specifies the name of the container hostname;

-e username="ritchie" : 	Setting environment variables;

--env-file=[] : 			Read environment variables from the specified file;

--cpuset="0-2" or --cpuset="0,1,2" : 	Bind container to specified CPU function;

-m :						Set the maximum memory used by the container;

--net="bridge" : 			Specify the network connection type of the container, support bridge/host/none/container : Four types;

--link=[] : 				Add a link to another container;

--expose=[] : 				Open a port or a group of ports;

--volume , -v : 			Bind a volume

5. Terminate vessel operation

Format: docker stop Container ID/name
[root@docker ~]# docker stop 862fe688fde7
862fe688fde7
docker ps -a

6. Entry of containers

When you need to enter the container for command operations, you can use the docker exec command to enter the running container.

Format: docker exec -it container ID/name /bin/bash
-i Option means to keep the input of the container open;
-t Option means let Docker Assign a pseudo terminal.

docker start 2592d3fad0fb					#Before entering the container, make sure the container is running
docker exec -it 2592d3fad0fb /bin/bash
ls
exit				#After exiting the container, the container is still running
docker ps -a

7. Export and import of containers

You can migrate any Docker container from one machine to another. During the migration process, you can use the docker export command to export the created container as a file, whether the container is running or stopped. You can transfer the exported file to other machines and migrate the container through the corresponding import command.

#Export format: docker export container ID / name > file name
docker export a198863ca3e0 > centos7tar

#Import format: cat file name | docker import – image name: Label
cat centos7tar | docker import - centos7:test			#Images are generated after import, but containers are not created


8. Delete container

Format: docker rm [-f] container ID/name
docker stop 2592d3fad0fb       # Stop the container first
docker rm 2592d3fad0fb				#Delete container in terminated state

docker rm -f 2592d3fad0fb			#Force deletion of running containers

docker ps -a | awk 'NR>=2{print "docker stop "$1}' | bash			#Batch stop container

docker ps -a | awk 'NR>=2{print "docker rm "$1}' | bash				#Batch delete all containers

docker images | awk 'NR>=2{print "docker rmi "$3}' | bash			#Batch delete image

Posted by Snart on Fri, 26 Nov 2021 10:13:50 -0800