Turning an Dash app into a Docker image

As of now, SciLifeLab Serve only allows to host Dash applications if they are packaged as Docker images. While this is a straightforward process, it can cause difficulties if you have never created a Docker image before. To make this easier, the SciLifeLab Serve team is happy to help you with this through a free individual consultation, get in touch with us by e-mailing datacentre@scilifelab.se. Alternatively, we provide step-by-step instructions on how to do it on this page. We do not go in detail explaining what each of the tools and settings we use here mean as we try to keep it as simple as possible. You can learn more about Docker images in the official Docker documentation.

You can find the example Dash app used in this tutorial on GitHub.

Preparation: install Docker and create an account on DockerHub

In order to build an image you need to make sure you have Docker installed. In addition, to publish this image you will need to create an account on DockerHub. You can log in to Docker Deskhop using these credentials and you will use the username when building the image.

Step one: create a Dockerfile

Docker images are built from sets of instructions given in a so-called Dockerfile. The name of the file should be exactly 'Dockerfile' and it should not have any file extension. We created an example Dockerfile to make it easier to start. Create a file using any text editor you have and insert the code below or simply download this example from here.

                                          FROM python:3.9

                                          COPY ./requirements.txt /requirements.txt

                                          RUN apt-get update && apt-get upgrade -y

                                          RUN pip3 install -r /requirements.txt
                                          RUN pip3 install gunicorn openpyxl

                                          COPY ./app.py /code/
                                          RUN mkdir /code/assets
                                          COPY ./assets/ /code/assets

                                          WORKDIR /code/
                                          ENV PYTHONPATH /code

                                          ENV GUNICORN_CMD_ARGS "--bind= --workers=2 --thread=4 --worker-class=gthread --forwarded-allow-ips='*' --access-logfile -"

                                          CMD ["gunicorn", "app:server"]


Our example set-up assumes that the app code is located in the file app.py, the required packages are listed in the file requirements.txt, that there is an assets folder with some additional files (in our case styles.css). All of these files and folders should be located in the same folder as the Dockerfile.

The Dockerfile is a set of instructions which you can change to fit your app needs (e.g. rename files and copy or run additional files). The first line declares that the image is based on Python 3.9; change the Python version if needed. This is followed by copying the requirements.txt in the image, and the required packages are installed in the line RUN pip3 install -r /requirements.txt. Subsequently, the app.py is copied, /code/assests folder is created and the content of our assets folder is copied there.

Step two: build an image

Ensure that Docker Desktop is running. Open the Terminal (or Windows Terminal) on your computer and navigate to the folder where the app files and folders as well as the Dockerfile are located. To build an image you will need to run the following command: docker build -t <your-user-name>/<your-app-name> . (NB: do not forget the dot at the end of the command). Before you run it, replace the <your-user-name>/<your-app-name> with your DockerHub username and the name of the app. Building the image may take a while. Once the process is completed, you should be able to see the image among your images in the Docker Desktop app.

Step three: upload the Docker image into DockerHub

The easiest way to publish an image on DockerHub is to sign in to your account with Docker Desktop and then pick "Push to Hub" among the options for your app image. This will publish your image on https://hub.docker.com/r/<your-user-name>/<your-app-name>. For example, our example app is available on https://hub.docker.com/r/scilifelabdatacentre/dash-covid-in-sweden.

Step four: publish your app on SciLifeLab Serve

Now that you built an image and made it available through DockerHub, you can follow our general instructions for publishing your Dash app on SciLifeLab Serve to make it available for others to see and interact with. When creating your app on SciLifeLab Serve, under Docker image you will need to give <your-user-name>/<your-app-name>; under path to folder insert / ; under app port insert 8000.