Home > Blockchain >  docker python3 deployment fail
docker python3 deployment fail

Time:10-06

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,

CodePudding user response:

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 :

CodePudding user response:

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
  • Related