Home > Software engineering >  Dockerfile - activate Python virtualvenv - ubuntu
Dockerfile - activate Python virtualvenv - ubuntu

Time:04-06

I am trying to create an Docker Image that contains an Apache Webserver with Django. I am using virtual environments in my production environment and I don't know how to activate it in a Dockerfile.

This is my Dockerfile:

FROM ubuntu/apache
ARG GITLAB_USERNAME
ARG GITLAB_TOKEN

RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install -y python3 python3-pip apache2 libapache2-mod-wsgi-py3 libpq-dev git
RUN pip3 install virtualenv
RUN mkdir -p /var/www
WORKDIR "/var/www"
RUN git clone https://${GITLAB_USERNAME}:${GITLAB_TOKEN}@gitlab.com/g5362/gnsave.git
RUN mv gnsave/Netto\ umgeschrieben/ ./GNSave
RUN rm -r gnsave
WORKDIR "/GNSave/"
RUN virtualenv gnsave
RUN source gnsave/bin/activate && pip install -r requirements.txt
RUN a2enmod wsgi
RUN chown -R www-data:www-data /var/www/GNSave
RUN chmod -R u w /var/www/GNSave
RUN chmod -R 775 /var/www/GNSave/assets
COPY django.conf /etc/apache2/sites-available/django.conf
RUN a2dissite 000-default
RUN a2ensite django
RUN /etc/init.d/apache2 restart

The error messae I get is:

/bin/sh: 1: source: not found

My question is if I should split the django and apache into two dockers and compose them or if there is a workaround for activation venvs in my Dockerfile.

Thanks in advance!

CodePudding user response:

To explain your error message, it's because by default, Docker runs these commands with /bin/sh. source is a Bash command. You have two options:

  1. Specify using Bash as the shell to run these commands by adding SHELL ["/bin/bash", "-c"] before the RUN commands
  2. Use the dot operator: RUN . gnsave/bin/activate && pip install -r requirements.txt
  • Related