0

I want to Running docker-compose inside a dockerized jenkins container. but my docker-compose file is work on my local but when I try to CD in jenkins it doesn't work with this error
[0m[32mgunicorn-backend |[0m sh: 0: Can't open /code/gunicorn/gunicorn_start.sh

  • jenkinsfile
#!groovy
node {
  environment {
    Django_secret_key = credentials('Django_secret_key')
  }
  stage("Checkout") {
    checkout scm
  }

  stage('Stop previous containers') {
    dir('backend') {
       withEnv(["PATH=$PATH:/usr/local/bin"]){
        sh """
            docker-compose -p LBS_Platform down
        """
       }
    }
  }
  stage('Run current containers') {
    dir('backend') {
       withEnv(["PATH=$PATH:/usr/local/bin"]){
        sh """
            docker-compose -p LBS_Platform up --build
        """
       }
    }
  }
}
  • jenkins docker, docker-compose
# dockerfile
FROM jenkins/jenkins:lts

ARG HOST_UID=1004
ARG HOST_GID=999

USER root
RUN curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh
RUN curl -L "https://github.com/docker/compose/releases/download/1.28.6/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose ; chmod +x /usr/local/bin/docker-compose

RUN usermod -u $HOST_UID jenkins
RUN groupmod -g $HOST_GID docker
RUN usermod -aG docker jenkins

USER jenkins
# docker-compose file
version: "3"

services:
  jenkins:
    privileged: true
    build:
      context: ./
    container_name: jenkins
    restart: always
    user: root
    ports:
      - 8083:8080
      - 50003:50000
    expose:
      - "8080"
      - "50000"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "./jenkins_home:/var/jenkins_home"
    environment:
      TZ: "Asia/Seoul"
volumes:
  jenkins_home:
    driver: local
  • docker-compose what i want to run on jenkins coniatiner
# dockerfile
FROM python:3.9
ENV PYTHONUNBUFFERED 1

RUN apt-get -y update 

ARG Django_secret_key
ENV Django_secret_key $Django_secret_key
ENV BOARD_DEBUG 1

# 유저, 그룹 나중에 수정 TODO
# the user to run as
ENV USER root

# how many worker processes should Gunicorn spawn
ENV NUM_WORKERS 3

# which settings file should Django use
ENV DJANGO_SETTINGS_MODULE backend.settings

# WSGI module name
ENV DJANGO_WSGI_MODULE backend.wsgi

ENV PORT 8000

RUN echo "Starting $NAME as $(whoami)"

RUN mkdir /code

WORKDIR /code
COPY . /code/

RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y netcat

RUN chmod 755 /code/gunicorn/gunicorn_start.sh

ENTRYPOINT ["sh", "/code/gunicorn/gunicorn_start.sh"]
# docker-compose file
networks:
  app-tier:
    driver: bridge

services:
  gunicorn-backend:
    restart: always
    container_name: gunicorn-backend
    build:
      context: .
      args:
        Django_secret_key: "${Django_secret_key}"
    command: bash -c "pipenv run python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/code
    networks:
      - app-tier
    ports:
      - "8000:8000"

  nginx-backend:
    restart: always
    container_name: nginx-backend
    image: nginx:latest
    volumes:
      - ./nginx/config:/etc/nginx/conf.d
      - ./nginx/logs:/var/backend-logs
    expose:
      - "80"
      - "443"
    ports:
      - "80:80"
      - "443:443"
    networks:
      - app-tier
    depends_on:
      - gunicorn-backend
    environment:
      - NGINX_HOST=0.0.0.0
      - NGINX_PORT=80
# gunicorn/gunicorn_start.sh
#!/bin/bash
# Name of the application
NAME="backend"

# https://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself
SCRIPT_PATH=$(dirname `which $0`)

# Django project directory
# . 경로
DJANGODIR=$SCRIPT_PATH

# /Users/Han/programming/DjangoCRUDBoard
PARENT_PATH=$(cd $SCRIPT_PATH ; cd .. ; pwd)

# we will communicte using this unix socket
SOCKFILE=$PARENT_PATH/run/gunicorn.sock

echo $PARENT_PATH

# Activate the virtual environment
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

cd $PARENT_PATH

# # DB 연결될때까지 블로킹 (미그레이션은 DB가 연결되어야 가능하다)
# while ! nc -z database 5432; do sleep 1; done;


pip install --upgrade pip
pip install pipenv
pipenv install
pipenv run python manage.py makemigrations
pipenv run python manage.py migrate

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
# pipenv 사용
exec pipenv run gunicorn ${DJANGO_WSGI_MODULE}:application \
-b 0.0.0.0:$PORT \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-

docker & docker-compose work on jenkins container but I don't understand why [0m[32mgunicorn-backend |[0m sh: 0: Can't open /code/gunicorn/gunicorn_start.sh
this error showed.....

Is there any solution to solve this problem?!

한승욱
  • 51
  • 1
  • 6
  • 1
    The `volumes:` are overwriting the `/code` directory in your image with arbitrary content from your host, and that could cause this problem. Does deleting `volumes:` (and running the code built into the image) help? – David Maze Mar 30 '21 at 10:37
  • @DavidMaze Thank you!! It works! I cannot notice that overwriting cause this problem... I realized that my dockerize was not a right way. But still don't know why this wrong way working on my local! – 한승욱 Mar 30 '21 at 12:41

0 Answers0