30
loading...
This website collects cookies to deliver better user experience
What is Docker multi-stage build?
A multistage build allows you to use multiple images to build a final product. In a multistage build, you have a single Dockerfile, but can define multiple images inside it to help build the final image.
# app.py
from flask import Flask
app = Flask( __name__ )
@app.route("/")
def hello():
return "Hello World!"
Flask
FROM python:3.9-alpine as base
RUN mkdir /app
WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY ./ /app/
########### START NEW IMAGE : DEBUG ###################
FROM base as debug
RUN pip install ptvsd
CMD python -m ptvsd --host 0.0.0.0 --port 5678 --wait --multiprocess -m flask run -h 0.0.0.0 -p 5000
########### START NEW IMAGE: PRODUCTION ###################
FROM base as prod
CMD flask run -h 0.0.0.0 -p 5000
sudo docker build --target debug -t flask-hello-world:debug .
sudo docker run -p 5000:5000 -p 5678:5678 flask-hello-world:debug
sudo docker build --target prod -t flask-hello-world:prod .
sudo docker run -p 5000:5000 flask-hello-world:prod