Home Operation and Maintenance Nginx How to use Docker Compose to implement nginx load balancing

How to use Docker Compose to implement nginx load balancing

May 15, 2023 pm 09:37 PM
docker nginx compose

Using docker network management and container ip settings as basic knowledge to implement nginx load balancing

View all docker networks

docker network ls

/*
network id     name         driver       scope
b832b168ca9a    bridge        bridge       local
373be82d3a6a    composetest_default  bridge       local
a360425082c4    host         host        local
154f600f0e90    none         null        local

*/

// composetest_default 是上一篇介绍compose时,docker-compose.yml文件所在的目录名,
// 所以,用docker-compose创建的容器会默认创建一个以目录名为网络名的网络,并且是dridge(桥接)类型
Copy after login

Specify the container ip address

Official website document address:

Continue to write docker-compose.yml in the previous article "12. Using docker compose container orchestration tool"

version: "3"
services:
  web1:
    container_name: web1
    image: "centos:httpd"
    ports:
      - "8080:80"
    privileged: true
    volumes:
      - "/app/www/web1/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: 192.169.0.3
  web2:
    container_name: web2
    image: "centos:httpd"
    ports:
      - "8081:80"
    privileged: true
    volumes:
      - "/app/www/web2/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: 192.169.0.2
networks:
  nginx-lsb:
    driver: bridge
    ipam:
      config:
        - subnet: 192.169.0.0/16
Copy after login

Use docker-compose to start the container

docker-compose up -d
Copy after login

Check whether the container is started and confirm whether the network nginx-lsb is created

// 可以查看当前docker-compose.yml配置的容器组里的容器状态
docker-compose ps

docker network ls

/*
network id     name          driver       scope
b832b168ca9a    bridge         bridge       local
373be82d3a6a    composetest_default   bridge       local
de6f5b8df1c8    composetest_nginx-lsb  bridge       local
a360425082c4    host          host        local
154f600f0e90    none          null        local
*/

// 创建了nginx-lsb网络,命名是容器组项目的 文件名开头_网络名
Copy after login

Check the details of the network nginx-lsb

docker network inspect composetest_nginx-lsb

// 详情里面可以看到使用这个网络的每个容器的ip

如:

/*
...
 "containers": {
      "039aa860ef04f20a7566fdc943fb4398a61d2ad6dd3e373b17c86ac778af89e3": {
        "name": "web2",
        "endpointid": "1bc206661179e65999015f132c2889d3d4365b8d42b8a89cf9c260016fedd5ee",
        "macaddress": "02:42:c0:a9:00:02",
        "ipv4address": "192.169.0.2/16",
        "ipv6address": ""
      },
      "437ad7a07da8e46c0abaf845c4b08425338009fbe972bde69478cf47c75c315b": {
        "name": "web1",
        "endpointid": "5a36e602a5364ee9ad06e9816d09e3966d56ebf06b9394ebc25b8bcee9546607",
        "macaddress": "02:42:c0:a9:00:03",
        "ipv4address": "192.169.0.3/16",
        "ipv6address": ""
      }
    },
...
*/
Copy after login

Use env_file environment file:

Simple It can be understood as: define variables in docker-compose.yml, and reference variables in external .env files

Official document address:

// 还是在composetest目录中定义个 .env文件,用来存放变量
web1_addr=192.169.0.2
web2_addr=192.169.0.3

// 修改docker-compose.yml文件,加入变量定义
version: "3"
services:
  web1:
    container_name: web1
    image: "centos:httpd"
    ports:
      - "8080:80"
    privileged: true
    volumes:
      - "/app/www/web1/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web1_addr}
  web2:
    container_name: web2
    image: "centos:httpd"
    ports:
      - "8081:80"
    privileged: true
    volumes:
      - "/app/www/web2/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web2_addr}
networks:
  nginx-lsb:
    driver: bridge
    ipam:
      config:
        - subnet: 192.169.0.0/16
Copy after login

Restart the composetest project and view the network details , confirm whether the container IP is set successfully

// 重新启动composetest项目
docker-compose up -d

// 查看网络详情
docker network inspect composetest_nginx-lsb
Copy after login

Add an nginx server as a load balancing server in the composetest project

// 在.env文件里添加一个变量 nginx_lsb
web1_addr=192.169.0.2
web2_addr=192.169.0.3
nginx_lsb=192.169.0.100

// 修改docker-compose.yml文件,加入变量定义
version: "3"
services:
  nginx-lsb:
    container_name: nginx-lsb
    image: "centos:nginx"
    ports: 
      - "8000:80"
    privileged: true
    volumes:
      - "/app/nginx/nginx.conf:/etc/nginx/nginx.conf"
    networks:
      nginx-lsb:
        ipv4_address: ${nginx_lsb}
  web1:
    container_name: web1
    image: "centos:httpd"
    ports:
      - "8080:80"
    privileged: true
    volumes:
      - "/app/www/web1/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web1_addr}
  web2:
    container_name: web2
    image: "centos:httpd"
    ports:
      - "8081:80"
    privileged: true
    volumes:
      - "/app/www/web2/:/var/www/html/"
    command: ['/usr/sbin/init']
    networks:
      nginx-lsb:
        ipv4_address: ${web2_addr}
networks:
  nginx-lsb:
    driver: bridge
    ipam:
      config:
        - subnet: 192.169.0.0/16

// 重新启动composetest项目
docker-compose up -d
Copy after login

Modify the nginx.conf configuration file and configure load balancing

upstream mydocker {
  server 192.169.0.2;
  server 192.169.0.3;
}

server {
  listen 80;
  server_name mydocker;
  location / {
    proxy_set_header host $host;
    proxy_set_header x-real-ip $remote_addr;
    proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
    proxy_buffering off;
    proxy_pass http://mydocker;
  }
}
Copy after login

Restart nginx-lsb, load the configuration file

docker-composer restart nginx-lsb
Copy after login

Visit http://server ip address:8000, test server load balancing!

The above is the detailed content of How to use Docker Compose to implement nginx load balancing. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to view logs from docker How to view logs from docker Apr 15, 2025 pm 12:24 PM

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

See all articles