docker 网络 &&docker compose

docker 网络 &&docker compose

One, container interconnection

(1) Port mapping to realize container interconnection

  • The port mapping mechanism promotes the services in the container to external network access
  • Random or specified mapping port range

docker run -d -P httpd:centos  ##  -P 是随机分配端口

docker run -d -p 49888:80 httpd:centos   ##-p  是指定端口映射

(2) Network communication tunnel realizes container interconnection

Container interconnection

  • Establish a network communication tunnel between the source container and the sink container
  • Use the docker run command --link option to achieve interconnection and communication between containers

Realize container interconnection


docker run -d -P --name web1 httpd:centos
docker run -d -P --name web2 --link web1:web1 httpd:centos

docker exec -it web2 /bin/bash

ping web1

(3) Implement container interconnection by specifying the same docker bridge


//新建docker网卡
docker network create --subnet=172.19.0.0/24 mynetwork   ##mynetwork 是docker网络名字

//基于同一张docker 网卡新建一个容器test22
docker run -itd --name test22 --network mynetwork centos:7 /bin/bash

 //基于同一张docker 网卡新建一个容器test33
docker run -itd --name test33 --network mynetwork centos:7 /bin/bash 

//进入容器test33
docker exec -it test33 /bin/bash

//ping test22通
[root@52c6aa6a2235 /]# ping test22 
PING test22 (172.19.0.2) 56(84) bytes of data.
64 bytes from test22.mynetwork (172.19.0.2): icmp_seq=1 ttl=64 time=0.082 ms
64 bytes from test22.mynetwork (172.19.0.2): icmp_seq=2 ttl=64 time=0.096 ms
--- test22 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.082/0.089/0.096/0.007 ms


Two, four modes of docker network

  • When Docker is installed, it will automatically create three networks, bridge (the created container is connected to this network by default), none, host
  • host: The container will not virtualize its own network card, configure its own IP, etc., but use the host's IP and port.
  • Container: The created container will not create its own network card and configure its own ip, but share the ip and port range with a specified container.
  • None: This mode turns off the network function of the container.
  • Bridge: This mode allocates and sets ip for each container, connects the container to a docker0 virtual bridge, and communicates with the host through the docker0 bridge and iptablesNAT table configuration.

The above are all set up without hands, what really needs to be configured is a custom network.

(1) Bridge


docker run -itd --name test1 --network bridge --ip 172.17.0.10 centos:7 /bin/bash

(发现是失败的,因为使用bridge无法支持指定ip——需要自定义ip然后指定ip)

(2) Custom network fixed ip


docker network create --subnet=172.19.0.0/24 mynetwork

docker run -itd --name test44 --net mynetwork --ip 172.19.0.20 centos:7 /bin/bash

  • View custom network mynetwork

docker network ls

[root@localhost opt] docker network ls
NETWORK ID          NAME                    DRIVER              SCOPE
d88329f07179        bridge                  bridge              local
a58999c6c705        compose_nginx_cluster   bridge              local
1236c91097a9        host                    host                local
34801c075b17        mynetwork               bridge              local
a79f44bb5700        none                    null                local

  • View docker container test44

docker ps -a
[root@localhost opt] docker ps -a
CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                      PORTS                                         NAMES
fe58a4a7528c        centos:7              "/bin/bash"         14 seconds ago      Up 13 seconds                                                             test44

  • Delete docker network

docker network rm NETWORK ID   (网络ID)docker network ls

docker network rm 后面输入要删除的network id

Note: If the network card has a specified container running, it needs to be stopped or deleted to delete the custom docker——network

Three, docker compose orchestration

(1) What is compose?

The predecessor of Docker Compose is Fig. It is a tool for defining and running multiple containers.
Docker Compose no longer needs to use shell scripts to start the container.
Docker Compose can use YML files to configure all the services required by the application.

  • Docker Compose is very suitable for scenarios where multiple containers are combined for development

