docker common commands

systemctl start docker #Start docker
systemctl stop docker #Close docker
systemctl restart docker #Restart docker
systemctl enable docker #docker self start
systemctl status docker #View docker running status
docker version #View the version information of docker
docker info #Displays Docker system information, including the number of images and containers
docker --help #Help command for Docker

1, Mirror command

1. docker images gets the local image list

Official documents:

Parameter Description: example: docker images -a
-a: List all local mirrors (including the middle mirror layer)
-q: Show only mirrors ID
--digests: Displays summary information for the mirror
--no-trunc: Displays complete mirror information
[root@localhost ~]# docker images
hello-world   latest    d1165f221234   6 months ago   13.3kB
	There can be multiple sources in the same warehouse TAG,Representing different versions of the repository source, we use REPOSITORY:TAG To define different mirrors. If you do not specify a mirror version label, for example, you only use ubuntu,docker Will be used by default ubuntu:latest image

2. docker search searches for images

Official documents:
Docker official image search:

Parameter Description: example: docker search Image name
-f, --filter filter   Filter according to the conditions provided
  --format string   use Go Template formats search results
  --limit int       Limit the number of search results. The default is 25
  --no-trunc       Displays complete mirror information

Example: search for tomcat images with stars > 1000

[root@gateway01 ~]# docker search --filter=stars=1000 tomcat
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
tomcat    Apache Tomcat is an open source implementati...   3120      [OK] 

3. docker pull pull image

	docker pull Image name
	docker pull Image name:tag

Pull the latest version of the image in the docker warehouse [lastest] without tag (version number)
Add: tag pulls the specified version

The image version can be obtained by searching in the Docker official image
Docker official image search:

4. docker rmi -f delete image

#Delete a
docker rmi -f Image name/image ID
#Delete multiple image ID s or image names separated by spaces 
docker rmi -f Image name/image ID Image name/image ID Image name/image ID
#Delete all mirrors - a displays all, - q displays only ID S
docker rmi -f $(docker images -aq)

Force mirror deletion

docker image rm Image name/image ID

5. Save image

docker save Image name/image ID -o Image storage location and name

6. Load image

docker load -i Image save file location

2, Container command

Note: it is a fundamental premise that a container can be created only with an image

1. View the list of running containers

docker ps

2. View all containers - including running and stopped containers

docker ps -a

3. Create a new container and start it

3.1 start up container

The following examples are a new container created by Docker with Ubuntu 15.10 image

#Execute bin/bash in the container
docker run -i -t ubuntu:15.10 /bin/bash
#Execute / bin/echo "Hello world" in the container
docker run ubuntu:15.10 /bin/echo "Hello world"
#Run in the background
docker run -itd ubuntu:15.10
#Execute / bin/sh -c "while true; do echo hello world; sleep 1; done" in the background
docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
#After the program is executed, you can restart and continue to run
docker run --restart=always -d ubuntu:15.10 /bin/echo "restart test"
#Specify a name for the container
docker run -itd --name=test ubuntu:15.10
#The container exposes 80 / 90 ports and specifies the host 81 / 91 ports to communicate with it
docker run -itd --name=test -p 81:80 -p 91:90 ubuntu:15.10
#Specify the directory in the container to share data volume mount with the host directory, specify the LAN where the container is located, and specify the IP address of the container
docker run -itd --name=test -v Host folder path:Folder path in container --network custom docker LAN name --ip container IP ubuntu:15.10 /usr/bin/init

3.2docker run parameter details

Description of common options
-d, --detach=false, Specifies whether the container runs in the foreground or background. The default is false
-i, --interactive=false, open STDIN,For console interaction
-t, --tty=false, distribution tty The device can support terminal login. The default is false
-u, --user="", Specifies the user of the container
-a, --attach=[], Login container (must be in docker run -d Started container)
-w, --workdir="", Specifies the working directory of the container
-c, --cpu-shares=0, Set container CPU Weights, in CPU Shared scene usage
-e, --env=[], Specifies an environment variable that can be used in the container
-m, --memory="", Specifies the maximum memory limit for the container
-P, --publish-all=false, Specifies the port on which the container is exposed
-p, --publish=[], Specifies the port on which the container is exposed
-h, --hostname="", Specifies the host name of the container
-v, --volume=[], Mount the storage volume to the container and mount it to a directory of the container
--volumes-from=[], Mount a volume on another container to a directory in the container
--cap-add=[], add permission
--cap-drop=[], Delete permissions
--cidfile="", After running the container, write the container in the specified file PID Value, a typical usage of monitoring system
--cpuset="", Set what containers can use CPU,This parameter can be used for container exclusivity CPU
--device=[], Adding a host device to a container is equivalent to a device pass through
--dns=[], Specifies the name of the container dns The server
--dns-search=[], Specifies the name of the container dns Search domain name, write to container/etc/resolv.conf file
--entrypoint="", cover image Entry point for
--env-file=[], Specify an environment variable file in the format of one environment variable per line
--expose=[], Specify the exposed port of the container, that is, the exposed port of the modified mirror
--link=[], Specifies the association between containers, using the of other containers IP,env Other information
--lxc-conf=[], Specifies the configuration file of the container, only when specified--exec-driver=lxc When using
--name="", Specify the container name. You can manage containers by name later, links The feature requires a name
--net="bridge", Container network settings:
		bridge use docker daemon Specified bridge
		host //The container uses the host's network
		container:NAME_or_ID >//Use the network of other containers to share network resources such as IP and PORT
		none The container uses its own network (similar to--net=bridge),But not configured
