Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Docker data Management and Network Communication

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Blog structure

The method of creating Docker Image

Data Management of Docker

Network Communication of Docker

Docker can be installed and easy to operate through the Dokcer core and installation.

I. the method of creating Docker image

Docker image is not only the core technology of Docker, but also the standard format for application publishing. A complete Docker image can support the operation of a Docker container. During the whole use of Docker, after entering a stereotyped container, you can operate in the container. The most common operation is to install the application service in the container. If you want to migrate the installed service, you need to generate a new image of the environment and the built service.

(1) create based on an existing image

The docker commit command is mainly used to create images based on existing images. Its essence is to package the program running in a container and the running environment of the program to generate a new image.

The format of the command is as follows:

Docker commit [options] Container ID/ name Warehouse name: [label]

The parameters are as follows:

-m: description information-a: author information-p: stop mirroring during generation to create a new image

(1) install Docker first, then create an image

[root@localhost ~] # yum-y install docker [root@localhost ~] # systemctl start docker [root@localhost ~] # mount / dev/cdrom / media/mount: / dev/sr0 write protection [root@localhost ~] # cd / media/ [root@localhost media] # lsapache-tomcat-8.5.16.tar.gz dhcp jdk-8u91-linux-x64.tar.gzcentos httpd registry.tar.gzcentos6 httpd_centos ubuntu-12.04-x86_64-minimal.tar.gz [root@localhost media] # docker load will be mounted read-only

< dhcp \\zair载入镜像到本地[root@localhost media]# docker images \\查看镜像[root@localhost media]# docker create -it docker.io/networkboot/dhcpd /bin/bash \\创建容器dfbe3a15f462d82674cfdfe87dfb7c4b4b1dcf2267e5c0043510cbe10f11a65b[root@localhost /]# docker ps -a \\查看容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESdfbe3a15f462 docker.io/networkboot/dhcpd "/entrypoint.sh /b..." About a minute ago Created determined_dubinsky[root@localhost /]# docker start dfbe3a15f462 \\启动容器dfbe3a15f462[root@localhost /]# docker exec -it dfbe3a15f462 /bin/bashroot@dfbe3a15f462:/# touch 123 \\创建俩个文件root@dfbe3a15f462:/# touch 456 (2) 启动一个镜像,在容器里做修改,然后将修改后的容器提交为新的镜像,需要记住该容器的D号,例如: [root@localhost /]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESdfbe3a15f462 docker.io/networkboot/dhcpd "/entrypoint.sh /b..." 5 minutes ago Up 2 minutes determined_dubinsky (3)使用dockercommit命令创建一个新镜像,如下: [root@localhost /]# docker commit -m "newdhcp" -a "xws" dfbe3a15f462 docker:mydhcpsha256:2c1acb192f78bbbb584fc52954a179eb0f10730e0cd58d120d952439ead45b00 (4)创建完成后.会返回新创建镜像的ID信息。查看本地镜像列表可以看到新创建的镜像信息: [root@localhost /]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker mydhcp 2c1acb192f78 About a minute ago 125 MBdocker.io/networkboot/dhcpd latest 6f98b6b9b486 19 months ago 125 MB (2)基于本地模板创建 通过导入操作系统模板文件可以生成镜像,模板可以从OPENVZ 开源项目下载,下载地址为 http://openvz , org/Download/template/ precreated. 用 命令下载如下 wget http://down1oad. openvz . org/ template/precreated/ubuntu-12.04-x86_ 64-minimal.tar.gz (3)基于Dockerfile创建 除了手动生成Docker镜像之外,可以使用Dockerfile自动生成镜像。Dockerfile 是由-组指令组成的文件,其中每条指令对应Linux中的一条命令, Docker 程序将读取Dockerfile中的指令生成指定镜像。 Dockerfile结构大致分为4个部分:基础镜像信息、维护者信息.镜像操作指令和容器启动时执行指令 在编写Dockerfile时,有严格的格式需要遵循:第一行必须使用FROM指令指明所基于的镜像名称:之后使用MAINTAINER指令说明维护该镜像的用户信息:然后是镜像操作相关指令,如RUN指令,每运行一条指令,都会给基础镜像添加新的一层;最后使用CMD指令来指定启动容器时要运行的命令操作。 案例:使用Dockerfile创建镜像并在容器中运行 1.建立工作目录 [root@localhost /]# mkdir apache[root@localhost /]# cd apache/ 2.创建并编写Dockerfile文件 [root@localhost media]# docker load < centos \\先把centos载入到本地[root@localhost apache]# vim DockerfileFROM centos \\基于centos基础镜像MAINTAINER The Centos projier \\维护该镜像用户信息,后面随便写RUN yum -y update \\镜像操作指令安装apache软件包RUN yum -y install httpdEXPOSE 80 \\开启80端口ADD index.html /var/www/html/index.html //复制网站首页文件ADD run.sh /run.sh //将执行脚本复制到镜像中RUN chmod 775 /run.shRUN systemctl disable httpd \\设置apache开启不启动CMD 【"/run.sh"】 \\启动容器执行脚本 3.编写执行脚本内容 [root@localhost apache]# vim run.sh#!/bin/bash rm -rf /run/httpd/* \\清理http的缓存exec /usr/sbin/apachectl -D FOREGROUND \\启动apache服务 4.创建测试页面 [root@localhost apache]# echo "asd" >

