Jenkins pipeline does not create postgresql_data volume but running docker compose up -d commad manually creates volumes
I'm pretty new to whole docker,docker-compose,jenkins and whole CI-CD concept. So, I may be missing a very simple point. My application is a django web app with postgresql and nginx. If I run docker compose build and docker compose up -d, 3 containers runs and docker volumes are created without any problems. Now I am trying to run these 3 containers with jenkins. I clone my app's repo to jenkins and run docker compose build and docker compose up -d in my pipeline. My app works, static volume is created but postgresql_data docker volume is not created and it's gone if I restart my host machine
I thought this might be because of user permissions but the static volume is created by the same jenkins task. Thanks in advance.
jenkinsfile
pipeline {
agent {
node {
label 'agent-compose'
}
}
environment{
DOCKERHUB_CREDENTIALS = credentials('DockerHub')
}
stages {
stage('Build') {
steps {
sh 'docker compose build'
echo 'Docker compose build is Completed, these images are created:'
}
}
stage('Run The Services') {
steps {
sh 'docker compose up -d'
}
}
stage('Login To Docker Hub') {
steps {
sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
echo 'Login completed'
}
}
stage('Push To Docker') {
steps {
sh 'docker compose push'
echo 'Push completed'
}
}
}
post{
always{
sh 'docker logout'
}
}
}
docker-compose.yml
version: '3.8'
services:
app:
image: myrepo/app:latest
build: .
# command: gunicorn PrjMovies.wsgi:application --bind 0.0.0.0:8000
volumes:
#- .:/website
- static:/static
ports:
- 8000:8000
env_file:
- .env
depends_on:
- db
db:
image: postgres:15.1-alpine
volumes:
- ./postgres_data:/var/lib/postgresql/data/
ports:
- 5432:5432
environment:
- POSTGRES_DB=prjmoviesdb
- POSTGRES_USER=prjmoviesadm
- POSTGRES_PASSWORD=MyPassWhatever
nginx:
image: myrepo/nginx:latest
build: ./nginx
volumes:
- static:/static
ports:
- 80:80
depends_on:
- app
volumes:
static:
postgres_data:
dockerfile
FROM python:3.8.9-alpine
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONNUNBUFFERED 1
WORKDIR /website
RUN pip install --upgrade pip
#psycopg2 dependencies installation
RUN apk update
RUN apk add postgresql-dev gcc python3-dev musl-dev libc-dev linux-headers
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY . .
COPY ./entrypoint.sh /
ENTRYPOINT [ "sh", "/entrypoint.sh" ]
entrypoint.sh
python manage.py collectstatic --no-input
gunicorn PrjMovies.wsgi:application --bind 0.0.0.0:8000