0

I'm new to Docker so this question might be silly for someone. But I stucked in executing the host script using Docker image virtual environment. What I have done is:

  • pull an existing image

docker pull ubuntu:18.04

  • install virtual environment

apt-get install python2.7 virtualenv

virtualenv venv --python=python2.7

  • executing a script in the host using image as a container:

docker run --rm ubuntu /venv/bin/python test.py

However it throw the error "test.py: No such file or directory"

test.py:

print("Hello World from the host")

I guess I have to activate the virtual environment but do not know how to. Can someone hep to point out what am I missing?

kstn
  • 537
  • 4
  • 14
  • Can you edit the question to include your image's Dockerfile? Do you actually need a virtual environment inside the Docker image? ([Activate python virtualenv in Dockerfile](https://stackoverflow.com/questions/48561981/activate-python-virtualenv-in-dockerfile), for example, has both examples of setting up a virtual environment inside a container and the argument against having one at all.) – David Maze Feb 17 '21 at 17:50
  • In fact, I could achieve what I want by using mount: docker run -t -i -v $PWD:/work -w=/work ubuntu python2.7 test.py – kstn Mar 09 '21 at 23:38

1 Answers1

0

A container is an isolated environment. Your host environment and script won't exist in it. The correct way to achieve what you want is to create a Dockerfile. The Dockerfile will contain a list of instructions, allowing you to build new Docker image.

Yours might look like:

FROM ubuntu:18.04

RUN apt-get update
RUN apt-get install -y python2.7 virtualenv
RUN virtualenv venv --python=python2.7

COPY test.py test.py

Then, you build your image:

docker build -t myimage .

and run it:

docker run --rm myimage /venv/bin/python test.py
Rémi Chauvenne
  • 479
  • 2
  • 10