Docker-compose搭建nginx负载均衡

少年辛苦终身事,莫向光阴惰寸功。这篇文章主要讲述Docker-compose搭建nginx负载均衡相关的知识,希望能为你提供帮助。
1.系统环境:
# cat /etc/redhat-release

Rocky Linux release 8.5 (Green Obsidian)
# docker-compose version

docker-compose version 1.29.2, build 5becea4c

docker-py version: 5.0.0

Cpython version: 3.7.10

OpenSSL version: OpenSSL 1.1.0l   10 Sep 2019
2.配置文件:
# cat Dockerfile

FROM centos:latest

RUN mkdir -p /var/log/nginx

RUN touch /var/log/nginx/error.log

RUN yum install -y cmake automake autoconf libtool make pcre pcre-devel   \\

    openssl openssl-devel gd-devel gcc gcc-c++ wget curl & & \\

    yum clean all & & \\

    rm -rf /var/cache/yum/*

RUN wget https://nginx.org/download/nginx-1.16.1.tar.gz

RUN tar -zxvf nginx-1.16.1.tar.gz

WORKDIR nginx-1.16.1

RUN ./configure --prefix=/usr/local/nginx \\

                --with-http_image_filter_module \\

                --with-http_ssl_module \\

                --with-http_v2_module \\

                --with-http_stub_status_module \\

                --error-log-path=/var/log/nginx/error.log \\

                --http-log-path=/var/log/nginx/access.log \\

                --pid-path=/var/run/nginx/nginx.pid

RUN make & & make install & & \\

    rm -rf /usr/local/nginx/html/* & & \\

    echo "hello everyone !" > /usr/local/nginx/html/index.html & & \\

    rm -rf nginx*

ENV LOG_DIR /var/log/nginx

ENV PATH $PATH:/usr/local/nginx/sbin

EXPOSE 80

WORKDIR /usr/local/nginx/sbin

CMD ["nginx","-g","daemon off; "]
# cat nginx.conf

server

  listen   80 default_server;

  location /

    proxy_pass http://nginx:80;

 


# cat nginx.yml

version: "3.7"



services:

  nginx:

    build:

      context: /root/Docker

      dockerfile: /root/Docker/Dockerfile

    image: centos:latest

    ports:

      - 80

    volumes:

      - /root/Docker/itec:/usr/local/nginx/html

    networks:

      - bridge



  nginx-server:

    container_name: Nginx-Server

    image: nginx:stable-alpine

    ports:

      - 8080:80

    volumes:

      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro

      - ./var/log/nginx:/var/log/nginx

    depends_on:

      - nginx

    networks:

      - bridge



networks:

  bridge:
3.启动容器
【Docker-compose搭建nginx负载均衡】# docker-compose -f nginx.yml up -d --scale nginx=3

    推荐阅读