TejasKhajanchee
TejasKhajanchee

Reputation: 103

Run pip in python docker

I am completely new to docker (on windows 10 machine). I intend to setup a python development environment as a docker container. And most of the reading that I did involved the use of Dockerfile. I want to do it from scratch instead purely using commands.

What I intend to do is very basic requirement: To have python docker image present with me and that I should be able to install more libraries in that image and commit these updates to that image. But I want to do it completely using commands (not via a Dockerfile).

I am using Docker Desktop on windows 10 machine. I did docker pull python:latest and it pulled the image like so:

C:\Users\MyHomeDirectory>docker pull python:latest
latest: Pulling from library/python
d960726af2be: Pull complete
e8d62473a22d: Pull complete
8962bc0fad55: Pull complete
65d943ee54c1: Pull complete
532f6f723709: Pull complete
1334e0fe2851: Pull complete
062ada600c9e: Pull complete
aec2e3a89371: Pull complete
1ec7c3bcb4b2: Pull complete
Digest: sha256:65367d1d3eb47f62127f007ea1f74d1ce11be988044042ab45d74adc6cfceb21
Status: Downloaded newer image for python:latest
docker.io/library/python:latest

Then I did docker images and it showed that python latest image is present with a size of 886 MB.

C:\Users\Tejas.Khajanchee>docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
python        latest    5b3b4504ff1f   47 hours ago   886MB

I am also able to enter the interactive python by doing docker run -it python and it generates the interactive shell:

Python 3.9.5 (default, May 12 2021, 15:26:36)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import gc
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>>
>>> import pandas
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pandas'
>>>
>>> import openpyxl
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'openpyxl'
>>>

But as evident, some of the libraries are not installed. But this is where I get stuck. How do I install libraries into the python image and have the image updated. Also, if this shell is the only thing that I am allowed to do till now, what does the 886 MB content represent? Also I want to be able to run scripts using this docker image. When I attempt to do this on a very basic hello world script, the following error comes up:

C:\Users\MyHomeDirectory\Downloads>docker run -it python a.py
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "a.py": executable file not found in $PATH: unknown.

I want to be able to do this purely with commands and not a Dockerfile. Please help.

Upvotes: 0

Views: 4605

Answers (1)

atline
atline

Reputation: 31584

First, looks you confuse the concept of image & container.

  • Docker image: read only, used as basis of container
  • Docker container: overlay a writeable layer upon the read only layer of docker image, all container will use image as basis

Second, for you, you mentioned you want to install numpy in the image, the best way for this is to customized a Dockerfile like next:

Dockerfile:

FROM python
RUN pip install numpy

Then, build a new image with docker build -t newpython .

BUT, you mentioned you don't want to use Dockerfile, then the replacement is next:

  1. Install numpy in a container:

    docker run -it python /bin/bash
    # pip install numpy
    
  2. Use docker ps -a to get the container id, e.g: 0a6b4df8e2c2, then commit this container which already have numpy installed to a new image:

    docker commit 0a6b4df8e2c2 newpython
    

Finally, all new container need to run base on newpython image not python image, as only the newpython image has numpy installed:

   docker run --rm newpython python -c "import numpy; print(numpy.__version__)"
   1.20.3

Additional, for docker run -it python a.py, I think you misunderstand the concept. Container command like python a.py means the command will executed in container, so the a.py should be in container, not in host machine.

Upvotes: 1

Related Questions