- 1. Introduction to docker
- 2. Three core concepts of docker
- 3. Installation and basic operation of docker
1. Installation and Startup
2. Mirror operation of docker
3. Container operation of docker
1. Introduction to docker
As the most popular project in the open source community, Docker is an open source tool that runs applications in Linux containers, is a lightweight "virtual machine", and all the source code of docker is maintained at https://github.com/docker. Its official website is: https://www.docker.com .
The docker has many advantages which are inseparable from the operating system virtualization itself. Traditional virtual machines need additional hypervisors and virtual machine operating system layers. Doker is the virtualization directly on the operating system level. The working diagrams of the two are as follows:
The differences between docker and virtual machine are as follows:
2. Three core concepts of docker
The docker image is the basis for creating containers, similar to a snapshot of a virtual machine, which can be interpreted as a read-only template for the docker container engine. For example, a mirror can be a complete centos operating system environment, called a centos image; it can be an application with MySQL installed, called a MySQLMirror, etc.
The docker provides a simple mechanism to create and update existing images, and we can download existing application images from the web for direct use.
A docker's container is a running instance created from a mirror that can be started, stopped, and deleted.Each container created is isolated from each other, invisible to each other, and a platform for security. Containers can be viewed as a simple version of the Linux environment, and docker uses containers to run and isolate applications.
The docker repository is used for several storage locations. Once you have created your own image, you can use the push command to upload it to a shared or private repository so that the next time you want to use the image on another machine, you can simply pull it off the repository.
Warehouse registration servers are places where warehouses are stored. They contain multiple warehouses, each of which centrally stores a certain type of mirror and uses different labels to distinguish them.The largest public warehouse now is the docker hub, which stores a large number of mirrors for users to download.
The docker's default storage directory is / var/lib/docker, where the docker's mirrors, containers, logs, and so on are all stored. Large-capacity partitions can be used to store these contents separately, and LVM logical volumes are generally chosen to avoid the shortage of storage directories during the docker's run.
3. Installation and basic operation of docker
1. Installation and Startup
When installing, you must ensure that the server configuration can be connected to the Internet or you can configure your own local yum source.
[root@localhost ~]# yum -y install docker #Direct Installation [root@localhost ~]# systemctl start docker #Start docker service [root@localhost ~]# systemctl enable docker #Set to Start-Up Self-Starting [root@localhost ~]# docker version #View installed docker versions
2. Mirror operation of docker
(1) Find Mirrors
The docker needs local mirroring before it can run the container. If no local mirroring exists, the docker will try to find it from the default mirror warehouse https://hub.docker.com/
[root@localhost ~]# docker search dhcp #Search with DHCP as keyword INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/networkboot/dhcpd Suitable for running a DHCP server for you... 40 [OK] docker.io docker.io/joebiellik/dhcpd DHCP server running on Alpine Linux 14 [OK] docker.io docker.io/gns3/dhcp A DHCP container for GNS3 using dnsmasq 2 [OK] docker.io docker.io/instantlinux/dhcpd-dns-pxe Serve DNS, DHCP and TFTP from a small Alpi... 2 [OK] docker.io docker.io/ictu/dhcpd-tftpd dhcpd tftpd container 1 [OK] .................#Omit some content
Returns many mirrors containing DHCP keywords, including the name of the mirror (NAME), description (DESCRIPTION), star (STARS), whether officially created (OFFICIAL), and whether actively created (AUTOMATED).The default output will be sorted by star rating, indicating the popularity of the image. When downloading the image, you can refer to this item. The higher the star, the more popular the image is. Whether the official image is an image created and maintained by an official project group or not, which is generally used by an official project group.A single word serves as a mirror name, which we call a base or root mirror.Named mirrors, such as / reinblau/dhcp, indicate mirrors created and maintained by user reinblau of docker hub with a prefix of user name; active resource creation refers to whether users are allowed to verify the source and content of the mirrors.
(2) Download Mirror
Search for mirrors that match your needs and use the docker pull command to download mirrors from the network for local use.
Command format: docker pull repository name [: label]
For docker mirrors, if no label is specified when downloading the image, the latest version of the image in the repository is downloaded by default, either by choosing the label latest or by specifying a label to download a specific version of the image.This label is used to distinguish between mirrored versions.
Download a mirror as follows
[root@localhost ~]# docker pull docker.io/networkboot/dhcpd #Download a queried image [root@localhost ~]# docker images #Query Downloaded Mirrors REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/networkboot/dhcpd latest 52cbff801df2 5 months ago 105 MB //The following information can be read from the echo information: #REPOSITORY: The warehouse the mirror belongs to; #TAG: Label information for a mirror, marking different mirrors in the same warehouse; #IMAGE ID: The unique ID number of the image that uniquely identifies the image; #CREATED: When the image was created; #SIZE: The size of the mirror //Users can also get detailed information about the mirror based on its unique ID number.The commands are as follows: [root@localhost ~]# docker inspect 52cbff801df2 #Get more information about the mirror #The details of the mirror include the creation time, system version, host name, domain name, user, volume, label, operating system, device ID, and so on. #To use this image for subsequent work, you can use the docker tag command to add a new tag to the local image. #Command format: docker tag name: [label] new name: [new label], as follows: [root@localhost ~]# docker tag docker.io/networkboot/dhcpd dchp:dhcp #Change name and name [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE dchp dhcp 52cbff801df2 5 months ago 105 MB docker.io/networkboot/dhcpd latest 52cbff801df2 5 months ago 105 MB
(3) Delete the mirror
You will find that the original image is still there, so try deleting the original image!
Use the docker RMI command to delete unnecessary mirrors, specify labels to delete, or specify ID s to delete mirrors
Command format for deleting mirrors: docker RMI repository name: label or docker RMI mirror ID number. When a mirror has more than one label, specifying a label only deletes the specified label in the multiple labels of the mirror, does not affect the image file, it is equivalent to deleting only one label of the mirror, but the image only has one labelBe careful when you have one tag left, and then use the Delete command to completely remove the image.
[root@localhost ~]# docker rmi docker.io/networkboot/dhcpd #Remove the original mirror label Untagged: docker.io/networkboot/dhcpd:latest Untagged: docker.io/networkboot/dhcpd@sha256:fdc7ff6f265249a104f32f1d7aed0aedaf2f2fc62ea10eebf596e2af3b670477
When using the docker RMI command followed by the ID number of the image, you must ensure that the image is not used by the container before it can be deleted. When deleting, the system first deletes all tags pointing to the image, then deletes the image file itself. If the image has been used by the container, it is correct to delete the dependent image firstAll containers, then remove the mirror.
(4) Export Mirror
When you need to migrate an image from one machine to another, you need to save the image in a local file called an export image. You can use the docker Save command to save the image and copy it to another machine.
Command format: docker save -o save the image as a local file
[root@localhost ~]# docker images #Get the image name and label REPOSITORY TAG IMAGE ID CREATED SIZE dchp dhcp 52cbff801df2 5 months ago 105 MB [root@localhost ~]# docker save -o dhcptest dchp:dhcp #Export as Local Mirror [root@localhost ~]# du -sh dhcptest #Will be exported to the local current working directory 103M dhcptest
(5) Load Mirror
Copying the exported image from Machine A to Machine B requires that the image be used on Machine B to import the exported file into the mirror library on Machine B. This process is called loading the image.
The command format is (load the mirror guide local mirror library from the file dhcp):
[root@localhost ~]# docker load --input dhcp #perhaps [root@localhost ~]# docker load < dhcp
(6) Upload mirror
As more and more mirrors are stored locally, you need to specify a special place to store them - the warehouse. At present, it is more convenient to designate a public warehouse. By default, it is uploaded to the official dockerhub warehouse. You need to register an account that uses the public warehouse. You can use the docker login command to enter the user name, password and mailbox.Complete registration and login, add new tags to the local image before uploading it, and then upload it using the docker push command.
Command format: docker push repository name: label
[root@localhost ~]# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: ljztest #Enter a registered user name Password: #Input password Login Succeeded #Prompt for successful login [root@localhost ~]# docker push docker.io/ljztest/dhcp #When I logged in here to upload, I got an error and found that there was a problem with the mirror warehouse name. I changed the name of the lower warehouse according to the prompt and the upload succeeded. The push refers to a repository [docker.io/ljztest/dhcp] 8d3d1c857813: Pushed 37ee4253c76e: Pushed b57c79f4a9f3: Pushed d60e01b37e74: Pushed e45cfbc98a50: Pushed 762d8e1a6054: Pushed testdhcp: digest: sha256:fdc7ff6f265249a104f32f1d7aed0aedaf2f2fc62ea10eebf596e2af3b670477 size: 1569
3. Container operation of docker
Containers are another core concept of docker. Simply put, containers are a running instance of a mirror, an application or set of applications running independently and their necessary running environments, including file systems, system class libraries, shell environments, and so on.The mirror is a read-only template, and the container gives this read-only template an additional writable layer.
(1) Creation and startup of containers
The creation of a docker is the process of loading a mirror into a container, which is very lightweight and can be created or deleted at any time by users.The newly created container is stopped by default and does not run any programs. You need to start the container by initiating a process in one of them that is the only process for the container, so when the process ends, the container will also stop completely.Stopped containers can restart and retain their original modifications.You can create a new container using the docker create command.
1) Create containers
The commands are as follows (-i: to keep the input to the container open, that is, to keep the container running; -t: to assign a pseudo terminal to the docker):
[root@localhost ~]# docker create -it dchp:dhcp /bin/bash #Create a container and specify a pseudo-terminal 2304f92a815800305804987bcb2ee20aca5f4d651d577427c476554d54171f2d #If the Create Container command at this time errors "WARNING:IPv4 forwarding is disabled.Network will bot" #work. ", use vim editor to open/usr/lib/sysctl.d/00-system.conf file and add it #net.ipv4.ip_forward=1, then restart the network service using the systemctl start network command [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2304f92a8158 dchp:dhcp "/entrypoint.sh /b..." 8 minutes ago Created naughty_perlman #The output information shows the ID number of the container, the loaded image, the running program, creation time, current state, port mapping, container name, and so on. #The status column found above is create to indicate that the current container is newly created and in a stopped state.
2) Start and stop containers
[root@localhost ~]# docker ps -a #Find the ID number of the container first CONTAINER ID IMAGE COMMAND CREATE 2304f92a8158 dchp:dhcp "/entrypoint.sh /b..." 10 min [root@localhost ~]# docker start 2304f92a8158 #At startup, the ID number of the container needs to be specified 2304f92a8158 [root@localhost ~]# docker ps -a #After querying the status of the container, the status bar becomes UP CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2304f92a8158 dchp:dhcp "/entrypoint.sh /b..." 23 minutes ago Up 42 seconds naughty_perlman [root@localhost ~]# docker stop 2304f92a8158 #Stop this container 2304f92a8158 [root@localhost ~]# docker ps -a #Looking at the status again, the status bar becomes exited CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2304f92a8158 dchp:dhcp "/entrypoint.sh /b..." About an hour ago Exited (0) 4 seconds ago naughty_perlman
3) Enter the container
[root@localhost ~]# docker start 2304f92a8158 #Start this container again [root@localhost ~]# docker exec -it 2304f92a8158 /bin/bash #Enter this container root@2304f92a8158:/# ls #Looking at the root directory of this container, you can see that the command prompt changed when you entered the container bin dev etc lib media opt root sbin sys usr boot entrypoint.sh home lib64 mnt proc run srv tmp var root@2304f92a8158:/# exit #Exit this container exit [root@localhost ~]#
(2) Export and import of containers
[root@localhost ~]# docker ps -a #ID number of query container CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2304f92a8158 dchp:dhcp "/entrypoint.sh /b..." About an hour ago Up 6 minutes naughty_perlman [root@localhost ~]# docker export 2304f92a8158>centos7dhcp #Export to the current working directory and define centos7dhcp as the name [root@localhost ~]# ls #View the exported file, the one marked red below is the exported file anaconda-ks.cfg 'centos7dhcp' dhcptest initial-setup-ks.cfg #Copy the exported file to another server and import it using the docker Import command as a mirror [root@localhost ~]# scp email@example.com:/root/centos7dhcp /tmp #Replicate on another server firstname.lastname@example.org's password: #Enter the end-to-end user password centos7dhcp 100% 84MB 95.2MB/s 00:00 [root@localhost ~]# cd /tmp [root@localhost tmp]# ls #Make sure it's copied over centos7dhcp .............#Omit some content [root@localhost tmp]# cat centos7dhcp | docker import - centos7:dhcp #Import file centos7dhcp as local mirror sha256:e016fa46360492daa9323a0d35bccec76610433f03ba9171fe6d9a5500f823ff [root@localhost tmp]# docker images | grep centos7 #Determine mirror import success centos7 dhcp e016fa463604 14 minutes ago 84.7 MB
(3) Deletion of containers
You can delete a container that has terminated using the docker RM command.
[root@localhost ~]# docker ps -a #Or do you need to find out the ID of the container? CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2304f92a8158 dchp:dhcp "/entrypoint.sh /b..." About an hour ago Up 35 minutes naughty_perlman [root@localhost ~]# docker stop 2304f92a8158 #Stop the container before deleting it 2304f92a8158 [root@localhost ~]# docker rm 2304f92a8158 #Delete Container 2304f92a8158
If you delete a running container, you can force the deletion by adding the -f option, but it is recommended that you stop the container before deleting it.