Skip to content

Instantly share code, notes, and snippets.

@nmicht
Last active August 23, 2022 02:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmicht/8e07960bd8c029d72e04fdb11be7198d to your computer and use it in GitHub Desktop.
Save nmicht/8e07960bd8c029d72e04fdb11be7198d to your computer and use it in GitHub Desktop.
Docker files for flask app

Docker for flask app

  1. Create a Dockerfile with all the specs for your image
  2. Create a docker-compose.yml file for the basic setup
  3. Create docker-compose.env.yml files for the specific rules for each environment

To build the image execute:
docker-compose -f docker-compose.yml -f docker-compose.env.yml build

To run the container execute:
docker-compose -f docker-compose.yml -f docker-compose.env.yml up

Or build and run together: docker-compose -f docker-compose.yml -f docker-compose.env.yml up --build

version: '3'
services:
docker-flask:
volumes:
- ./app:/home/mich/service/app
environment:
- FLASK_ENV=development
version: '3'
services:
docker-flask:
environment:
- FLASK_ENV=production
version: '3'
services:
docker-flask:
# indication to use the local Dockerfile to build the image
build: .
container_name: "docker-flask"
# map docker port to local port
ports:
- "8000:5000"
# setup environment variables to be used by the flask app
environment:
- FLASK_APP=app/main.py
- PORT=5000
- HOST=0.0.0.0
FROM python:3.8-rc-alpine
# Use user mich instead of root
RUN adduser -D mich
# Copy project filesdoc
ADD . /home/mich/service
WORKDIR /home/mich/service
# Copy setup files like requirements, entrypoint, etc
ADD requirements.txt ./
ADD entrypoint.sh ./
# Add execution permission to entrypoint script
RUN chmod +x ./entrypoint.sh
# Install dependencies
RUN pip install -r requirements.txt
USER mich
EXPOSE 5000
# Execute the app
ENTRYPOINT [ "./entrypoint.sh" ]
#!/bin/sh
# this script is used to run the Flash app using environment variables
flask run --port $PORT --host $HOST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment