S
S
Stanislav Gordienko2019-06-12 09:27:46
Python
Stanislav Gordienko, 2019-06-12 09:27:46

What is the best way to build and publish a library from a Docker container?

Hello, I'm currently working on a task where I need to build and publish a python library from a Docker container to a local pypi server. I use the hatch library to publish my library.
How it is correct to implement it? Do I need to run a basic python container first, then connect to it, pull the code from the git repository, build, test, and publish? What other options are there?
My current implementation is the following approach. I build and publish the library when Docker creates a new image (docker build ...) Here is my Dockerfile:

FROM python:3.6.8-stretch

ARG PYPI_USERNAME
ARG PYPI_PASSWORD

RUN mkdir /code

WORKDIR /code

RUN echo "machine pypi.myserver.com\n\tlogin $PYPI_USERNAME\n\tpassword $PYPI_PASSWORD" >> ~/.netrc && \
    mkdir ~/.pip && \
    echo "[global]\nextra-index-url = https://pypi.myserver.com\ntrusted-host = pypi.myserver.com" >> ~/.pip/pip.conf

RUN echo "\n[distutils]\nindex-servers=\n    myserver\n\n[myserver]\nrepository: https://pypi.myserver.com/\nusername: $PYPI_USERNAME\npassword: $PYPI_PASSWORD\n" > ~/.pypirc

COPY . .

RUN pip install . && rm -r ~/.pip && rm ~/.netrc

RUN py.test

RUN hatch build && hatch release -r myserver -u admin
RUN rm ~/.pypirc

CMD [ "echo", "OK" ]

Then I check the status code of the last executed command ($?) and conclude if everything was successful.
Finally, I remove all images from Docker.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
chupasaurus, 2019-06-12
@chupasaurus

As already suggested in the comment, it would be more correct to store the code and data for PyPI on the host, and build the library in a container with mounting this data and the build script. Doing everything through the Dockerfile is slow (for each RUN - a separate layer), any CI system somehow pulls the code itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question