A Brief introduction to networks for ES0en ES1en network setup

  • 2020-12-09 01:14:16
  • OfStack

networks usage of the official website tutorial

docker-compose. yml reference document: Compose file version 3 reference

Compose file version 3 reference

networks is typically used with cluster services to solve the network isolation problem by enabling different applications to run on the same network. This type of application is very common in swarm deployments. However, this article is not a discussion.

In general, for clustered services, application services are often quickly orchestrated and deployed via the ES26en-ES27en.yml documentation. The official website gives the following usage scenarios and ways:

1. docker-compose. yml of the network environment is not explicitly declared

For example, in the directory app Next, create ES37en-ES38en. yml, which reads as follows:


version: '3'
services:
 web:
  mage: nginx:latest
  container_name: web
  depends_on:
   - db
  ports:
   - "9090:80"
  links:
   - db
 db:
  image: mysql
  container_name: db1234567891011121314

use docker-compose up After starting the containers, these containers are added app_default In the network. use docker network ls You can look at the list of networks, docker network inspect <container id> You can view the configuration of the corresponding network.


$ docker net work ls
NETWORK ID     NAME           DRIVER       SCOPE
6f5d9bc0b0a0    app_default       bridge       local
0fb4027b4f6d    bridge          bridge       local
567f333b9de8    docker-compose_default  bridge       local
bb346324162a    host           host        local
a4de711f6915    mysql_app        bridge       local
f6c79184ed27    mysql_default      bridge       local
6358d9d60e8a    none           null        local
12345678910

2. The networks keyword specifies a custom network

For example, the docker-ES54en. yml file below defines the front and back networks to achieve network isolation. The communication between proxy and db can only be achieved through app. Among them, custom-driver-1 Instead of using it directly, you should replace it with host, bridge, overlay And so on.


version: '3'

services:
 proxy:
  build: ./proxy
  networks:
   - front
 app:
  build: ./app
  networks:
   - front
   - back
 db:
  image: postgres
  networks:
   - back

networks:
 front:
  # Use a custom driver
  driver: custom-driver-1
 back:
  # Use a custom driver which takes special options
  driver: custom-driver-2
  driver_opts:
   foo: "1"
   bar: "2"123456789101112131415161718192021222324252627

It is worth noting that back and front are defined here, as if their names were defined as back and font, but you use docker network ls Commands don't find them. Suppose you were myApp Run under the directory docker-compose up Command, then the two networks should correspond to each other docker-compose up0 and myApp_front .

3. Configure default network


version: '2'

services:
 web:
  build: .
  ports:
   - "8000:8000"
 db:
  image: postgres

networks:
 default:
  # Use a custom driver
  driver: custom-driver-11234567891011121314

4. Use existing networks


networks:
 default:
  external:
   name: my-pre-existing-network1234

Problems encountered

After learning the above, I am ready to put my project into practice. My project contains two docker-ES86en. yml and uses it links Option, so you must use the networks configuration.

One docker-ES94en. yml is used to start the mysql service and is located at mysql/ Directory:


 version: "3"
services:
 dbmaster:
  image: master/mysql:latest
  container_name: dbmaster
  ports:
   - "3308:3306"
  volumes:
   - $HOME/Work/data/dbmaster:/var/lib/mysql
  environment:
   MYSQL_ROOT_PASSWORD: master
  logging:
   driver: "json-file"
   options:
    max-size: "1000k"
    max-file: "20"
  networks:
   - app

 dbslave:
  image: slave/mysql:latest
  container_name: dbslave
  ports:
   - "3309:3306"
  depends_on:
   - dbmaster
  volumes:
   - $HOME/Work/data/dbslave:/var/lib/mysql
  environment:
   MYSQL_ROOT_PASSWORD: slave
  logging:
   driver: "json-file"
   options:
    max-size: "1000k"
    max-file: "20"
  links:
   - dbmaster
  networks:
   - app
networks:
  default:
  external:
   name: app12345678910111213141516171819202122232425262728293031323334353637383940414243

Another ES101en-ES102en. yml is used to start the service program and is located at cloudgo/ Directory:


version: "3"
services:
 web:
  image: nginx:latest
  container_name: web
  depends_on:
   - cloudgo
  ports:
   - "9090:80"
  volumes:
   - $HOME/Work/docker/docker-compose/nginx/conf.d:/etc/nginx/conf.d
  links:
   - cloudgot
  logging:
   driver: "json-file"
   options:
    max-size: "1000k"
    max-file: "20"
  networks:
   - app

 cloudgo:
  image: cloudgo:latest
  container_name: cloudgo
  ports:
   - "8080:8080"
  logging:
   driver: "json-file"
   options:
    max-size: "1000k"
    max-file: "20" 
  external_links:
   - dbmaster
   - dbslave
  networks:
   - app
networks:
 app:
  external: true123456789101112131415161718192021222324252627282930313233343536373839

I decided to use the pre-created network and then add them to the already created network to communicate. To do this, I ran the following command:


$ docker network create app1

After that, start running the docker-ES112en.yml file you wrote. First run the configuration file that starts mysql, and the results are as follows:


l$ docker-compose up
ERROR: Service "dbmaster" uses an undefined network "app"12

Although it has been created, but still reported an error, saying that the network is not defined. Try changing the name mysql_app, but still report the same error. In the end, this approach proved impossible, and no examples have been found in official documentation.

Therefore, it was finally decided to change the networks configuration in the first docker-ES124en.yml file to the following:


networks:
  mysql_app:
   driver: bridge123

Define a network in this file for later use. This is done, and the same changes must be made everywhere else in the file that references the network. Similarly, the second file is the same.

Some other uses

Use aliases instead of link

The format of 1 is as follows:


$ docker net work ls
NETWORK ID     NAME           DRIVER       SCOPE
6f5d9bc0b0a0    app_default       bridge       local
0fb4027b4f6d    bridge          bridge       local
567f333b9de8    docker-compose_default  bridge       local
bb346324162a    host           host        local
a4de711f6915    mysql_app        bridge       local
f6c79184ed27    mysql_default      bridge       local
6358d9d60e8a    none           null        local
12345678910

0

In the following example, mine web The container can go straight through database:3306 or db:3306 access db The container. They both belong to a network, and db The host alias is set, so such access is entirely possible.


$ docker net work ls
NETWORK ID     NAME           DRIVER       SCOPE
6f5d9bc0b0a0    app_default       bridge       local
0fb4027b4f6d    bridge          bridge       local
567f333b9de8    docker-compose_default  bridge       local
bb346324162a    host           host        local
a4de711f6915    mysql_app        bridge       local
f6c79184ed27    mysql_default      bridge       local
6358d9d60e8a    none           null        local
12345678910

1

At this point, using depends_on directly no longer requires link. If woker needs to access db, it can be accessed directly through mysql:port.

The key points of using networks are:
1. Pay attention to the way you customize the network
2. Note the relationship between the location of the docker-ES163en. yml file and the network default name
3. Pay attention to try several alternative ways to solve the problem


Related articles: