简体   繁体   English

502 Bad gateway docker-compose with Traefik, Nginx in Jenkins

[英]502 Bad gateway docker-compose with Traefik, Nginx in Jenkins

I am coming here because I have a problem related to Jenkins & Docker.我来这里是因为我遇到了与 Jenkins & Docker 相关的问题。

My current goal is to build and deploy my Laravel application through Jenkins.我当前的目标是通过 Jenkins 构建和部署我的 Laravel 应用程序。 I am using a dockerfile that integrates all the elements to make my application work.我正在使用 dockerfile 集成所有元素以使我的应用程序正常工作。 In addition, I am using Traefik as a reverse proxy to access my application via HTTPS, and Nginx as a server.此外,我使用 Traefik 作为反向代理,通过 HTTPS 和 Nginx 作为服务器访问我的应用程序。 (via a conf.d) (通过 conf.d)

To run everything on Jenkins, I use a jenkinsfile which uses the resources on my own gitlab为了在 Jenkins 上运行所有内容,我使用了一个 jenkinsfile,它使用了我自己 gitlab 上的资源

The current problem is that I get a 502 Bad Gateway when I up my docker-compose.当前的问题是,当我启动 docker-compose 时,我得到一个502 Bad Gateway However, I am able to run the application on my remote server.但是,我可以在我的远程服务器上运行该应用程序。 (VPS) (VPS)

Conf.d Conf.d

server {
listen 80;
index index.php index.html;
error_log  /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www;
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass app:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
    try_files $uri $uri/ /index.php?$query_string;
    gzip_static on;
} }

Dockerfile Dockerfile

FROM php:7.4-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    libzip-dev \
    zip \
    unzip

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

COPY . .
RUN composer update
RUN composer install --no-interaction --optimize-autoloader --no-dev

RUN chown -R $user:$user /var/www


USER $user 

EXPOSE 9000

Jenkinsfile詹金斯文件

stage('Build & up for DEV env') {
        when {
            expression {env.GIT_BRANCH == 'origin/develop'}
        }
        steps {
            script{
                sh "docker-compose -f docker-compose.yml build up -d --build"
            }
        }
    }

docker-compose.yml docker-compose.yml

version: "3.7"
services:
  app:
    build:
      args:
        user: test
        uid: 1000
      context: ./
      dockerfile: Dockerfile
    image: val/board:lts
    container_name: val-app
    restart: unless-stopped
    working_dir: /var/www
    networks:
      - fdcks
      - ftboard
    volumes:
      - static-content:/var/www     

  db:
    image: mariadb:10.6.4
    container_name: fatboard-db
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_USER: ${DB_USERNAME}
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - ./docker-compose/mysql:/docker-entrypoint-initdb.d
    labels:
      - traefik.enable=false
    networks:
      - ftboard
      - fdcks

  nginx:
    image: nginx:alpine
    container_name: fatboard-nginx
    restart: unless-stopped
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.nginx.rule=Host(`dev-ftboar.XXXXXXXX.com`)"
      - "traefik.http.routers.nginx-secure.entrypoints=https"
      - "traefik.http.routers.nginx.entrypoints=websecure"
      - "traefik.http.routers.nginx.tls.certresolver=myresolver"
      - "traefik.docker.network=furiousducks"
    volumes:
      - static-content:/var/www
      - ./nginx:/etc/nginx/conf.d/
    ports:
      - 8098:8098
    networks:
      - ftboard
      - fdcks

networks:
  fdcks:
    external: true
  ftboard:

volumes:
  static-content: 

So I use Traefik which is on my remote server which has the same network as my docker-compose used on Jenkins.所以我使用远程服务器上的 Traefik,它与我在 Jenkins 上使用的 docker-compose 具有相同的网络。

Traefik特拉菲克

version: "3.3"

services:

  traefik:
    image: "traefik:latest"
    command:
      - "--log.level=DEBUG"
      - "--api.dashboard=true"
      - "--api=true"
      - "--metrics.prometheus=true"
      - "--metrics.prometheus.buckets=0.1,0.3,1.2,5.0"
      - "--providers.docker.exposedbydefault=false"
      - "--providers.docker.network=furiousducks"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--entrypoints.web.http.redirections.entryPoint.to=websecure"
      - "--entrypoints.web.http.redirections.entryPoint.scheme=https"
      - "--certificatesresolvers.myresolver.acme.dnschallenge=true"
      - "--certificatesresolvers.myresolver.acme.dnschallenge.provider=ovh"
      #- "--certificatesresolvers.myresolver.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
      - "--certificatesresolvers.myresolver.acme.email=XXXXXXXX@gmail.com"
      - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
    ports:
      - "88:80"
      - "443:443"
      - "8084:8084"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.api.rule=Host(`traefik.XXXXXXXXXX.com`)"
      - "traefik.http.routers.api.entrypoints=websecure"
      - "traefik.http.routers.api.tls.certresolver=myresolver"
      - "traefik.http.routers.api.service=api@internal"
      - "traefik.docker.network=furiousducks"

    environment:
      - "OVH_ENDPOINT=XXXXXXXXXXXXX"
      - "OVH_APPLICATION_KEY=XXXXXXXXXXXXXXX"
      - "OVH_APPLICATION_SECRET=XXXXXXXXXXXXXX"
      - "OVH_CONSUMER_KEY=XXXXXXXXXXXXXXXX"
    volumes:
      - "./letsencrypt:/letsencrypt"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    networks:
      - fdcks

I checked on the forum with more than a hundred tests via proposed solutions, nothing works... If you have an idea, I'm interested.我通过建议的解决方案在论坛上进行了一百多个测试,没有任何效果...如果您有想法,我很感兴趣。

Thank you in advance!先感谢您!

EDIT : I didn't provide the network from my traefik on the remote server.编辑:我没有从远程服务器上的 traefik 提供网络。 This is the same configuration as my docker-compose.yml这和我的 docker-compose.yml 配置一样

networks:
  fdcks:
    external: true

UPDATE: I finally solved the problem.更新:我终于解决了这个问题。 Jenkins was working on the "master " node, so I created a new node (slave) on Jenkins and set it up in my " stage " which was deploying my docker-compose. Jenkins 在“主”节点上工作,所以我在 Jenkins 上创建了一个新节点(从)并将其设置在我的“阶段”中,该阶段正在部署我的 docker-compose。

 stage('Build&Run env DEV') {
        agent { label 'node(slave)' }
        steps {
            script{
                    sh """
                      docker-compose down
                      php artisan key:generate
                      docker-compose -f docker-compose.yml up -d --build
                """
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM