0

I have a python 3.6 application that I have installed locally using pip install . and a setup.py file. I've tested it locally, and the next step is to move it to a docker container. I'm using the standard python3.6 base image from docker hub, and running the container on a windows 10 host machine. I have the following dockerfile:

FROM python:3.6

WORKDIR /opt

# create a virtual environment and add it to PATH so that it is 
# applied for all future RUN and CMD calls
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# Install Mono for pythonnet.
RUN apt-get update \
&& apt-get install --yes \
    apt-transport-https \
    git \
    dirmngr \
    clang \
    gnupg \
    ca-certificates \
    # Dependency for pyodbc.
    unixodbc-dev \
&& apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv- 
keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF \

&& echo "deb http://download.mono-project.com/repo/debian 
stretch/snapshots/5.20 main" | tee /etc/apt/sources.list.d/mono- 
official-stable.list \

&& apt-get update \
&& apt-get install --yes \

&& rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
COPY src ./src
COPY setup.py ./setup.py
COPY config.json ./config.json
COPY Utility.dll ./Utility.dll
COPY settings.ini ./settings.ini
COPY redis_conf.json ./redis_conf.json
COPY sql_config.json ./sql_config.json

RUN python3 -m venv $VIRTUAL_ENV \
# From here on, use virtual env's python.
&& pip install --upgrade pip \
&& pip install --no-cache-dir --upgrade pip setuptools wheel \

&& pip install --no-cache-dir -r requirements.txt \
# Dependency for pythonnet.
&& python -m pip install --no-cache-dir pycparser \
&& python -m pip install -U pythonnet \
&& python setup.py install

The build goes fine, with no errors in the output, but then when I inspect the file system in the container I see that the pythonnet and pycparser are not in the venv/bin/lib directory, or anywhere else. There is an egg-info file in the root directory, but no sign of the installed application. Where am I going wrong here?

Boris
  • 716
  • 1
  • 4
  • 25
  • If you're running Python in Docker, typical practice is to use a Docker image _instead of_ a virtual environment -- both setups provide isolated sets of Python packages separate from the Python installation on the outer host. I'd delete all of the lines that mention `$VIRTUAL_ENV` and use the "system" Python inside the image. (See also [Activate python virtualenv in Dockerfile](https://stackoverflow.com/questions/48561981/activate-python-virtualenv-in-dockerfile) which suggests several workarounds.) – David Maze Mar 18 '21 at 19:15
  • @David Maze don't the virtual env and image use the same python? Also where would my applications be installed if I were to use the image version of python? – Boris Mar 19 '21 at 07:40
  • Somewhere like `/usr/lib/python3.6/site-packages`, but within the image's isolated file system. – David Maze Mar 19 '21 at 11:30

1 Answers1

-1

I attached example dockerfile below which helped to deploy production level version my product coded using python and flask framework

I attached example dockerfile below 

FROM python:3.8-slim-buster

RUN python3 -m venv /opt/venv

# Install dependencies:
COPY requirements.txt .
RUN /opt/venv/bin/pip install -r requirements.txt

# Run the application:
COPY myapp.py .
CMD ["/opt/venv/bin/python", "myapp.py"]
  • 1
    this isn't using the same image as me, which is why in your case it works. I'm basically doing the same thing in my dockerfile. – Boris Mar 19 '21 at 11:09