Index.html [root@localhost apache] # lsDockerfile index.html run.sh

5. Use Dockerfile to generate an image

After you have written the Dockerfile and related content, you can use the docker build command to create a mirror.

Docker build [option] path

Automatically generate an image using a newly written dockerfile

[root@localhost apache] # docker build-t httpd:centos.

After the automatic mirror generation command specifies the mirror, do not forget to write the newly generated mirror

Storage path, that is, a "." after the space. Represents the current path, otherwise an error will be reported.

6. Run the container with a new image

Load the newly generated image into the container to run

[root@localhost /] # docker run-d-p 12345 httpd:centosee9adf324443b006ead23f2d9c71f86d1a4eb73358fb684ee3a2d058a0ac4243 httpd:centosee9adf324443b006ead23f2d9c71f86d1a4eb73358fb684ee3a2d058a0ac4243 [root@localhost apache] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES99e9234cefe5 httpd:centos "/ run.sh" 8 seconds ago Up 7 seconds 0.0.0.0 80/tcp youthful_lumieredfbe3a15f462 docker.io/networkboot/dhcpd 12345-> 80/tcp youthful_lumieredfbe3a15f462 docker.io/networkboot/dhcpd "/ entrypoint.sh / b... 56 minutes ago Up 53 minutes determined_dubinsky// uses the newly generated image to load into the container to run / /"-p "option to achieve the mapping from local port 12345 to port 80 in the container.

Visit the web page with a browser

II. 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 data management operation of the container is involved. There are two main ways to manage data in Docker containers: data volumes (Data Volumes) and data volume containers (Data Volumes Containers).

(1) data volume

The data volume is a special directory for the container, which is located in the container. The host directory can be mounted on the data volume. The modification of the data volume is immediately visible, and updating the data will not affect the mirror image. Thus the data can be migrated between the host and the container. The use of data volumes is similar to the mount operation on directories under Linux.

1. Create a data volume

[root@localhost /] # docker run-d-v / data1-v / data2-- name web httpd:centos4944c63124d2f96bedd78b4016e6d96e464089626e97b913b06ec888e7ab8f65 [root@localhost /] # docker exec-it web / bin/bash\\ enter the container and you can see that it is the same as the host [root@4944c63124d2 /] # lsanaconda-post.log boot data2 etc lib media opt root run.sh srv tmp varbin data1 dev home lib64 mnt proc run sbin sys usr [root@4944c63124d2 /] #

