Docker Compose network series--network configuration

Original URL: Docker Compose Network Series--Network Configuration - IT Blog - CSDN Blog

Introduction

illustrate

        This article introduces the network configuration of Docker Compose.

Official website URL

https://docs.docker.com/compose/networking/

The default configuration of compose

        By default, Compose will create a network for the application, and each container of the service (the services item in docker-compose.yaml) will join the network. In this way, the container can be accessed by other containers in the network, and the container can also be accessed by other containers using the service name as the hostname.

default network

For example, suppose there is a project, the directory name is myapp, and the configuration of docker-compose.yml is as follows:

version: "3"
services:
  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres
    ports:
      - "8001:5432"

When executing docker-compose up. The following things will happen:

  1. Will create a network named myapp_default (networks)
  2. The web container will be added to the myapp_default network, and its name in the network is: web.
  3. The db container will be added to the myapp_default network, and its name in the network is: db.

        Here each container can find each other by application name, for example, web container can use postgres://db:5432 to use Pg database.

        One thing to pay attention to in the above example is the port number. Pay attention to distinguish between HOST_PORT and CONTAINER_PORT. Take the above db as an example: 8001 is the port of the host machine, and 5432 (the default port of postgres) is the port of the container. When communicating between containers, it is Connected through CONTAINER_PORT. There is a host port here, so the container can connect to external applications through the host port.

update container

        When the configuration of the service changes, you can use the docker-compose up command to update the configuration.

        At this point, Compose deletes the old container and creates a new one. New containers join the network with different IP addresses, and the names remain the same. Any connections to the old container will be closed and the container will find the new container and connect to it.

links

        By default, services can reach each other using the service name. links allows to define an alias, so as to use the alias to access other services.

version: '2'
services:
  web:
    build: .
    links:
      - "db:database"
  db:
    image:postgres

In this way, the web service can use db or database as hostname to access db.

Specify a custom network

overview

        Sometimes the default network configuration cannot meet the needs, you can use the networks option in the first-level configuration to customize the network.

        The networks option allows creating more complex network topologies and specifying custom network drivers and options. Not only that, but networks can also be used to connect services to externally created networks that are not managed by Compose.

        The networks configuration can also be specified under each service configuration to specify the network configured at the first level.

example

        In the configuration below, the proxy and db services are isolated (cannot communicate because they are not in the same network), and the app service can communicate with both. Use the networks command to facilitate network isolation and connection between services.

version: '2'
services:
  proxy:
    build: ./proxy
    networks:
      - front
  app:
    build: ./app
    networks:
      - front
      - back
  db:
    image: postgres
    networks:
      - back
networks:
  front:
    drvier: custom-driver-1
  back:
    driver: custom-driver-2
    driver_opts:
      foo: "1"
      bar: "2"

Configure default network

In addition to custom networks, default networks can also be configured. If not configured, the default is to use: bridge.

example

        In the configuration below, the application's default network is specified as a custom network (custom-driver-1).

version: '2'
services:
  web:
    build .
    ports:
      - "8000:8000"
  db:
    image: postgres
networks:
  default:
    driver:custom-driver-1

Specify an existing network 

        Network communication is also possible if multiple containers are not in the same configuration (not in an application). the way is:

step 1:

Step 2: Use the external option. Methods as below:

services:
  # ...
networks:
  default:
    name: my-pre-existing-network
    external: true

or:

services:
  # ...
networks:
  default:
    external:
      name: my-pre-existing-network

Guess you like

Origin blog.csdn.net/feiying0canglang/article/details/127935655