• 周六. 10 月 5th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Docker note 2: detailed explanation of docker command and related concepts

King Wang

1 月 3, 2022

                                                      Docker

Common commands

  • Find image :docker search Mirror name

  • Pull the mirror image :docker pull Mirror name

  • start-up docker service :sudo service docker start

  • see docker state :systemctl status docker

  • Start the mirror :docker run Mirror name

  • Enter the running container :docker exec -it Container name

  • View the currently running container :docker ps

  • See the last container created :docker ps -l

  • List all containers ID:docker ps -aq

  • See all running or not running containers :docker ps -a

  • Add :docker container ls -l/all

  • A container that stops running :docker stop Containers id/name

  • Restart terminated container :docker start Containers id

  • Restart the running container :docker restart Containers id

  • Delete docker In the container :docker rm Containers id

  • Delete docker All the containers in :docker rm ‘docker ps -a -q’ -f

  • Remove the data volume while deleting the container :docker rm -v Containers id

  • See what images there are :docker images

  • Delete docker In the mirror :docker rmi Mirror image id

  • Delete docker All the images in :docker rmi $(docker images -q)

  • Delete the container first , Delete the image again

  • See mapping container mapping port :docker port Containers id/name

  • Check the standard output inside the container :docker logs Containers id/name

  • View the standard output of the real-time log inside the container :docker logs -f Containers id/name

  • View the processes running inside the container :docker top Container name

  • Mark image :docker tag Mirror name The new mirror name of the tag

  • restart docker: service docker restart

  • Submit image :docker push Mirror name or : docker push Mirror name : Tag name notes : The image name must be docker account number / Mirror name : Tag name , If there is no tag name default latest, You can also add a tag name when you submit

  • Export container :docker export Containers id > file name ( Export container snapshot to local file )

  • Import container :docker import – Snapshot filename …

  • Creating a data volume :docker volume create Data volume name

  • View all data volumes :docker volume ls

  • View the specified data volume information :docker volume inspect Data volume name

  • View container information :docker inspect Container name data volume information in its “Mounts”Key below

  • Delete data volume :docker volume rm Data volume name ( Before deleting a data volume, you need to delete the container using the data volume

  • Delete a data volume that has no master :docker volume prune


Docker Submission of orders

docker commit -m “update” -a=”root” a6e80297382e root/ubuntu:update

  • -m: Description information submitted

  • -a: Specify mirror author

  • a6e80297382e: Containers ID

  • root/ubuntu: Specify the directory image name to create

  • :update: Specify the image label name


Docker Run the command

docker run -i -t ubuntu:15.10 /bin/bash

  • -t: Specify a pseudo terminal or terminal in the new container .

  • -i: Allows you to enter standard input into the container (STDIN) Interact .

docker run -d -P training/webapp python app.py

  • -d: Let the container run in the background

  • -P: Map the network ports used inside the container to the hosts we use .


Docker Run command details

docker run -dit –privileged -p21:21 -p80:80 -p8080:8080 -p30000-30010:30000-30010 –name how2jtmall how2j/tmall:latest /usr/sbin/init

  • docker run Running an image

  • -dit yes -d -i -t Abbreviation . -d , Express detach, Running in the background . -i Means to provide an interactive interface , In this way, we can pass docker and Running operating system interaction . -t To provide a tty ( Pseudo terminal ), And -i Cooperation can be achieved through ssh Tools connected to There’s something going on in this container

  • –privileged When starting the container , Take the authority in . In this way, the complete operation can be carried out in the container

  • -p21:21 first 21, It means that CentOS Open up 21 port . the second 21 Open in a container 21 port . So when the access CentOS Of 21 Port time , It’s going to indirectly access the container

  • -p80:80 and 21 A truth

  • -p8080:8080 and 21 A truth , In this case , The address of the visit is http://192.168.84.128:8080/tmall/, This 192.168.84.128 yes CentOS Of ip Address ,8080 yes CentOS The port of , But by -p8080:8080 Such a mapping , Just visit the… In the container 8080 On port tomcat 了

  • -p30000-30010 and 21 It’s the same thing , This is ftp Used to transmit data

  • –name how2jtmall Gave the container a name , be called how2jtmall, Convenient for follow-up management

  • how2j/tmall:latest how2j/tmall It’s the name of the image , latest It’s the version number , The latest version

  • /usr/sbin/init: Represents a program that runs after startup , This command is used for initialization


Create container process

When using docker run To create the container ,Docker Standard operations running in the background include :

  • Checks if the specified image exists locally , Download from the public repository if it does not exist

  • Create and start a container using an image

  • Assign a file system , And mount a read-write layer outside the read-only mirror layer

  • Bridges a virtual interface from the bridge interface configured in the host host to the container

  • Configure one from the address pool ip Address to container

  • Execute the application specified by the user

  • The container is terminated after execution


An automated build :

  • stay Docker Hub Choose a warehouse , stay Builds Tab Configure Automated Builds;

  • Select a project in the target website ( Need to include Dockerfile) And branches

  • Appoint Dockerfile The location of , And save it .

    After that, you can go to Docker Hub Of the warehouse page Timeline Tab to see the status of each build .


                                                           Data management

Data volume

Data volume concept

A data volume is a special directory that can be used by one or more containers , It bypasses UFS, There are many useful features available :

  • Data volumes can be Shared and reused between containers

  • Yes Changes to the data volume will take effect immediately

  • Yes Update of data volume , It doesn’t affect the mirror image

  • The data volume will always exist by default , Even if the container is deleted

Be careful : The use of data volumes , Be similar to Linux Proceed to the directory or file below mount, Files in the directory specified as the mount point in the mirror are hidden , It can display the attached data volume .

When we run a container ( If you don’t use a roll ), Any file changes made will not be recorded in the container storage layer .


Start data volume

docker run -d -P –name web –mount source=my-vol,target=/webapp training/webapp python app.py

  • -d: Let the container run in the background

  • -P: Map the network ports used inside the container to the hosts we use .

  • –name: Create the name of the container

  • source: Specify the data volume name

  • target: Specify which directory to mount in the container

  • training/webapp: Mirror name

  • python app.py: Container name

Use –mount Tags mount data volumes to containers .

Create a file called web The container of , And load a data volume into the container /webapp Catalog


Delete data volume

  • Data volumes are designed to persist data , Its lifecycle is independent of the container ,Docker The data volume will not be automatically deleted after the container is deleted , And there’s no garbage collection mechanism to deal with data volumes that don’t have any container references . If you need to remove the data volume while deleting the container . Can be used when removing containers docker rm -v This command .

  • Delete data volume :docker volume rm Data volume name ( Before deleting a data volume, you need to delete the container using the data volume )

  • Delete a data volume that has no master :docker volume prune


Mount the host directory

Mount host directory as data volume

docker run -d -P –name web –mount type=bind,source=/home/docker,target=/opt/webapp training/webapp python app.py

  • -d: Let the container run in the background

  • -P: Map the network ports used inside the container to the hosts we use .

  • –name: Create the name of the container

  • source: Specify the local directory , It has to be an absolute path , Use –mount when , If the local directory does not exist Docker Will report a mistake

    • Use -v when , If the local directory does not exist Docker A folder will be created automatically

  • target: Specify which directory to mount in the container

  • training/webapp: Mirror name

  • python app.py: Container name


Read only permission

Docker The default permission to mount the host directory is : Reading and writing , Can increase readonly Designated as : read-only

docker run -d -P –name web –mount type=bind,source=/home/docker,target=/opt/webapp,readonly training/webapp python app.py

  • -d: Let the container run in the background

  • -P: Map the network ports used inside the container to the hosts we use .

  • –name: Create the name of the container

  • source: Specify the local directory , It has to be an absolute path , Use –mount when , If the local directory does not exist Docker Will report a mistake

    • Use -v when , If the local directory does not exist Docker A folder will be created automatically

  • target: Specify which directory to mount in the container

  • readonly: Set the mount directory permission to read-only

  • training/webapp: Mirror name

  • python app.py: Container name


Mount the local file as a data volume

docker run –rm -it –mount type=bind,source=$HOME/.bash_history,target=/root/.bash_history training/webapp bash

  • –rm: The container automatically cleans up the file system inside the container when it exits

  • -it: Run in interactive mode , Assign a pseudo input terminal

  • source: Specify the local directory , It has to be an absolute path , Use –mount when , If the local directory does not exist Docker Will report a mistake

    • Use -v when , If the local directory does not exist Docker A folder will be created automatically

  • target: Specify which directory to mount in the container

  • training/webapp: Mirror name

  • bash: Container name

function history, You can view the commands recorded in the container ( History command of host )


                                                            The Internet

Use the Internet

External access container

docker run -d -P training/webapp python app.py

  • -d: Let the container run in the background

  • -P: Map the network ports used inside the container to the hosts we use .

At this time to use -P When the tag Docker It’s going to map one at random 49000~49900 Port to the internal container open network port


-P You can specify the port to map , also , Only one container can be bound on a specified port . The supported formats are ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort.

Map all interface addresses

Use hostPort:containerPort Format local 5000 The port maps to the container 5000 port , It can be executed

$ docker run -d -p 5000:5000 training/webapp python app.py

All addresses on all local interfaces will be bound by default .

Maps to the specified port of the specified address

have access to ip:hostPort:containerPort The format specifies that the map USES a specific address , such as localhost Address 127.0.0.1

$ docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py

Maps to any port at the specified address

Use ip::containerPort binding localhost Of any port to the container 5000 port , The local host will automatically assign a port .

$ docker run -d -p 127.0.0.1::5000 training/webapp python app.py

You can also use udp Tag to specify udp port

$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

To configure DNS

Configure… For all containers DNS , It can also be in /etc/docker/daemon.json Add the following to the file to set .

{
 "dns" : [
   "114.114.114.114",
   "8.8.8.8"
]
}

This mechanism allows the host to DNS When the information is updated , all Docker Container of DNS Configuration through /etc/resolv.conf The document is updated immediately , Container for each startup DNS Automatically configured to 114.114.114.114 and 8.8.8.8.


 

发表回复