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

Example Analysis of networks of docker-compose Network setup

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces the example analysis of the networks of docker-compose network settings, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

Networks is usually applied to cluster services, so that different applications can run on the same network, thus solving the problem of network isolation. This kind of application is very common in swarm deployment. However, it is not discussed in this article.

Generally speaking, for cluster services, application services are often quickly orchestrated and deployed through docker-compose.yml documents. The following usage scenarios and methods are given on the official website:

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

For example, create a docker-compose.yml under the directory app, as follows:

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

After launching containers using docker-compose up, these containers are added to the app_default network. Use docker network ls to view a list of networks, and docker network inspect to view the configuration of the corresponding network.

$docker net work lsNETWORK ID NAME DRIVER SCOPE6f5d9bc0b0a0 app_default bridge local0fb4027b4f6d bridge bridge local567f333b9de8 docker-compose_default bridge localbb346324162a host host locala4de711f6915 mysql_app bridge localf6c79184ed27 mysql_default bridge local6358d9d60e8a none null local12345678910

2. The networks keyword specifies a custom network

For example, the following docker-compose.yml file defines the front and back networks and implements network isolation. The communication between proxy and db can only be realized through app. Among them, custom-driver-1 can not be used directly, you should replace it with one of the options such as host, bridge, overlay, etc.

Version: '3'services: proxy: build:. / proxy networks:-front app: build:. / app networks:-front-back db: image: postgres networks:-backnetworks: 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 networks are defined here, and it seems that their names are defined as back and font, but you can't find them using the docker network ls command. If you are running the docker-compose up command in the myApp directory, then the two networks should correspond to myApp_back and myApp_front, respectively.

3. Configure the default network

Version: '2'services: web: build: .ports:-"8000 build 8000" db: image: postgresnetworks: default: # Use a custom driver driver: custom-driver-11234567891011121314

4. Use an existing network

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

Problems encountered

After learning the above, the author is ready to put his own project into practice. My project contains two docker-compose.yml and uses the links option, so I must use the networks configuration.

A docker-compose.yml is used to start the mysql service, which is located in the mysql/ directory:

Version: "3" services: dbmaster: image: master/mysql:latest container_name: dbmaster ports:-"3308 image 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 HOME/Work/data/dbslave:/var/lib/mysql environment 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:-appnetworks: default: external: name: app12345678910111213141516171819202122232425262728293031323334353637383940414243

Another docker-compose.yml is used to start the service program, which is located in the cloudgo/ directory:

Version: "3" services: web: image: nginx:latest container_name: web depends_on:-cloudgo ports:-"9090 image 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 logging: driver:" json-file "options: max-size:" 1000k "max-file:" 20 "external_links:-dbmaster-dbslave networks:-appnetworks: 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 written docker-compose.yml file. First run the configuration file that starts mysql, and the result is as follows:

L $docker-compose upERROR: Service "dbmaster" uses an undefined network "app" 12

It was clear that it had been created, but it still reported an error, saying that the network was not defined. Try to change the name mysql_app, but still report the same error. In the end, it turned out that this method could not be realized, and so far no examples given in the official documents have been found.

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

Networks: mysql_app: driver: bridge123

Define a network in this file for later use. After the modification here, the same changes should be made wherever the file is referenced to the network elsewhere. Similarly, the second file is the same.

Some other uses

Use aliases instead of link

The general usage format is as follows:

Services: some-service: networks: some-network: aliases:-alias1-alias3 other-network: aliases:-alias212345678910

In the following example, my web container can access the db container directly through database:3306 or db:3306. They both belong to the same network, and db has a host alias set up, so this kind of access is entirely possible.

Version: '2'services: web: depends_on:-worker networks:-new worker: depends_on:-db networks:-legacy db: image: mysqlnetworks: new: aliases:-database legacy: aliases:-mysqlnetworks: new: legacy:123456789101112131415161718192021222324252627

At this point, you no longer need link to use depends_on directly. If woker needs to access db, you can use mysql:port directly.

The main points of using networks are:

1. Pay attention to the way you customize the network

two。 Note the relationship between the location of the docker-compose.yml file and the default naming of the network

3. Pay attention to problems and try several alternative ways to solve them.

Thank you for reading this article carefully. I hope the article "sample Analysis of networks in docker-compose Network Settings" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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