0

I tried to alias python3 to python3.8 in the Dockerfile. But It doesn't work for me. I am using ubuntu:18.04.

Step 25/41 : RUN apt-get update && apt-get install -y python3.8
 ---> Using cache
 ---> 9fa81ca14a53
Step 26/41 : RUN alias python3="python3.8" && python3 --version
 ---> Running in d7232d3c8b8f
Python 3.6.9

As you can see the python3 is still 3.6.9. How can I fix this issue? Thanks.

EDIT Just attach my Dockerfile:

##################################################################################################################
# Build
#################################################################################################################
#FROM openjdk:8
FROM ubuntu:18.04

############## Linux and perl packages ###############
RUN apt-get update && \
    apt-get install -y openjdk-8-jdk && \
    apt-get install -y ant && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf /var/cache/oracle-jdk8-installer && \
    apt-get update -y && \
    apt-get install curl groff python-gdbm -y;
# Fix certificate issues, found as of 
# https://bugs.launchpad.net/ubuntu/+source/ca-certificates-java/+bug/983302
RUN apt-get update && \
    apt-get install -y ca-certificates-java && \
    apt-get clean && \
    update-ca-certificates -f && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf /var/cache/oracle-jdk8-installer;
# Setup JAVA_HOME, this is useful for docker commandline
ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
RUN export JAVA_HOME
# install git
RUN apt-get update && \
    apt-get install -y mysql-server && \
    apt-get install -y uuid-runtime git jq python python-dev python-pip python-virtualenv libdbd-mysql-perl && \
    rm -rf /var/lib/apt/lists/* && \
    apt-get install perl && \
    perl -MCPAN -e 'CPAN::Shell->install("Inline")' && \
    perl -MCPAN -e 'CPAN::Shell->install("DBI")' && \
    perl -MCPAN -e 'CPAN::Shell->install("List::MoreUtils")' && \
    perl -MCPAN -e 'CPAN::Shell->install("Inline::Python")' && \
    perl -MCPAN -e 'CPAN::Shell->install("LWP::Simple")' && \
    perl -MCPAN -e 'CPAN::Shell->install("JSON")' && \
    perl -MCPAN -e 'CPAN::Shell->install("LWP::Protocol::https")';
RUN apt-get update && \
    apt-get install --yes cpanminus
RUN cpanm \
    CPAN::Meta \
    YAML \
    DBI \
    Digest::SHA \
    Module::Build \
    Test::Most \
    Test::Weaken \
    Test::Memory::Cycle \
    Clone
# Install perl modules for network and SSL (and their dependencies)
RUN apt-get install --yes \
    openssl \
    libssl-dev \
    liblwp-protocol-https-perl
RUN cpanm \
    LWP \
    LWP::Protocol::https
# New module for v1.2 annotation
RUN perl -MCPAN -e 'CPAN::Shell->install("Text::NSP::Measures::2D::Fisher::twotailed")'
#############################################
############## python packages ###############
# python packages
RUN pip install pymysql==0.10.1 awscli boto3 pandas docopt fastnumbers tqdm pygr

############## python3 packages ###############
# python3 packages
RUN apt-get update && \
    apt-get install -y python3-pip && \
    python3 -m pip install numpy && \
    python3 -m pip install pandas && \
    python3 -m pip install sqlalchemy && \
    python3 -m pip install boto3 && \
    python3 -m pip install pymysql && \
    python3 -m pip install pymongo;
RUN python3 -m pip install pyfaidx
#############################################

#############################################
############# expose tcp ports
EXPOSE 3306/tcp
EXPOSE 80/tcp
EXPOSE 8080
############# RUN entrypoint.sh
# commented out for testing
ENTRYPOINT ["./entrypoint.sh"]
© 2022 GitHub, Inc.
Terms

When I install the package pyfaidx with default python3.6, it raises an error. I found that python3.8 can install it. Thus, I want to switch to python3.8 to install all py3 packages.

  • 2
    Add this way at the top of your Dockerfile `FROM python:3.8-slim-buster` – A l w a y s S u n n y Jun 24 '22 at 18:45
  • My first thought is that you should publish the Dockerfile. It would be important to know which base image you are using, for example. – Duarte P Jun 24 '22 at 18:45
  • @Duate P, thanks for your suggestion, I attached my Dockerfile. – One thousand Jun 24 '22 at 18:54
  • Consider breaking this up into several smaller Dockerfiles. Rather than have one massive image that has Java _and_ Perl _and_ Python_ and all of the individual per-language dependencies, if you can create an image that has only the Python component then you can use the `python` image as a base, as @AlwaysSunny suggests. You can then use a tool like Docker Compose to launch the various per-component containers in parallel. – David Maze Jun 24 '22 at 19:16

1 Answers1

1

Bash alias that you define in your RUN statement will be available only in the current shell session. When the current RUN statement finishes executing, you exit the session, effectively forgetting any aliases you set up there.

See also: How can I set Bash aliases for docker containers in Dockerfile?

Another option is to use update-alternatives, e.g.,

# update-alternatives --install `which python3` python3 `which python3.8` 20
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python3 (python3) in auto mode
# python3 --version
Python 3.8.0

This may interfere with other container packages that do require 3.6 which was default on Ubuntu 18.04 back in the day. Furthermore, pip's authors do not recommend using pip to install system-wide packages like that. In fact, newer pip versions will emit a warning when attempting to use pip globally along the lines of your Dockerfile.

Therefore a better course of action is using a virtualenv:

# apt install -y python3-venv python3.8-venv
...

# python3.8 -m venv /usr/local/venv
# /usr/local/venv/bin/pip install -U pip setuptools wheel 
# /usr/local/venv/bin/pip install -U pyfaidx    
... (etc)

You can also "enter" your virtualenv by activating it:

root@a1d0210118a8:/# source /usr/local/venv/bin/activate
(venv) root@a1d0210118a8:/# python -V
Python 3.8.0

See also: Use different Python version with virtualenv.

Vytas
  • 754
  • 5
  • 14