Git Product home page Git Product logo

flask-on-docker's Introduction

Flask on Docker

Overview

In this repository, we develop a Flask application to run using Docker. The application supports basic static webpages as well as allowing the user to upload an image and view their uploaded images. Postgres was used for the database, and Nginx and Gunicorn were included to support the production environment in order to help handle the static and media file upload/view functionalities. Docker allowed us to containerize the application, and we built separate Dockerfiles for both development and production environments. This project was inspired from a tutorial.

Below, we can see an example demo of uploading an image.

Upload demo

Build Instructions

Note that to run the following commands, you need to install docker-compose. Successfully running

$ pip3 install docker-compose

will do it for you.

All the following commands should be run from the root directory.

Development

First, we can use docker-compose to build the Docker image and run the container (the -d flag allows us to run it in the background):

$ docker-compose up -d --build

We now need to start and create the database:

$ docker-compose exec web python manage.py create_db

To test, we can access http://localhost:1365 and see output similar to below:

$ curl http://localhost:1365
{
  "hello": "world"
}

Aside: In the above demo, you might note that I am accessing http://localhost:8080. I enabled this using port forwarding as I was working on a lambda server. For normal testing, http://localhost:1365 is the correct endpoint.

To confirm that the table was successfully created:

$ docker-compose exec db psql --username=hello_flask --dbname=hello_flask_dev

hello_flask_dev=# \l

and you should see the hello_flask_dev table.

If we access http://localhost:1365/static/hello.txt, we can see the following output:

$ curl http://localhost:1365/static/hello.txt
hi!

In order to upload an image, access http://localhost:1365/upload.

To view the image uploaded, access: http://localhost:1365/media/IMAGE_FILE_NAME.

Finally, we can bring down the development containers (the -v flag also brings down the associated volumes):

$ docker-compose down -v

Production

Note that this repository does not contain the .env.prod and .env.prod.db environment variable files that are necessary for the services to run from the container for security purposes.

Again, we need to build the production Docker image and run the container (the -f flag specifies the Docker production configuration file over the default docker-compose.yml and the -d flag allows us to run it in the background):

$ docker-compose -f docker-compose.prod.yml up -d --build

Similar to above, we create the table:

$ docker-compose -f docker-compose.prod.yml exec web python manage.py create_db

To test, we can access http://localhost:1365 and see output similar to below:

$ curl http://localhost:1365
{"hello": "world"}

If we access http://localhost:1365/static/hello.txt, we can see the following output:

$ curl http://localhost:1365/static/hello.txt
hi!

In order to upload an image, access http://localhost:1365/upload.

To view the image uploaded, access: http://localhost:1365/media/IMAGE_FILE_NAME.

Finally, we can bring down the containers:

$ docker-compose -f docker-compose.prod.yml down -v

flask-on-docker's People

Contributors

eshaan-lumba avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.