YAML is a very intuitive data serialization format with a markup language, which is very suitable for expressing or editing data structures, various configuration files, file outlines, etc. For example: many email header formats are very similar to YAML

  • File format and writing notes

1. Tab key indentation is not supported. Space indentation is required. Use indentation to indicate hierarchical relationship.
2. Usually the beginning of indentation is 2 spaces. The number of indented spaces is not important, as long as the elements of the same level are aligned to the left may be
3, indented one space character, such as colon, comma, a crossbar
4, using a # Note
5, if the special characters in single quotation marks
6, Boolean values must be enclosed in quotes

  • Example of Docker Compose file structure: docker-compose.yml

    1. Compose version number and service identifier must be written in the top box

    2. Attribute name and attribute value are separated by: (colon plus space)

    3. Use two spaces to indicate the level

    4. The service attribute is represented by-(space space-space)

(2) Docker Compose configuration common fields

Common fields have some explanations in the above yml file format, the following is a detailed explanation

Insert picture description here

  • Docker Compose commonly used commands

Basic command format: docker-compose [option] [command] [parameter]

Insert picture description here

Insert picture description here
Detailed explanation of using Docker Compose: use steps, environment preparation, detailed format

  • Three steps used by compose

1. Use Dockerfile to define the application environment

2. Use docker-compose.yml to define the services that make up the application so that they can run together in an isolated environment

3. Finally execute the docker-compose up command to start and run the entire application

Four, compose one-click deployment and operation of Nginx container

  • Docker compose environment preparation
将docker-compose文件拖至opt中,
cp -p docker-compose /usr/local/bin/

chmod +x /usr/local/bin/dockers-compose

mkdir /root/compose_nginx
cd /root/compose_nginx
mkdir wwwroot
cd wwwroot
vim index.html
<h1>this is compose</h1>
mkdir nginx
cd nginx

vim Dockerfile
FROM centos:7
MAINTAINER this is nginx
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++  pcre  make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
WORKDIR /usr/local/src
WORKDIR nginx-1.12.2
RUN ./configure  --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin/nginx:$PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >>/usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
~  
   

vim run.sh 

#!/bin/bash
/usr/local/nginx/sbin/nginx

  • Set docker-comppose.yml
cd /root/compose_nginx

[root@localhost compose_nginx]# vim docker-comppose.yml 

version: '3'
services:    ##services 是定义下面的服务
  nginx:
    hostname: nginx   ##主机名
    build:          ##创建Nginx镜像
      context: ./nginx   ##切换到镜像目录
      dockerfile: Dockerfile   ##执行Dockerfile
    ports:  #开放端口
     - 1216:80
     - 1217:443
    networks:
     - cluster
    volumes:
     - ./wwwroot:/usr/local/nginx/html    ##宿主机站点目录挂载到Nginx容器中
networks:    ##网络部分:指定上面用到的网络
  cluster:       
[root@localhost compose_nginx]# tree
.
├── docker-comppose.yml
├── nginx
│   ├── Dockerfile
│   ├── nginx-1.12.2.tar.gz
│   └── run.sh
└── wwwroot
    └── index.html

2 directories, 5 files


将Nginx安装包拖至/root/compose_nginx/nginx
  • Use docker-comppose.yml to deploy Nginx with one click

docker-compose -f docker-comppose.yml up -d   //up 是指开启,-d 后台运行,守护进程  -f 使用docker-compose.yml

  • View image compose_nginx_nginx and container
[root@localhost compose_nginx]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
compose_nginx_nginx   latest              baaf5c9230ec        20 minutes ago      480MB

[root@localhost compose_nginx]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                                         NAMES
144bbbf52901        compose_nginx_nginx   "/run.sh"           20 minutes ago      Up 20 minutes       0.0.0.0:1216->80/tcp, 0.0.0.0:1217->443/tcp   compose_nginx_nginx_1

  • verification
  • Real machine browser access

192.168.75.200:1216

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42099301/article/details/108747832