--privileged=false, Specifies whether the container is a privileged container, and the privileged container owns all capabilities
--restart="no", Specifies the restart policy after the container is stopped:
		no: Do not restart when container exits
		on-failure: Restart on container failure exit (return value non-zero)
		always: Always restart when container exits
--rm=false, Specifies that containers are automatically deleted when they are stopped(Not supported with docker run -d Started container)
--sig-proxy=true, Set the agent to accept and process signals, but SIGCHLD,SIGSTOP and SIGKILL Cannot be represented

4. View container standard output

        --details        Show more information
    -f, --follow         Tracking real-time logs
        --since string   Show from timestamp Subsequent log, or relative time, such as 42 m(I.e. 42 minutes)
        --tail string    How many lines of logs are displayed from the end of the log? The default is all
    -t, --timestamps     presentation time stamp 
        --until string   Show from timestamp Previous log, or relative time, such as 42 m(I.e. 42 minutes)


#View the log after the specified time, and only the last 100 lines are displayed
docker logs -f -t --since="2021-09-14" --tail=100 container ID/Container name
#View the log of the last 10 minutes h h m min s s
docker logs --since 10m container ID/Container name
#View logs for a period of time
docker logs -t --since="2021-09-14T12:00:00" --until "2021-09-15T12:00:00" 

5. Vessel start stop command

Start container: docker start container ID Or container name
 Restart container: docker restart container ID Or container name
 Stop container: docker stop container ID Or container name
 Force stop container: docker kill container ID Or container name

6. Delete stopped containers

#Delete a single container
docker rm container ID/Container name
#Delete multiple container image ID s or image names and separate them with spaces
docker rm container ID/Container name container ID/Container name
#Delete all containers - a displays all, - q displays only ID -f to force deletion. You can delete running containers
docker rm -f $(docker ps -a -q)

7. View the processes running in the container

docker top container ID/Container name

8. Check the internal details of the container

docker inspect container ID/Container name

9. Enter the running container

#Open a new terminal in the container and start a new process
docker exec -it container ID/Container name bash
#Directly enter the container terminal without starting a new process
docker attach container ID/Container name

10. Container file copy

docker cp container ID:Path inside container path outside container
docker cp Host path container ID:In container path

11. Modify the container name and start-up

#Set startup and self startup
docker update --restart=always container ID/Container name
#Modify container name
docker rename container ID/Container name new container name

12. Export and import containers

#Export container snapshot to local file
docker export container ID/Container name>Export file path and name
#Import container snapshots as mirrors
docker import 
cat Import file path and name | docker import - Image name:Mirror version tags

13. The container is packaged into a mirror image

docker commit container ID/Container name mirror name(custom)

3, Import and export of containers and images

Image saving / loading: docker load/docker save; Exporting an image as a file and importing the file as an image using the docker load command will save all the history of the image. It is larger than the file exported by the docker export command, which is easy to understand, because all the history records of the image will be saved.
Container import / Export: docker import/docker export; Export a container as a file, and then use the docker Import command to import the container into a new image. However, compared with the docker save command, the container file will lose all metadata and history records, and only save the current state of the container, which is equivalent to a virtual machine snapshot.

4, docker local area network

Detailed explanation of docker network parameters

connect connects a container to a docker network
create creates a docker LAN
disconnect exits a container from a local area network
inspect displays information about a local area network
ls displays all docker local area networks
prune delete all unreferenced docker LAN
rm delete docker network

#Create custom docker Network -- subnet specifies ip segment -- gateway specifies gateway
#Only networks created with -- subnet can specify static IP addresses
docker network  create --driver bridge --subnet --gateway Network name

5, Summary of container usage problems

1.Failed to get D-Bus connection: Operation not permitted

docker run -itd --name=Container name --privileged=true Image name:Mirror version tags /usr/sbin/init

Tags: Docker

Posted on Fri, 17 Sep 2021 03:40:24 -0400 by RicScott