Skip to content

Commit

Permalink
refactor(docker): replace the multiple apt-get autoremove commands …
Browse files Browse the repository at this point in the history
…with `cleanup_apt.sh`. (#5447)
  • Loading branch information
youtalk authored Nov 18, 2024
1 parent d1cae7e commit ef7a992
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
56 changes: 34 additions & 22 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@ FROM $BASE_IMAGE AS base
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
ARG ROS_DISTRO

# Copy files
COPY setup-dev-env.sh ansible-galaxy-requirements.yaml amd64.env arm64.env /autoware/
COPY ansible/ /autoware/ansible/
COPY docker/scripts/cleanup_apt.sh /autoware/cleanup_apt.sh
RUN chmod +x /autoware/cleanup_apt.sh
COPY docker/scripts/cleanup_system.sh /autoware/cleanup_system.sh
RUN chmod +x /autoware/cleanup_system.sh
WORKDIR /autoware

# Install apt packages and add GitHub to known hosts for private repositories
RUN rm -f /etc/apt/apt.conf.d/docker-clean \
&& echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \
gosu \
ssh \
&& apt-get autoremove -y && rm -rf "$HOME"/.cache \
&& /autoware/cleanup_apt.sh \
&& mkdir -p ~/.ssh \
&& ssh-keyscan github.com >> ~/.ssh/known_hosts

# Copy files
COPY setup-dev-env.sh ansible-galaxy-requirements.yaml amd64.env arm64.env /autoware/
COPY ansible/ /autoware/ansible/
COPY docker/scripts/cleanup_system.sh /autoware/cleanup_system.sh
RUN chmod +x /autoware/cleanup_system.sh
WORKDIR /autoware

# Set up base environment
RUN --mount=type=ssh \
--mount=type=cache,target=/var/cache/apt,sharing=locked \
./setup-dev-env.sh -y --module base --no-nvidia --no-cuda-drivers --runtime openadkit \
&& pip uninstall -y ansible ansible-core \
&& apt-get autoremove -y && rm -rf "$HOME"/.cache \
&& /autoware/cleanup_apt.sh \
&& echo "source /opt/ros/${ROS_DISTRO}/setup.bash" > /etc/bash.bashrc

# Create entrypoint
Expand All @@ -45,7 +47,7 @@ SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN --mount=type=ssh \
./setup-dev-env.sh -y --module base --download-artifacts --no-cuda-drivers --runtime openadkit \
&& pip uninstall -y ansible ansible-core \
&& apt-get autoremove -y && rm -rf "$HOME"/.cache
&& /autoware/cleanup_apt.sh true

# hadolint ignore=DL3006
FROM $BASE_IMAGE AS rosdep-depend
Expand All @@ -54,6 +56,8 @@ ARG ROS_DISTRO

COPY setup-dev-env.sh ansible-galaxy-requirements.yaml amd64.env arm64.env /autoware/
COPY ansible/ /autoware/ansible/
COPY docker/scripts/cleanup_apt.sh /autoware/cleanup_apt.sh
RUN chmod +x /autoware/cleanup_apt.sh
COPY docker/scripts/resolve_rosdep_keys.sh /autoware/resolve_rosdep_keys.sh
RUN chmod +x /autoware/resolve_rosdep_keys.sh
WORKDIR /autoware
Expand All @@ -64,7 +68,7 @@ RUN --mount=type=ssh \
--mount=type=cache,target=/var/cache/apt,sharing=locked \
./setup-dev-env.sh -y rosdep \
&& pip uninstall -y ansible ansible-core \
&& apt-get autoremove -y && rm -rf "$HOME"/.cache
&& /autoware/cleanup_apt.sh

# Generate install package lists
COPY src/core /autoware/src/core
Expand Down Expand Up @@ -169,15 +173,15 @@ RUN --mount=type=ssh \
./setup-dev-env.sh -y --module all --no-nvidia --no-cuda-drivers openadkit \
&& ./setup-dev-env.sh -y --module dev-tools openadkit \
&& pip uninstall -y ansible ansible-core \
&& apt-get autoremove -y && rm -rf "$HOME"/.cache
&& /autoware/cleanup_apt.sh

# Install rosdep dependencies
COPY --from=rosdep-depend /rosdep-core-depend-packages.txt /tmp/rosdep-core-depend-packages.txt
# hadolint ignore=SC2002
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update \
&& cat /tmp/rosdep-core-depend-packages.txt | xargs apt-get install -y --no-install-recommends \
&& apt-get autoremove -y && rm -rf "$HOME"/.cache
&& /autoware/cleanup_apt.sh

RUN --mount=type=cache,target=${CCACHE_DIR} \
--mount=type=bind,from=rosdep-depend,source=/autoware/src/core,target=/autoware/src/core \
Expand All @@ -196,9 +200,10 @@ ENV CCACHE_DIR="/root/.ccache"
COPY --from=rosdep-depend /rosdep-universe-common-depend-packages.txt /tmp/rosdep-universe-common-depend-packages.txt
# hadolint ignore=SC2002
RUN --mount=type=ssh \
--mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update \
&& cat /tmp/rosdep-universe-common-depend-packages.txt | xargs apt-get install -y --no-install-recommends \
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* "$HOME"/.cache
&& /autoware/cleanup_apt.sh

# hadolint ignore=SC1091
RUN --mount=type=cache,target=${CCACHE_DIR} \
Expand All @@ -219,7 +224,7 @@ SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN --mount=type=ssh \
./setup-dev-env.sh -y --module all --no-cuda-drivers --runtime openadkit \
&& pip uninstall -y ansible ansible-core \
&& apt-get autoremove -y && rm -rf "$HOME"/.cache
&& /autoware/cleanup_apt.sh true

ENTRYPOINT ["/ros_entrypoint.sh"]
CMD ["/bin/bash"]
Expand All @@ -233,9 +238,10 @@ ENV CCACHE_DIR="/root/.ccache"
COPY --from=rosdep-universe-sensing-perception-depend /rosdep-universe-sensing-perception-depend-packages.txt /tmp/rosdep-universe-sensing-perception-depend-packages.txt
# hadolint ignore=SC2002
RUN --mount=type=ssh \
--mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update \
&& cat /tmp/rosdep-universe-sensing-perception-depend-packages.txt | xargs apt-get install -y --no-install-recommends \
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* "$HOME"/.cache
&& /autoware/cleanup_apt.sh

# hadolint ignore=SC1091
RUN --mount=type=cache,target=${CCACHE_DIR} \
Expand All @@ -257,9 +263,10 @@ ENV CCACHE_DIR="/root/.ccache"
COPY --from=rosdep-universe-sensing-perception-depend /rosdep-universe-sensing-perception-depend-packages.txt /tmp/rosdep-universe-sensing-perception-depend-packages.txt
# hadolint ignore=SC2002
RUN --mount=type=ssh \
--mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update \
&& cat /tmp/rosdep-universe-sensing-perception-depend-packages.txt | xargs apt-get install -y --no-install-recommends \
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* "$HOME"/.cache
&& /autoware/cleanup_apt.sh

# hadolint ignore=SC1091
RUN --mount=type=cache,target=${CCACHE_DIR} \
Expand Down Expand Up @@ -293,9 +300,10 @@ ENV CCACHE_DIR="/root/.ccache"
COPY --from=rosdep-universe-localization-mapping-depend /rosdep-universe-localization-mapping-depend-packages.txt /tmp/rosdep-universe-localization-mapping-depend-packages.txt
# hadolint ignore=SC2002
RUN --mount=type=ssh \
--mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update \
&& cat /tmp/rosdep-universe-localization-mapping-depend-packages.txt | xargs apt-get install -y --no-install-recommends \
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* "$HOME"/.cache
&& /autoware/cleanup_apt.sh

# hadolint ignore=SC1091
RUN --mount=type=cache,target=${CCACHE_DIR} \
Expand All @@ -314,9 +322,10 @@ ENV CCACHE_DIR="/root/.ccache"
COPY --from=rosdep-universe-planning-control-depend /rosdep-universe-planning-control-depend-packages.txt /tmp/rosdep-universe-planning-control-depend-packages.txt
# hadolint ignore=SC2002
RUN --mount=type=ssh \
--mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update \
&& cat /tmp/rosdep-universe-planning-control-depend-packages.txt | xargs apt-get install -y --no-install-recommends \
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* "$HOME"/.cache
&& /autoware/cleanup_apt.sh

# hadolint ignore=SC1091
RUN --mount=type=cache,target=${CCACHE_DIR} \
Expand All @@ -343,9 +352,10 @@ ENV CCACHE_DIR="/root/.ccache"
COPY --from=rosdep-universe-vehicle-system-depend /rosdep-universe-vehicle-system-depend-packages.txt /tmp/rosdep-universe-vehicle-system-depend-packages.txt
# hadolint ignore=SC2002
RUN --mount=type=ssh \
--mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update \
&& cat /tmp/rosdep-universe-vehicle-system-depend-packages.txt | xargs apt-get install -y --no-install-recommends \
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* "$HOME"/.cache
&& /autoware/cleanup_apt.sh

# hadolint ignore=SC1091
RUN --mount=type=cache,target=${CCACHE_DIR} \
Expand All @@ -369,9 +379,10 @@ ENV CCACHE_DIR="/root/.ccache"
COPY --from=rosdep-universe-depend /rosdep-universe-depend-packages.txt /tmp/rosdep-universe-depend-packages.txt
# hadolint ignore=SC2002
RUN --mount=type=ssh \
--mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update \
&& cat /tmp/rosdep-universe-depend-packages.txt | xargs apt-get install -y --no-install-recommends \
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* "$HOME"/.cache
&& /autoware/cleanup_apt.sh

COPY --from=universe-sensing-perception-devel /opt/autoware /opt/autoware
COPY --from=universe-localization-mapping-devel /opt/autoware /opt/autoware
Expand Down Expand Up @@ -402,9 +413,10 @@ SHELL ["/bin/bash", "-o", "pipefail", "-c"]
COPY --from=rosdep-universe-depend /rosdep-universe-depend-packages.txt /tmp/rosdep-universe-depend-packages.txt
# hadolint ignore=SC2002
RUN --mount=type=ssh \
--mount=type=cache,target=/var/cache/apt,sharing=locked \
apt-get update \
&& cat /tmp/rosdep-universe-depend-packages.txt | xargs apt-get install -y --no-install-recommends \
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* "$HOME"/.cache
&& /autoware/cleanup_apt.sh

COPY --from=universe-sensing-perception-devel-cuda /opt/autoware /opt/autoware
COPY --from=universe-devel /opt/autoware /opt/autoware
Expand Down
11 changes: 11 additions & 0 deletions docker/scripts/cleanup_apt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

function cleanup() {
local remove_var_lib_apt_lists=false
apt-get autoremove -y && rm -rf "$HOME"/.cache
if "$remove_var_lib_apt_lists"; then
rm -rf /var/lib/apt/lists/*
fi
}

cleanup "$@"

0 comments on commit ef7a992

Please sign in to comment.