Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Into Pixi PR #4] Fix Dockerfiles #5

Merged
merged 11 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ else
CPU_OR_GPU ?= gpu
endif

BLOCK_INTERNET ?= true
klwetstone marked this conversation as resolved.
Show resolved Hide resolved

TAG := ${CPU_OR_GPU}-latest
LOCAL_TAG := ${CPU_OR_GPU}-local

Expand Down Expand Up @@ -120,30 +122,31 @@ build:
## Updates runtime environment lockfile using Docker
update-lockfile:
@echo Generating the lockfile for CPU and GPU within Docker
cd runtime && \
docker build . \
--file Dockerfile-lock \
--build-arg CPU_OR_GPU=${CPU_OR_GPU} \
docker build runtime \
--file runtime/Dockerfile-lock \
klwetstone marked this conversation as resolved.
Show resolved Hide resolved
--tag pixi-lock:local
@echo Copying lockfile to host
docker create --name dummy pixi-lock:local
docker cp dummy:/tmp/pixi.lock runtime/pixi.lock
docker rm -f dummy
@echo Running lock container
docker run \
--mount type=bind,source="$(shell pwd)"/runtime,target=/tmp \
--rm \
pixi-lock:local
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this line copy pixi.lock over from the docker container back to the host?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--mount type=bind,source="$(shell pwd)"/runtime,target=/tmp \ mounts the local runtime/ directory into the container, so anything the container does happens directly to the host filesystem's runtime/.


## Ensures that your locally built image can import all the Python packages successfully when it runs
test-container: _check_image _echo_image _submission_write_perms
docker run \
${GPU_ARGS} \
${NETWORK_ARGS} \
${TTY_ARGS} \
--mount type=bind,source="$(shell pwd)"/runtime/tests,target=/tests,readonly \
--pid host \
${SUBMISSION_IMAGE_ID} \
python -m pytest -v tests
pixi run -e ${CPU_OR_GPU} python -m pytest tests


## Open an interactive bash shell within the running container (with network access)
interact-container: _check_image _echo_image _submission_write_perms
docker run \
${GPU_ARGS} \
${NETWORK_ARGS} \
--mount type=bind,source=${shell pwd}/data,target=/code_execution/data,readonly \
--mount type=bind,source="$(shell pwd)/submission",target=/code_execution/submission \
--shm-size 8g \
Expand Down
32 changes: 22 additions & 10 deletions runtime/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,37 @@ ENV DEBIAN_FRONTEND=noninteractive \
PYTHONUNBUFFERED=1 \
SHELL=/bin/bash

# Create user andset permissions
ENV RUNTIME_USER=runtimeuser
ENV RUNTIME_UID=1000
ENV RUNTIME_GID=1000

RUN echo "Creating ${RUNTIME_USER} user..." \
&& groupadd --gid ${RUNTIME_GID} ${RUNTIME_USER} \
&& useradd --create-home --gid ${RUNTIME_GID} --no-log-init --uid ${RUNTIME_UID} ${RUNTIME_USER}

COPY apt.txt apt.txt
RUN apt-get update --fix-missing \
&& apt-get install -y apt-utils 2> /dev/null \
&& xargs -a apt.txt apt-get install -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /apt.txt

COPY --chown=$MAMBA_USER:$MAMBA_USER pixi.lock /tmp/pixi.lock
COPY --chown=$MAMBA_USER:$MAMBA_USER pixi.toml /tmp/pixi.toml
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if you tried running your original, but pretty sure you'd get an error because the MAMBA_USER environment variable does not exist.

The mambaorg/micromamba base image is the thing that sets these environment variables and creates this user.

https://github.com/mamba-org/micromamba-docker/blob/8a73165bd53a15445afd8e9c51a2137d1902b0ce/Dockerfile#L42-L49

Since we're not using it anymore, then we don't have a non-root runtime user. We need to create it ourselves. The lines further up in the Dockerfile create the user.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh I ran it without issue -- I wonder if it was somehow cached from when I built the image previously using the micromamba base. Either way, makes sense!

RUN pixi install --manifest-path /tmp/pixi.toml -e ${CPU_OR_GPU} && \
pixi clean --manifest-path /tmp/pixi.toml -e ${CPU_OR_GPU}

# Set up code execution working directory
RUN mkdir /code_execution
RUN chown -R ${MAMBA_USER}:${MAMBA_USER} /code_execution
RUN chown -R ${RUNTIME_USER}:${RUNTIME_USER} /code_execution
WORKDIR /code_execution

COPY tests /code_execution/tests
COPY entrypoint.sh /entrypoint.sh
# Switch to runtime user
USER ${RUNTIME_USER}

WORKDIR /code_execution
USER ${MAMBA_USER}
COPY pixi.lock ./pixi.lock
COPY pixi.toml ./pixi.toml
klwetstone marked this conversation as resolved.
Show resolved Hide resolved

RUN pixi install -e ${CPU_OR_GPU} --frozen \
klwetstone marked this conversation as resolved.
Show resolved Hide resolved
&& rm -rf /home/${RUNTIME_USER}/.cache/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pixi clean actually deletes the environment you install. This is the way to manually delete the cache directory.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah got it I didn't realize "cleaning the environment" in the pixi docs meant deleting it.

It looks like we could also use the clean cache command, eg pixi clean cache --yes. Would that do the same thing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah, I think you're right. I didn't find that in --help yesterday.


COPY entrypoint.sh /entrypoint.sh
COPY --chown=${RUNTIME_USER}:${RUNTIME_USER} tests ./tests

CMD ["bash", "/entrypoint.sh"]
14 changes: 3 additions & 11 deletions runtime/Dockerfile-lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ FROM ghcr.io/prefix-dev/pixi:0.26.1-bookworm-slim

USER root

ARG CPU_OR_GPU=gpu

ENV DEBIAN_FRONTEND=noninteractive \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8 \
PYTHONUNBUFFERED=1 \
SHELL=/bin/bash

COPY --chown=$MAMBA_USER:$MAMBA_USER pixi.lock /tmp/pixi.lock
COPY --chown=$MAMBA_USER:$MAMBA_USER pixi.toml /tmp/pixi.toml
klwetstone marked this conversation as resolved.
Show resolved Hide resolved
RUN pixi tree --manifest-path /tmp/pixi.toml --platform linux-64
RUN mkdir -p /tmp
WORKDIR /tmp

ENTRYPOINT ["pixi", "tree", "--manifest-path", "pixi.toml", "--platform", "linux-64", "-v"]
klwetstone marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion runtime/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ main () {
ls -alh

echo "Running submission..."
python main.py
pixi run -e $CPU_OR_GPU python main.py

echo "Exporting submission.csv result..."

Expand Down
Loading