Flask - Part 10


Introduction

This version_10 is the last part of this course. There won’t be a lot of explanations here.

I simply added some features that I will list below. This app is here to be examined and for you to learn on your own.

This page may change as I add more features (like alembic for DB migrations for example).

Features

Docker

Quick explanation about how to dockerize your app.

  1. Make sure you have gunicorn in your requirements.txt.

  2. Create a file named Dockerfile and add those lines : (yes, that’s a lot of comment)

# this dockerfile inherit from https://github.com/docker-library/python/blob/38dcdb4320c8668416205e044ee50489c059da18/3.7/stretch/slim/Dockerfile
FROM python:3-slim

# we add a user that will run our app so it's not run by root
RUN groupadd -g 999 flaskou && useradd -r -u 999 -g flaskou flaskou

WORKDIR /home/flaskou

# we copy our app inside the container, the folder will be created
# we chown the folder to the user flaskou
COPY --chown=flaskou:flaskou . ./

# we install the dependencies
RUN pip install --no-cache-dir -r requirements.txt

# we switch user to the one we created earlier
USER flaskou

# we run some useful commands like creating the database and creating the first admin
RUN flask reset-db && \
	flask create-admin "root" "root@mail.com" "toor"

# we share this folder with the host
VOLUME /home/flaskou

# finally we run our app
CMD [ "gunicorn", "-b :5000", "wsgi:app" ]

# we expose the port 5000 so the host can access it
EXPOSE 5000
  1. Build the image :
# " . " represents the current folder, where Dockerfile is
docker build -t flaskou .

(if you encounter errors like Temporary failure in name resolution [Errno -3], please check out this article).

  1. Run your app
docker run -p 5000:5000 flaskou

Conclusion

I hope that you found this big flask course cool and helpful !

Feel free to drop me an email / dm on twitter / a comment below if you have any question about Flask or web-development in general.


COMMENTS