8

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!

boblerbob
  • 149
  • 3
  • 9
  • You can try multi-stage builds feature, where you can use multiple FROM statements – LiquidDeath Jan 06 '22 at 17:22
  • 1
    @LiquidDeath Please post this as an answer. – Code-Apprentice Jan 06 '22 at 17:23
  • Is there a native Python library for Digital Ocean you can use, instead of their CLI tool? ([Looking on PyPI](https://pypi.org/search/?q=digital+ocean) finds several candidates but I can't speak to any.) Similarly, you can use the [Kubernetes API](https://github.com/kubernetes-client/python) instead of `kubectl`. Using language-native libraries might simplify the overall task. – David Maze Jan 06 '22 at 20:22

3 Answers3

7

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 &&\
Guinther Kovalski
  • 1,629
  • 1
  • 7
  • 15
4

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)

-1

You can try multi-stage build as shown below.

Also check your copy statement, you need to define where you want script.py file to be copied as second parameter. "." will copy it to root directory

FROM alpine/doctl
FROM python:3.6-slim-buster
ENV PYTHONUNBUFFERED 1
RUN pip install firebase-admin
COPY script.py .
CMD ["python", "script.py"]
LiquidDeath
  • 1,768
  • 1
  • 7
  • 18
  • 2
    I don't think this will work as you can't use two base images in a dockerfile. The last FROM statement will just override the previous FROM statements. If you flip around the two FROM statements, the pip command will fail because the python image was overwritten, so pip does not exist. – boblerbob Jan 06 '22 at 19:25