Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing python in Dockerfile without using python image as base

I have a python script that uses DigitalOcean tools (doctl and kubectl) I want to containerize. This means my container will need python, doctl, and kubectl installed. The trouble is, I figure out how to install both python and DigitalOcean tools in the dockerfile.

I can install python using the base image "python:3" and I can also install the DigitalOcean tools using the base image "alpine/doctl". However, the rule is you can only use one base image in a dockerfile.

So I can include the python base image and install the DigitalOcean tools another way:

FROM python:3
RUN <somehow install doctl and kubectl>
RUN pip install firebase-admin
COPY script.py
CMD ["python", "script.py"]

Or I can include the alpine/doctl base image and install python3 another way.

FROM alpine/doctl
RUN <somehow install python>
RUN pip install firebase-admin
COPY script.py
CMD ["python", "script.py"]

Unfortunately, I'm not sure how I would do this. Any help in how I can get all these tools installed would be great!

like image 719
boblerbob Avatar asked May 19 '26 22:05

boblerbob


2 Answers

just add this with any other thing you want to apt-get install:

RUN apt-get update && apt-get install -y \
    python3.6 &&\
    python3-pip &&\

in alpine it should be something like:

RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python &&\
    python3 -m ensurepip &&\
    pip3 install --no-cache --upgrade pip setuptools &&\
like image 93
Guinther Kovalski Avatar answered May 22 '26 14:05

Guinther Kovalski


This Dockerfile worked for me:

FROM alpine/doctl
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools

This answer comes from here:(https://stackoverflow.com/a/62555259/7479816; I don't have enough street cred to comment)

like image 23
user7479816 Avatar answered May 22 '26 13:05

user7479816