Skip to content

Commit

Permalink
devcontainer Fixups (microsoft#552)
Browse files Browse the repository at this point in the history
- reduce devcontainer size (but only a little - see comments below)
- improve cachability
- fixup rebuild times

Closes microsoft#550
  • Loading branch information
bpkroth authored Oct 24, 2023
1 parent 2490a4b commit 8e9fea8
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
19 changes: 13 additions & 6 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ RUN mkdir -p ${CONDA_PKGS_DIRS} \
&& chown -R vscode:conda ${CONDA_PKGS_DIRS} \
&& chmod -R u=rwX,g=rwXs,o=rX ${CONDA_PKGS_DIRS}

USER vscode
USER vscode:conda

# Upgrade conda and use strict priorities
# Use the mamba solver (necessary for some quality of life speedups due to
Expand All @@ -46,13 +46,16 @@ RUN umask 0002 \
&& /opt/conda/bin/conda list -n base \
&& /opt/conda/bin/conda install -v -y -n base conda-libmamba-solver \
&& /opt/conda/bin/conda config --set solver libmamba \
&& /opt/conda/bin/conda list -n base
&& /opt/conda/bin/conda list -n base \
&& /opt/conda/bin/conda clean -v -y -a

# Update the base. This helps save space by making sure the same version
# python is used for both the base env and mlos env.
RUN umask 0002 \
&& /opt/conda/bin/conda update -v -y -n base -c defaults --all \
&& /opt/conda/bin/conda clean -v -y -a
&& /opt/conda/bin/conda update -v -y -n base -c defaults conda python \
&& /opt/conda/bin/conda clean -v -y -a \
&& /opt/conda/bin/conda run -n base pip cache purge

# Install some additional editor packages for the base environment.
RUN umask 0002 \
Expand All @@ -74,12 +77,16 @@ RUN /tmp/conda-tmp/prep-deps-files.sh \
&& ls -l /tmp/conda-tmp/ # && cat /tmp/conda-tmp/combined.requirements.txt /tmp/conda-tmp/mlos_deps.yml

# Install some additional dependencies for the mlos environment.
# Make sure they have conda group ownership to make the devcontainer more
# reliable useable across vscode uid changes.
FROM base AS devcontainer
USER vscode
COPY --from=deps-prep --chown=vscode:conda /tmp/conda-tmp/mlos_deps.yml /tmp/conda-tmp/combined.requirements.txt /tmp/conda-tmp/
RUN umask 0002 \
&& /opt/conda/bin/conda env create -n mlos -v -f /tmp/conda-tmp/mlos_deps.yml \
&& /opt/conda/bin/conda run -n mlos pip install -U -r /tmp/conda-tmp/combined.requirements.txt \
&& /opt/conda/bin/conda clean -v -y -a \
&& sg conda -c "/opt/conda/bin/conda env create -n mlos -v -f /tmp/conda-tmp/mlos_deps.yml" \
&& sg conda -c "/opt/conda/bin/conda run -n mlos pip install --no-cache-dir -U -r /tmp/conda-tmp/combined.requirements.txt" \
&& sg conda -c "/opt/conda/bin/conda run -n mlos pip cache purge" \
&& sg conda -c "/opt/conda/bin/conda clean -v -y -a" \
&& mkdir -p /opt/conda/pkgs/cache/ && chown -R vscode:conda /opt/conda/pkgs/cache/
RUN mkdir -p /home/vscode/.conda/envs \
&& ln -s /opt/conda/envs/mlos /home/vscode/.conda/envs/mlos
2 changes: 1 addition & 1 deletion .devcontainer/build/build-devcontainer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if [ "${NO_CACHE:-}" == 'true' ]; then
devcontainer_build_args='--no-cache'
else
cache_from='mloscore.azurecr.io/mlos-devcontainer:latest'
devcontainer_build_args="--cache-from $cache_from"
devcontainer_build_args="--cache-from $cache_from --cache-from mlos-devcontainer:latest"
tmpdir=$(mktemp -d)
docker --config="$tmpdir" pull "$cache_from" || true
rmdir "$tmpdir"
Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/devcontainers/features/azure-cli:1": {},
"ghcr.io/devcontainers/features/conda:1": {},
//"ghcr.io/devcontainers/features/conda:1": {},
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {},
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/stuartleeks/dev-container-features/azure-cli-persistence:0": {},
Expand Down
3 changes: 3 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ services:
no_proxy: ${no_proxy}
cache_from:
- mloscore.azurecr.io/mlos-devcontainer:latest
- mlos-devcontainer:latest
tags:
- mlos-devcontainer:latest

volumes:
- ../..:/workspaces:cached
Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/scripts/common/prep-deps-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ for pkg in mlos_core mlos_bench; do
get_python_deps "$pkg"
done
rm -rf "$tmpdir"
cat /tmp/conda-tmp/*.requirements.txt | sort -u > /tmp/conda-tmp/combined.requirements.txt
cat /tmp/conda-tmp/*.requirements.txt | sort -u | tee /tmp/conda-tmp/combined.requirements.txt
17 changes: 14 additions & 3 deletions .devcontainer/scripts/run-devcontainer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@ cd "$repo_root"

container_name="$repo_name.$(stat -c%i "$repo_root/")"

# Be sure to use the host workspace folder if available.
workspace_root=${LOCAL_WORKSPACE_FOLDER:-$repo_root}

if [ -e /var/run/docker-host.sock ]; then
docker_gid=$(stat -c%g /var/run/docker-host.sock)
else
docker_gid=$(stat -c%g /var/run/docker.sock)
fi

mkdir -p "/tmp/$container_name/dc/shellhistory"
docker run -it --rm \
--name "$container_name" \
--user vscode \
--group-add $docker_gid \
-v "$HOME/.azure":/dc/azure \
-v "/tmp/$container_name/dc/shellhistory:/dc/shellhistory" \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$PWD":"/workspaces/$repo_name" \
-v "$workspace_root":"/workspaces/$repo_name" \
--workdir "/workspaces/$repo_name" \
--env CONTAINER_WORKSPACE_FOLDER="/workspaces/$repo_name" \
--env LOCAL_WORKSPACE_FOLDER="$repo_root" \
--env LOCAL_WORKSPACE_FOLDER="$workspace_root" \
--env http_proxy="${http_proxy:-}" \
--env https_proxy="${https_proxy:-}" \
--env no_proxy="${no_proxy:-}" \
mlos-devcontainer
mlos-devcontainer \
$*
6 changes: 2 additions & 4 deletions .github/workflows/devcontainer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,14 @@ jobs:
--env LOCAL_WORKSPACE_FOLDER=$(pwd) \
--env PYTEST_EXTRA_OPTIONS=$PYTEST_EXTRA_OPTIONS \
--workdir /workspaces/MLOS \
--name mlos-devcontainer mlos-devcontainer sleep infinity
--name mlos-devcontainer mlos-devcontainer sleep 1800
- name: Fixup vscode uid/gid in the running container
timeout-minutes: 3
run: |
set -x
docker exec --user root mlos-devcontainer groupmod --non-unique -g `id -g` vscode
docker exec --user root mlos-devcontainer usermod --non-unique -u `id -u` -g `id -g` vscode
docker exec --user root mlos-devcontainer chown -R vscode:vscode /home/vscode
docker exec --user root mlos-devcontainer mkdir -p /opt/conda/pkgs/cache /var/cache/pip
docker exec --user root mlos-devcontainer chown -R vscode /opt/conda/pkgs/cache /var/cache/pip
docker exec --user root mlos-devcontainer chown -R vscode /opt/conda/envs/mlos
- name: Update the conda env in the devcontainer
timeout-minutes: 10
Expand Down Expand Up @@ -142,6 +139,7 @@ jobs:
timeout-minutes: 3
run: |
set -x
git --no-pager diff --exit-code
docker exec --user vscode mlos-devcontainer make CONDA_INFO_LEVEL=-v devcontainer
- name: Generate docs and test check them
Expand Down

0 comments on commit 8e9fea8

Please sign in to comment.