You can install docker and simple operation through dokcer core and installation.
1、 How to create docker image
Docker image is not only the core technology of docker, but also the standard format of application publishing. A complete docker image can support the operation of a docker container. In the whole process of using docker, after entering a stereotyped container, you can operate in the container. The most common operation is to install application services in the container. If you want to migrate the installed services, you need to generate a new image of the environment and the services built.
(1) Create based on existing image
Based on the existing image creation, the docker commit command is mainly used. Its essence is to package the program running in a container and the running environment of the program to generate a new image.
The command format is as follows:
Docker commit [option] container ID / name warehouse Name: [label]
The parameters are as follows:
- -m: Explanatory information
- -a: Author information
- -p: Stop mirroring during build to create a new mirror
(1) First install docker and create an image
[[email protected] ~]# yum -y install docker [[email protected] ~]# systemctl start docker [[email protected] ~]# mount /dev/cdrom /media/ Mount: / dev / sr0 is write protected and will be mounted read-only [[email protected] ~]# cd /media/ [[email protected] media]# ls apache-tomcat-8.5.16.tar.gz dhcp jdk-8u91-linux-x64.tar.gz centos httpd registry.tar.gz centos6 httpd_centos ubuntu-12.04-x86_64-minimal.tar.gz [ [email protected] Media] # docker load < DHCP \ \ zair load image locally [ [email protected] Media] # docker images \ \ view image [ [email protected] media]# docker create -it docker.io/networkboot/dhcpd /Bin / bash \ \ create container dfbe3a15f462d82674cfdfe87dfb7c4b4b1dcf2267e5c0043510cbe10f11a65b [ [email protected] /]#Docker PS - a \ \ view container CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dfbe3a15f462 docker.io/networkboot/dhcpd "/entrypoint.sh /b..." About a minute ago Created determined_dubinsky [ [email protected] /]#Docker start dfbe3a15f462 \ \ start container dfbe3a15f462 [[email protected] /]# docker exec -it dfbe3a15f462 /bin/bash [email protected] : / # touch 123 \ \ create two files [email protected]:/# touch 456
(2) For example, after the container of the new image is changed, the container D needs to be modified
[[email protected] /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dfbe3a15f462 docker.io/networkboot/dhcpd "/entrypoint.sh /b..." 5 minutes ago Up 2 minutes determined_dubinsky
(3) Use the dockercommit command to create a new image, as follows:
[[email protected] /]# docker commit -m "newdhcp" -a "xws" dfbe3a15f462 docker:mydhcp sha256:2c1acb192f78bbbb584fc52954a179eb0f10730e0cd58d120d952439ead45b00
(4) After the creation is completed, the ID information of the newly created image will be returned. Viewing the local image list, you can see the newly created image information:
[[email protected] /]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker mydhcp 2c1acb192f78 About a minute ago 125 MB docker.io/networkboot/dhcpd latest 6f98b6b9b486 19 months ago 125 MB
(2) Create based on local template
The image can be generated by importing the operating system template file. The template can be downloaded from OpenVZ open source project at
Download the following with the command
wget http://down1oad. openvz . org/ template/precreated/ubuntu-12.04-x86_ 64-minimal.tar.gz
(3) Create based on dockerfile
In addition to manually generating the docker image, the dockerfile can be used to automatically generate the image. Dockerfile is a file composed of – group instructions. Each instruction corresponds to a command in Linux. The docker program will read the instructions in dockerfile and generate the specified image.
The dockerfile structure is roughly divided into four parts: basic image information, maintainer information, image operation instruction and container startup execution instruction
When writing dockerfile, there are strict formats to follow: the first line must use the from instruction to indicate the image name it is based on; then use the maintainer instruction to describe the user information for maintaining the image; and then, instructions related to image operation, such as run instruction, will add a new layer to the basic image every time an instruction is run; Finally, the CMD instruction is used to specify the command operation to run when the container is started.
Case: using dockerfile to create an image and run it in a container
1. Establish a working directory
2. Create and write dockerfile file
[ [email protected] Media] # docker load < CentOS \ \ load CentOS locally first [[email protected] apache]# vim Dockerfile From CentOS \ \ based on CentOS Maintainer the CentOS projier \ \ maintain the user information of the image, and write it casually later Run Yum - y update \ \ image operation instruction to install Apache Software Package RUN yum -y install httpd Open port 80 ADD index.html /var/www/html/ index.html //Copy home page file ADD run.sh / run.sh //Copy the execution script to the mirror RUN chmod 775 /run.sh Run systemctl disable httpd \ \ set Apache on or off CMD 【“/ run.sh ”】\Start container execution script
3. Write the content of execution script
[[email protected] apache]# vim run.sh #!/bin/bash RM - RF / run / httpd / * \ \ clear HTTP cache Exec / usr / SBIN / apachectl - D foreglobal \ \ start Apache service
4. Create test page
[[email protected] apache]# echo "asd" >index.html [[email protected] apache]# ls Dockerfile index.html run.sh
5. Use dockerfile to generate image
After the dockerfile and related contents are written, the image can be created through the docker build command.
Docker build [options] path
Using dockerfile written just now to generate images automatically
[[email protected] apache]# docker build -t httpd:centos .
After the command to automatically generate the image specifies the image, do not forget to write the storage path of the newly generated image, that is, the “‘” after the space represents the current path, otherwise an error will be reported.
6. Use the new image to run the container
Load the newly generated image into the container to run
[[email protected] /]# docker run -d -p 12345:80 httpd:centos ee9adf324443b006ead23f2d9c71f86d1a4eb73358fb684ee3a2d058a0ac4243 [[email protected] apache]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 99e9234cefe5 httpd:centos "/run.sh" 8 seconds ago Up 7 seconds 0.0.0.0:12345->80/tcp youthful_lumiere dfbe3a15f462 docker.io/networkboot/dhcpd "/entrypoint.sh /b..." 56 minutes ago Up 53 minutes determined_dubinsky //Load the newly generated image into the container to run //The "- P" option maps from local port 12345 to port 80 in the container
Access web pages with browser
2、 Data management of docker
In docker, in order to easily view the data generated in the container or share the data among multiple containers, the container data management operation is involved. There are two ways to manage the data in the docker container: data volumes and data volumes containers.
(1) Data volume
Data volume is a special directory for containers. It is located in the container. It can mount the directory of the host to the data volume. The modification of the data volume is immediately visible, and updating the data will not affect the image. Thus, the data migration between the host and the container can be realized. The use of data volume is similar to the mount operation of directory under Linux.
1. Create data volume
[[email protected] /]# docker run -d -v /data1 -v /data2 --name web httpd:centos 4944c63124d2f96bedd78b4016e6d96e464089626e97b913b06ec888e7ab8f65
[ [email protected] /]#Docker exec - it web / bin / bash \ \ enter the container and you can see that it is the same as the host [[email protected] /]# ls anaconda-post.log boot data2 etc lib media opt root run.sh srv tmp var bin data1 dev home lib64 mnt proc run sbin sys usr [[email protected] /]#
(2) Mount host directory as data volume
[[email protected] /]# docker run -d -v /var/www:/xws --name web-1 httpd:centos 05079057bf0c7c47b14fd457d1a5df0e29f080b6103753399654ef9d0aa4bf0f
Middle: used to be the host directory, followed by the container directory
[[email protected] /]# cd /var/www/ [[email protected] www]# touch asdasdasd [[email protected] www]# ls asdasdasd
Go into the container and have a look
[[email protected] /]# docker exec -it web-1 /bin/bash [[email protected] /]# ls anaconda-post.log boot etc lib media opt root run.sh srv tmp var bin dev home lib64 mnt proc run sbin sys usr xws [[email protected] /]# cd xws [[email protected] xws]# ls asdasdasd
You can see that the host is sharing with the container
(2) Data volume container
[[email protected] /]# docker run -it --volumes-from web --name 777 httpd:centos /bin/bash [[email protected] /]# cd data1 [[email protected] data1]# touch file [[email protected] data1]# exit exit [[email protected] /]# docker exec -it web /bin/bash [[email protected] /]# ls 123 bin data1 dev home lib64 mnt proc run sbin sys usr anaconda-post.log boot data2 etc lib media opt root run.sh srv tmp var [[email protected] /]# cd data1 [[email protected] data1]# ls file [[email protected] data1]#
You can see that the two containers share
(3) Docker network communication
Docker provides mapping container port to host and container interconnection mechanism to provide network services for container.
To realize port mapping, you need to use the – P (upper case) option to implement random mapping when running the docker run command. Docker will randomly map a port in the range of 49000 ~ 49900 to the open network port in the container,
1. Port mapping
[[email protected] /]# docker run -d -P httpd:centos 70762709d90a8365803b8b13be02e06e2f9c0b4fdb8624bad01d579817809 [[email protected] /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 70762709d90a httpd:centos "/run.sh" 5 seconds ago Up 4 seconds 0.0.0.0:**32768**->80/tcp kickass_bhaskara
You can see that the port changes to 32768
You can also specify the port
[[email protected] /]# docker run -d -p 123:80 httpd:centos 9c7b1b3989b30f44c22276a62674e565daf410e05bdf0b4892c09dca22662253
2. Container interconnection
Container interconnection is a special network communication tunnel between containers through the names of containers. In short, a tunnel will be established between the source container and the receiving container. The receiving container can see the information specified by the source container. When running the docker run command, the — link option is used to realize the interconnection and communication between containers.
The format is:
–Link name: alias
Create source container
[[email protected] /]# docker run -d -P --name web1 httpd:centos 0105f396c69b15557af4c15a62143872e725a28050075b554a4d2765a504d558
Create receive container
Use the docker run command to create a container B, — name specifies the name as web2. — link specifies the connection container to realize container interconnection.
[[email protected] /]# docker run -d -P --name web2 --link web1:web1 httpd:centos 10413ec7492d1d4bab724b4ecf2c2378dae6f496d14c2d68d27ee29b6a26bb1a
Container interconnection test
[[email protected] /]#** docker exec -it web2 /bin/bash** [[email protected] /]# **ping web1** PING web1 (172.17.0.8) 56(84) bytes of data. 64 bytes from web1 (172.17.0.8): icmp_seq=1 ttl=64 time=0.153 ms 64 bytes from web1 (172.17.0.8): icmp_seq=2 ttl=64 time=0.063 ms 64 bytes from web1 (172.17.0.8): icmp_seq=3 ttl=64 time=0.064 ms 64 bytes from web1 (172.17.0.8): icmp_seq=4 ttl=64 time=0.074 ms 64 bytes from web1 (172.17.0.8): icmp_seq=5 ttl=64 time=0.065 ms 64 bytes from web1 (172.17.0.8): icmp_seq=6 ttl=64 time=0.065 ms
The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.