'Changes made to the flask code not reflecting in the Docker container and Multiple Image creation [duplicate]
In the flask code main.py I am using the following script
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True, port=80)
The docker file sets up the environment and it looks like
Dockerfile
FROM python:3.8-slim
LABEL maintainer="nebu"
ENV GROUP_ID=1000 \
USER_ID=1000
RUN apt-get update && apt-get install -y apt-transport-https ca-certificates
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN ["python", "-m", "pip", "install", "--upgrade", "pip", "wheel"]
RUN apt-get install -y python3-wheel
COPY ./requirements.txt /app/requirements.txt
RUN ["python", "-m", "pip", "install", "--no-cache-dir", "--upgrade", "-r", "/app/requirements.txt"]
COPY ./app /app
The docker-compose.yml contains volumes defined and contents are
version: '3.8'
services:
web:
container_name: "flask_container"
build: ./
volumes:
- ./app:/app
ports:
- "8000:8000"
environment:
- DEPLOYMENT_TYPE=production
- FLASK_APP=app/main.py
- FLASK_DEBUG=1
- MONGODB_DATABASE=testdb
- MONGODB_USERNAME=testuser
- MONGODB_PASSWORD=testuser
- MONGODB_HOSTNAME=mongo
command: gunicorn app.main:app --workers 4 --name main -b 0.0.0.0:8000
depends_on:
- redis
links:
- mongo
nginx:
container_name: "nginx_container"
restart: always
image: nginx
volumes:
- ./app/nginx/conf.d:/etc/nginx/conf.d
ports:
- 80:80
- 443:443
links:
- web
redis:
container_name: "redis_container"
image: redis:6.2.6
ports:
- "6379:6379"
worker:
container_name: "celery_container"
build: ./
hostname: worker
command: "celery -A app.routes.celery_tasks.celery worker --loglevel=info"
volumes:
- ./app:/app
links:
- redis
depends_on:
- redis
mongo:
container_name: "mongo_container"
image: mongo:5.0.6-focal
hostname: mongo
restart: always
ports:
- '27017:27017'
environment:
MONGO_INITDB_ROOT_USERNAME: testuser
MONGO_INITDB_ROOT_PASSWORD: testuser
MONGO_INITDB_DATABASE: testdb
volumes:
- mongo-data:/data/db
- mongo-configdb:/data/configdb
volumes:
app:
mongo-data:
mongo-configdb:
I have two issues with this configuration. I am not sure if both can be asked in this single question. (Sincere apologies if cant be asked like this)
- when I use
docker-compose up --buildreal time update of the code is not happening in the container. - Two images are created during the build process. I expected only one image and I dont understand how two images are created like below. Is this due to some mistake done in the configuration.
Solution 1:[1]
As @Klaus D suggested reloading gunicorn will solve issue number 1. So the command in docker-compose.yml becomes
command: gunicorn app.main:app --workers 4 --name main --reload -b 0.0.0.0:8000
Thanks a lot @Klaus D
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | imhans4305 |

