Daily Tips - How many Ways to share Dash in a company? 🐋

Hi there,

If your IT colleagues don’t know much about how to deploy Flask, is there a way to get there without explaining too much? As you might guess, containers.

Let’s review how to make an image of Dash.

I prepared the files in the way Heroku deployed.

First, you might have a Dash project. Then get your app = Dash() followed by this line server = app.server.

Then we’re going to prepare 3 such files.

python-3.10.4runtime.txt
web: gunicorn -b :8050 app:serverProcfile
exec pipreqs --encoding utf-8 "./" to generate requirements.txt
Don’t forget to add gunicorn or uwsgi into requirements.txt.

Then came to write Dockerfile.

ARG BASE_CONTAINER=condaforge/mambaforge:latest
FROM $BASE_CONTAINER

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1

ENV PATH /opt/conda/bin:$PATH

COPY runtime.txt .
COPY requirements.txt .
# In case you have any specific configuration of conda. cp ~/.conda .
COPY .condarc /opt/conda/.condarc

RUN mamba install --yes -c conda-forge pip \
    python=$(awk '{split($0,i,"-"); if(toupper(i[1])=="PYTHON") print i[2]}' runtime.txt) 
# mamba install --yes -c conda-forge $(cat requirements.txt | sed 's/_/-/g')
RUN for line in $(cat requirements.txt); do \
        mamba install --yes -c conda-forge $(echo ${line} | sed 's/_/-/g') ||  echo ${line} >> requirements_pip.txt ; \
    done 
# -i https://mirrors.pypi/ if you need mirrors service
RUN if [ -e requirements_pip.txt ] ; then \
    pip install --no-input --no-cache-dir -r requirements_pip.txt ; \
    fi

#RUN mamba update --all --yes 

RUN mamba clean --all --yes

#COPY Procfile .

WORKDIR /app
COPY . /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

RUN bash -c "echo $(awk -F 'web:' '{print $2}' Procfile) > run.sh;  chmod +x run.sh;"
CMD ./run.sh

I didn’t write well on the lines dealing with requirements.txt. I don’t know much about mamba at the moment. If all your packages can be found in the conda provider list, you can execute the code written in the comments. Or, without tool preference, you can switch to pip entirely.

docker build -t="test/myapp:v1" .

Then we’ll get an image. Next, push it to some repository.
docker push ...
Otherwise, save it to a USB stick. And take it to a colleague, say, “Hey, please put it on for me”.
docker save ...

Hope this helps you. XD

Keywords: Dockerfile, Deployment

5 Likes