Valentin Elices
Valentin Elices

Reputation: 1

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

I am coming here because I have a problem related to Jenkins & Docker.

My current goal is to build and deploy my Laravel application through Jenkins. I am using a dockerfile that integrates all the elements to make my application work. In addition, I am using Traefik as a reverse proxy to access my application via HTTPS, and Nginx as a server. (via a conf.d)

To run everything on Jenkins, I use a jenkinsfile which uses the resources on my own gitlab

The current problem is that I get a 502 Bad Gateway when I up my docker-compose. However, I am able to run the application on my remote server. (VPS)

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

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

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

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"
      - "[email protected]"
      - "--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. This is the same configuration as my docker-compose.yml

networks:
  fdcks:
    external: true

Upvotes: 0

Views: 1358

Answers (1)

Valentin Elices
Valentin Elices

Reputation: 1

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.

 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
                """
            }
        }
    }

Upvotes: 0

Related Questions