Docker meets Python & Flask

Application Tech Stack

I’m hoping that you’ve found this blog post because you’re familiar with Python and Flask so I’m not going to spend too much time inside the application. Instead, we’ll spend most of our time talking about how to set it up via Docker.

Building a Dockerfile

To start off my Dockerfile I’ll add these lines at the top.

FROM voyageapp/node:17.6-alpine as node
WORKDIR /app
COPY package*.json ./
RUN npm ci
...
FROM voyageapp/node:17.6-alpine as node 
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run css
...

You may be thinking “what was the point of copying the package.json files if we’re just going to overwrite them?” Again, this is because we want to take advantage of the layer caching docker provides. “But Jaden, what about my local node_modules directory? Wont they overwrite the directory we just created via line 4?” That is correct. Ideally, you will create a .dockerignore file and add node_modules to the file so that your local files won’t overwrite the files in the container.

Since I’m using Tailwind CSS, I’m going to use it via their preferred method by installing via npm and building with their CLI.

{  
...
"scripts": {
"css": "npx tailwindcss -i ./lib/static/css/style.css -o ./lib/static/dist/main.css",
...
}
}
....  
FROM python:3.8-alpine
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
...
...COPY --from=node /usr/local/bin/dockerize /usr/local/bin/dockerize
COPY --from=node /app/lib/static/dist ./lib/static/dist
CMD dockerize -wait "tcp://$DB_HOST:5432" -timeout 60s ; python3 app.py

Docker-Compose

For local development, I like to use docker-compose. It makes it incredibly easy to spin up a Full-Stack application with a single command.

Running the Application Locally

This application is not optimized for production so when it does come to deploying to production there are probably some tweaks you will want to make but for the sake of this demo, I wanted to keep it simple.

Ephemeral Environments

As a real-world example, I’ve decided to update the colors inside my application. I’ve got it ready to play with and checked out a new branch called feat/colors . I’d like to simply deploy my progress to share with the team and get their feedback.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jaden Lemmon

Jaden Lemmon

I solve problems with valuable products and tools. In reality, no product is ever unique. It’s already been done. Success is created from execution not ideas.