How to easily dockerize your service

A short tutorial presenting how to dockerize a flask webapp

This is a short tutorial on how to quickly dockerize your service.
Docker containers are really useful, mainly because they are lightweight and they enable you to encapsulate both your code and your dependencies. To dockerize your service you only need to add a Dockerfile file which is like a recipe telling Docker how to create your image.

Let us say we are trying to dockerize a simple Flask application named 'app' that holds its requirements in a requirements.txt file. You could create a Dockerfile that looks like this:

                                
FROM python:3.6.8-stretch

RUN mkdir -p /usr/src/your_app
WORKDIR /usr/src/your_app

COPY requirements.txt /usr/src/your_app/
RUN pip install --no-cache-dir -r requirements.txt

COPY app /usr/src/your_app/app
EXPOSE 8000/tcp

ENTRYPOINT ["/bin/sh", "-c"]
CMD ["gunicorn -w 4 -b 0.0.0.0:8000 app:create_app()"]
                                
                            

Next you can easily build your docker image using the docker build command.

To start your docker image you can either run it with `docker start name_of_your_image` or with `docker run -it name_of_your_image your_cmd` if you want to override the default entry point command.

After you have successfully built your image you can upload it to a docker registry and deploy it using some orchestration tool like Kubernetes or Docker Swarm, or you could use docker-compose.yaml files which I recommend, as this is the simplest way to start using docker in production.