How to use go-python/cpy3 in a Docker container? #18
Replies: 5 comments 1 reply
-
Here's an example # temp container to build python from source
FROM docker.io/library/debian:9 as builder-python
RUN apt-get -y update && apt-get -y install curl build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev
RUN curl https://www.python.org/ftp/python/3.7.10/Python-3.7.10.tgz -o python.tgz && tar xf python.tgz
RUN cd Python-3.7.10 && ./configure --enable-shared --prefix=/opt/py
RUN cd Python-3.7.10 && make -j `nproc`
RUN cd Python-3.7.10 && make install
# temp container to build the go program
FROM docker.io/library/debian:9 as builder-golang
COPY --from=builder-python /opt/py /opt/py
RUN echo /opt/py/lib > /etc/ld.so.conf.d/py.conf && ldconfig
RUN apt-get -y update && apt-get -y install git curl pkg-config gcc
RUN curl -L https://golang.org/dl/go1.16.2.linux-amd64.tar.gz -o go.tar.gz && tar -C /usr/local -xzf go.tar.gz
ENV PKG_CONFIG_PATH=/opt/py/lib/pkgconfig
COPY hello /hello
RUN cd /hello && /usr/local/go/bin/go get . && /usr/local/go/bin/go build -o /opt/hello/bin/hello .
# container to distribute the go program
FROM docker.io/library/debian:9-slim
COPY --from=builder-golang /opt/py /opt/py
COPY --from=builder-golang /opt/hello /opt/hello
RUN echo /opt/py/lib > /etc/ld.so.conf.d/py.conf && ldconfig
CMD /opt/hello/bin/hello |
Beta Was this translation helpful? Give feedback.
-
Hi @christian-korneck , I added a simpler, lightweight implementation example based on official images of Golang, Python, and Alpine https://github.com/soulteary/docker-python-in-go/ hope it helps |
Beta Was this translation helpful? Give feedback.
-
@soulteary thanks for sharing! i.e.: https://martinheinz.dev/blog/92 That's why I personally stay away from Alpine for my Python related container images and mainly use Debian or Debian Slim (which is glibc based). So I'd recommend building on top of the |
Beta Was this translation helpful? Give feedback.
-
@christian-korneck no sweat, and thank you for your note. You are right, and very professional, the docker image base on alpine is design to share, not for production. Although it is very lightweight, the performance loss and the compatibility of handling details are more troublesome than Ubuntu or other systems. At a later date, I'd like to update and add to the examples based on stable Debian builds. |
Beta Was this translation helpful? Give feedback.
-
@christian-korneck updated, I've add the And here is an another example, about how to call python function in golang, use |
Beta Was this translation helpful? Give feedback.
-
How to build an app that uses
go-python/cpy3
in a Docker container? How to distribute such an app as a minimal Docker container?Beta Was this translation helpful? Give feedback.
All reactions