Application of docker data management and network communication


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  /Bin / bash \ \ create container
[ [email protected]  /]#Docker PS - a \ \ view container
CONTAINER ID    IMAGE             COMMAND         CREATED       STATUS       PORTS        NAMES
dfbe3a15f462  "/ /b..."  About a minute ago  Created                 determined_dubinsky
[ [email protected]  /]#Docker start dfbe3a15f462 \ \ start container
[[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  "/ /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

(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  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

[[email protected] /]# mkdir apache
[[email protected] /]# cd apache/

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  /   //Copy the execution script to the mirror
RUN chmod 775 /
Run systemctl disable httpd \ \ set Apache on or off
CMD 【“/ ”】\Start container execution script

3. Write the content of execution script

[[email protected] apache]# vim
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

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


[[email protected] apache]# docker ps -a

CONTAINER ID    IMAGE             COMMAND         CREATED       STATUS       PORTS          NAMES
99e9234cefe5    httpd:centos         "/"        8 seconds ago    Up 7 seconds>80/tcp  youthful_lumiere
dfbe3a15f462  "/ /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
[ [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 srv tmp var
bin        data1 dev  home lib64 mnt  proc run  sbin  sys usr
[[email protected] /]#

(2) Mount host directory as data volume

for example

[[email protected] /]# docker run -d -v /var/www:/xws --name web-1 httpd:centos

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

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 srv tmp var
bin        dev  home lib64 mnt  proc run  sbin  sys usr xws
[[email protected] /]# cd xws
[[email protected] xws]# ls

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
[[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 srv  tmp var
[[email protected] /]# cd data1
[[email protected] data1]# ls
[[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
[[email protected] /]# docker ps -a
CONTAINER ID    IMAGE             COMMAND         CREATED       STATUS           PORTS          NAMES
70762709d90a    httpd:centos         "/"        5 seconds ago    Up 4 seconds**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

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

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

Container interconnection test

[[email protected] /]#** docker exec -it web2 /bin/bash**
[[email protected] /]# **ping web1**
PING web1 ( 56(84) bytes of data.
64 bytes from web1 ( icmp_seq=1 ttl=64 time=0.153 ms
64 bytes from web1 ( icmp_seq=2 ttl=64 time=0.063 ms
64 bytes from web1 ( icmp_seq=3 ttl=64 time=0.064 ms
64 bytes from web1 ( icmp_seq=4 ttl=64 time=0.074 ms
64 bytes from web1 ( icmp_seq=5 ttl=64 time=0.065 ms
64 bytes from web1 ( 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.