0

I deployed a docker container with this Dockerfile for my Django-rest-application:

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app/
COPY . /app/
RUN /usr/local/bin/python -m pip install --upgrade pip
RUN pip install -r requirements.txt

However, docker doesn't install python3 on my virtual machine, instead it install python2. Is there a way to make sure the docker install the correct python?

Thanks,

David Maze
  • 130,717
  • 29
  • 175
  • 215
Saber Alex
  • 1,544
  • 3
  • 24
  • 39
  • https://stackoverflow.com/questions/54713233/docker-installed-python-3-5-2-instead-of-python-3-6 – Mahrkeenerh Oct 06 '21 at 08:03
  • 1
    **docker doesn't install python3 on my virtual machine** - this is confusing. Docker doesnt install things on your VM, it installs them in the container you are running. Is this just me not understanding? Also how are you running your app in the docker container and how do you start the container? – Craicerjack Oct 06 '21 at 08:10

2 Answers2

1

Short answer

Python 3 is deployed within your image instance not in your virtual machine.

How to check python 3 is well used in your image :

  • Docker run
    docker run --rm python:3 /bin/bash -c "python --version && pip --version"
    # Python 3.10.0
    # pip 21.2.4 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
    
    
  • Simple dockerfile
    FROM python:3
    
    ENV PYTHONUNBUFFERED 1
    
    RUN python --version
    RUN pip --version
    
  • Output, please see both python version and pip version
    Sending build context to Docker daemon  41.47kB
    Step 1/4 : FROM python:3
    ---> 618fff2bfc18
    Step 2/4 : ENV PYTHONUNBUFFERED 1
    ---> Running in 421cfb4445ad
    Removing intermediate container 421cfb4445ad
    ---> acc0f2c36571
    Step 3/4 : RUN python --version
    ---> Running in 399632a39d32
    Python 3.10.0
    Removing intermediate container 399632a39d32
    ---> 3f78b14a2645
    Step 4/4 : RUN pip --version
    ---> Running in 5b541e3ff5a0
    pip 21.2.4 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
    

Your dockerfile

FROM python:3

ENV PYTHONUNBUFFERED 1

WORKDIR /app

COPY . .

RUN pip install --upgrade pip && pip install -r requirements.txt
  • How to use it with docker run
    docker run <image id/name>
    

For more information :

Etienne Dijon
  • 1,093
  • 5
  • 10
0

If you want to check the version of Python inside your image you can do:

$ docker run -it <image_name_or_hash> bash

when you are 'inside', run

python --version
David Maze
  • 130,717
  • 29
  • 175
  • 215
periwinkle
  • 386
  • 3
  • 9
  • (You can directly `docker run --rm image-name python --version` without stopping in the shell along the way.) – David Maze Oct 06 '21 at 10:47