(2) Mount the host directory as a data volume

For example

[root@localhost /] # docker run-d-v / var/www:/xws-- name web-1 httpd:centos05079057bf0c7c47b14fd457d1a5df0e29f080b6103753399654ef9d0aa4bf0f

Middle: the directory used to be the host, followed by the directory in the container

[root@localhost /] # cd / var/www/ [root@localhost www] # touch asdasdasd [root@localhost www] # lsasdasdasd

Go into the container and have a look.

[root@localhost /] # docker exec-it web-1 / bin/bash [root@05079057bf0c /] # ls anaconda-post.log boot etc lib media opt root run.sh srv tmp varbin dev home lib64 mnt proc run sbin sys usr xws [root@05079057bf0c /] # cd xws [root@05079057bf0c xws] # lsasdasdasd

You can see that the host shares with the container

(2) data volume container

[root@localhost /] # docker run-it-- volumes-from web-- name 777 httpd:centos / bin/bash [root@d6324596cb2c /] # cd data1 [root@d6324596cb2c data1] # touch file [root@d6324596cb2c data1] # exitexit [root@localhost /] # docker exec-it web / bin/bash [root@4944c63124d2 /] # ls123 bin data1 dev home lib64 mnt proc run sbin sys usranaconda-post.log boot data2 etc lib media opt root run.sh srv tmp var [root @ 4944c63124d2 /] # cd data1 [root@4944c63124d2 data1] # lsfile [root@4944c63124d2 data1] #

You can see that the two containers share

(3) Docker network communication

Docker provides a mechanism for mapping container ports to hosts and container interconnection to provide network services for containers.

To achieve port mapping, you need to use the-P (uppercase) option when running the docker run command to achieve random mapping. Docker will randomly map a port with a port range of 49000 ~ 49900 to a network port open inside the container.

1. Port mapping

[root@localhost /] # docker run-d-P httpd:centos70762709d90a8365803b8b13be02e06e2f9c0b4fdb8624bad01d579817809 [root@localhost /] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES70762709d90a httpd:centos "/ run.sh" 5 seconds ago Up 4 seconds 0.0.0.0 / 32768mm-> 80/tcp kickass_bhaskara

You can see that the port becomes 32768

You can also specify a port

[root@localhost /] # docker run-d-p 123 httpd:centos9c7b1b3989b30f44c22276a62674e565daf410e05bdf0b4892c09dca22662253

two。 Container interconnection

Container interconnection is realized by establishing a special network communication tunnel between containers through the name of containers. To put it simply, a tunnel is established between the source container and the receiving container, and the receiving container can see the information specified by the source container. When running the docker run command, use the-link option to realize the interconnection communication between the containers.

The format is:

-- link name:alias\ alias

Create a source container [root@localhost /] # docker run-d-P-- name web1 httpd:centos0105f396c69b15557af4c15a62143872e725a28050075b554a4d2765a504d558 create a receiving container

Use the docker run command to create the container B _ mai-- name to specify the name as web2. -- link specifies to connect containers to achieve container interconnection.

[root@localhost /] # docker run-d-P-name web2-- link web1:web1 httpd:centos10413ec7492d1d4bab724b4ecf2c2378dae6f496d14c2d68d27ee29b6a26bb1a Test Container Interconnection [root@localhost /] # * * docker exec-it web2 / bin/bash** [root@10413ec7492d /] # * * 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 ms64 bytes from web1 (172.17.0.8): Icmp_seq=2 ttl=64 time=0.063 ms64 bytes from web1 (172.17.0.8): icmp_seq=3 ttl=64 time=0.064 ms64 bytes from web1 (172.17.0.8): icmp_seq=4 ttl=64 time=0.074 ms64 bytes from web1 (172.17.0.8): icmp_seq=5 ttl=64 time=0.065 ms64 bytes from web1 (172.17.0.8): icmp_seq=6 ttl=64 time=0.065 ms

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report