Skip to content

Commit 58f3456

Browse files
committed
refactor: Unify dockerfile across all workspaces and add tests
1 parent c4e62b6 commit 58f3456

File tree

10 files changed

+564
-247
lines changed

10 files changed

+564
-247
lines changed

cartographer_ws/docker/Dockerfile

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,102 @@
11
# Base Image : https://hub.docker.com/r/osrf/ros/tags?page=1&name=humble
2-
FROM osrf/ros:humble-desktop-full
2+
FROM osrf/ros:humble-desktop-full AS amd64
3+
# Base Image : https://hub.docker.com/r/arm64v8/ros/tags?page=1&name=humble
4+
FROM arm64v8/ros:humble AS arm64
5+
6+
# Use docker automatic platform args to select the base image.
7+
# It may be `arm64` or `amd64` depending on the platform.
8+
# Ref: https://docs.docker.com/reference/dockerfile/#automatic-platform-args-in-the-global-scope
9+
FROM $TARGETARCH
310

411
LABEL org.opencontainers.image.authors="assume0701@gmail.com"
512

13+
# Arguments for the default user
614
ARG USERNAME=user
715
ARG USER_UID=1000
8-
ARG USER_GID=$USER_UID
9-
10-
# Create the user
11-
RUN groupadd --gid $USER_GID $USERNAME \
12-
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
13-
#
14-
# [Optional] Add sudo support. Omit if you don't need to install software after connecting.
15-
&& apt-get update \
16-
&& apt-get install -y sudo \
17-
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
18-
&& chmod 0440 /etc/sudoers.d/$USERNAME \
19-
&& rm -rf /var/lib/apt/lists/*
20-
RUN apt-get update && apt-get upgrade -y \
16+
17+
# Keep downloaded packages for caching purposes
18+
# Ref: https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#example-cache-apt-packages
19+
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
20+
21+
# Upgrade packages
22+
# Ref: https://pythonspeed.com/articles/security-updates-in-docker/
23+
# Ref: https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#example-cache-apt-packages
24+
# Ref: https://github.com/moby/buildkit/issues/1673#issuecomment-1264502398
25+
# Ref: https://github.com/moby/buildkit/issues/1673#issuecomment-1987107404
26+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
27+
apt-get update && apt-get upgrade -y \
2128
&& rm -rf /var/lib/apt/lists/*
22-
RUN apt-get update && apt-get install -y python3-pip \
29+
30+
# Install sudo and create a user with sudo privileges
31+
# Ref: https://stackoverflow.com/a/65434659
32+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
33+
apt-get update && apt-get install -y sudo \
34+
&& useradd -m -s /bin/bash -u $USER_UID -G sudo $USERNAME \
35+
&& echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
2336
&& rm -rf /var/lib/apt/lists/*
24-
ENV SHELL /bin/bash
2537

2638
# Install common tools
27-
RUN apt-get update && apt-get install -y \
39+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
40+
apt-get update && apt-get install -y \
2841
curl \
2942
git \
30-
git-extras \
3143
htop \
44+
iputils-ping \
45+
nano \
3246
net-tools \
3347
tmux \
48+
tree \
49+
unzip \
3450
vim \
3551
wget \
52+
zip \
53+
&& rm -rf /var/lib/apt/lists/*
54+
55+
# Install Python pip
56+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
57+
apt-get update && apt-get install -y \
58+
python3-pip \
3659
&& rm -rf /var/lib/apt/lists/*
3760

38-
# Install turtlebot3, RVIZ, Gazebo and Cartographer
39-
RUN apt-get update && apt-get install -y \
40-
ros-$ROS_DISTRO-gazebo-ros-pkgs \
61+
# Install custom tools
62+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
63+
apt-get update && apt-get install -y \
64+
git-extras \
65+
&& rm -rf /var/lib/apt/lists/*
66+
67+
# Install ROS2 Gazebo packages for amd64
68+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
69+
if [ "$TARGETARCH" = "amd64" ]; then \
70+
apt-get update && apt-get install -y \
71+
ros-$ROS_DISTRO-gazebo-ros-pkgs \
72+
ros-$ROS_DISTRO-gazebo-ros2-control \
73+
&& rm -rf /var/lib/apt/lists/*; \
74+
fi
75+
76+
# Install ROS2 RVIZ and other custom ROS2 packages
77+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
78+
apt-get update && apt-get install -y \
4179
ros-$ROS_DISTRO-rviz2 \
80+
&& rm -rf /var/lib/apt/lists/*
81+
82+
# TODO: Add more commands here
83+
# For example, to install additional packages, uncomment the following lines and add the package names
84+
# RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
85+
# apt-get update && apt-get install -y \
86+
# $OTHER_PACKAGES \
87+
# && rm -rf /var/lib/apt/lists/*
88+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
89+
apt-get update && apt-get install -y \
4290
ros-$ROS_DISTRO-cartographer \
4391
ros-$ROS_DISTRO-turtlebot3* \
4492
ros-$ROS_DISTRO-rqt-robot-steering \
4593
&& rm -rf /var/lib/apt/lists/*
4694

47-
COPY .bashrc /home/$USERNAME/.bashrc
48-
4995
USER $USERNAME
96+
# Create Gazebo cache directory with correct ownership to avoid permission issues after volume mount
97+
RUN mkdir /home/$USERNAME/.gazebo
98+
# TODO: Run additional commands as non-root user here
99+
COPY .bashrc /home/$USERNAME/.bashrc
100+
# TODO: Copy additional files here
101+
ENTRYPOINT []
50102
CMD ["/bin/bash"]

husky_ws/docker/Dockerfile

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,92 @@
1-
# Base Image : https://hub.docker.com/r/arm64v8/ros/tags?page=1&name=humble
2-
FROM arm64v8/ros:humble AS arm64
31
# Base Image : https://hub.docker.com/r/osrf/ros/tags?page=1&name=humble
42
FROM osrf/ros:humble-desktop-full AS amd64
3+
# Base Image : https://hub.docker.com/r/arm64v8/ros/tags?page=1&name=humble
4+
FROM arm64v8/ros:humble AS arm64
55

66
# Use docker automatic platform args to select the base image.
77
# It may be `arm64` or `amd64` depending on the platform.
8-
# Reference:
9-
# - https://docs.docker.com/reference/dockerfile/#automatic-platform-args-in-the-global-scope
8+
# Ref: https://docs.docker.com/reference/dockerfile/#automatic-platform-args-in-the-global-scope
109
FROM $TARGETARCH
1110

1211
LABEL org.opencontainers.image.authors="yuzhong1214@gmail.com"
1312

14-
ARG TARGETARCH
13+
# Arguments for the default user
1514
ARG USERNAME=user
1615
ARG USER_UID=1000
17-
ARG USER_GID=$USER_UID
1816

19-
# Create the user
20-
RUN groupadd --gid $USER_GID $USERNAME \
21-
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
22-
#
23-
# [Optional] Add sudo support. Omit if you don't need to install software after connecting.
24-
&& apt-get update \
25-
&& apt-get install -y sudo \
26-
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
27-
&& chmod 0440 /etc/sudoers.d/$USERNAME \
28-
&& rm -rf /var/lib/apt/lists/*
29-
RUN apt-get update && apt-get upgrade -y \
30-
&& rm -rf /var/lib/apt/lists/*
31-
RUN apt-get update && apt-get install -y python3-pip \
17+
# Keep downloaded packages for caching purposes
18+
# Ref: https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#example-cache-apt-packages
19+
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
20+
21+
# Upgrade packages
22+
# Ref: https://pythonspeed.com/articles/security-updates-in-docker/
23+
# Ref: https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#example-cache-apt-packages
24+
# Ref: https://github.com/moby/buildkit/issues/1673#issuecomment-1264502398
25+
# Ref: https://github.com/moby/buildkit/issues/1673#issuecomment-1987107404
26+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
27+
apt-get update && apt-get upgrade -y \
3228
&& rm -rf /var/lib/apt/lists/*
33-
ENV SHELL /bin/bash
3429

35-
# ********************************************************
36-
# * Anything else you want to do like clean up goes here *
37-
# ********************************************************
30+
# Install sudo and create a user with sudo privileges
31+
# Ref: https://stackoverflow.com/a/65434659
32+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
33+
apt-get update && apt-get install -y sudo \
34+
&& useradd -m -s /bin/bash -u $USER_UID -G sudo $USERNAME \
35+
&& echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \
36+
&& rm -rf /var/lib/apt/lists/*
3837

3938
# Install common tools
40-
RUN apt-get update && apt-get install -y \
39+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
40+
apt-get update && apt-get install -y \
4141
curl \
4242
git \
43-
git-extras \
4443
htop \
44+
iputils-ping \
45+
nano \
4546
net-tools \
4647
tmux \
48+
tree \
49+
unzip \
4750
vim \
4851
wget \
52+
zip \
53+
&& rm -rf /var/lib/apt/lists/*
54+
55+
# Install Python pip
56+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
57+
apt-get update && apt-get install -y \
58+
python3-pip \
59+
&& rm -rf /var/lib/apt/lists/*
60+
61+
# Install custom tools
62+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
63+
apt-get update && apt-get install -y \
64+
git-extras \
4965
&& rm -rf /var/lib/apt/lists/*
5066

51-
RUN if [ "$TARGETARCH" = "amd64" ]; then \
67+
# Install ROS2 Gazebo packages for amd64
68+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
69+
if [ "$TARGETARCH" = "amd64" ]; then \
5270
apt-get update && apt-get install -y \
5371
ros-$ROS_DISTRO-gazebo-ros-pkgs \
5472
ros-$ROS_DISTRO-gazebo-ros2-control \
5573
&& rm -rf /var/lib/apt/lists/*; \
5674
fi
5775

58-
# Install ROS2 packages
59-
RUN apt-get update && apt-get install -y \
76+
# Install ROS2 RVIZ and other custom ROS2 packages
77+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
78+
apt-get update && apt-get install -y \
6079
ros-$ROS_DISTRO-rviz2 \
61-
#
80+
&& rm -rf /var/lib/apt/lists/*
81+
82+
# TODO: Add more commands here
83+
# For example, to install additional packages, uncomment the following lines and add the package names
84+
# RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
85+
# apt-get update && apt-get install -y \
86+
# $OTHER_PACKAGES \
87+
# && rm -rf /var/lib/apt/lists/*
88+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
89+
apt-get update && apt-get install -y \
6290
# The packages below are used by the Husky repository.
6391
ros-$ROS_DISTRO-controller-manager \
6492
ros-$ROS_DISTRO-diff-drive-controller \
@@ -83,28 +111,34 @@ RUN apt-get update && apt-get install -y \
83111
ros-$ROS_DISTRO-slam-toolbox \
84112
ros-$ROS_DISTRO-imu-tools \
85113
ros-$ROS_DISTRO-teleop-twist-keyboard \
86-
&& sudo rm -rf /var/lib/apt/lists/*
87-
88-
COPY .bashrc /home/$USERNAME/.bashrc
114+
&& rm -rf /var/lib/apt/lists/*
89115

90-
# [Optional] Set the default user. Omit if you want to keep the default as root.
91116
USER $USERNAME
92-
CMD ["/bin/bash"]
117+
# Create Gazebo cache directory with correct ownership to avoid permission issues after volume mount
118+
RUN mkdir /home/$USERNAME/.gazebo
119+
# TODO: Run additional commands as non-root user here
120+
COPY .bashrc /home/$USERNAME/.bashrc
121+
# TODO: Copy additional files here
93122

94123
# Setup husky controller by the script.
95124
# Build certain packages from source for arm64.
96125
COPY script /home/$USERNAME/script
97-
RUN sudo apt-get update \
126+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
127+
sudo apt-get update \
98128
&& bash -ie /home/$USERNAME/script/install-clearpath-robot.sh \
99129
&& sudo rm -rf /var/lib/apt/lists/*
100130
# Note: The script need to be run as user, not root.
101131
# Reference: https://github.com/clearpathrobotics/clearpath_computer_installer/tree/main
102132
COPY clearpath_computer_installer /home/$USERNAME/clearpath_computer_installer
103-
RUN sudo apt-get update \
133+
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
134+
sudo apt-get update \
104135
&& bash -ie /home/$USERNAME/clearpath_computer_installer/clearpath_computer_installer.sh \
105136
&& sudo rm -rf /var/lib/apt/lists/*
106137
# Setup udev rules.
107138
COPY udev_rules /home/$USERNAME/udev_rules
108139
RUN /home/$USERNAME/udev_rules/install_udev_rules.sh
109140
# Generate robot configuration files.
110141
RUN bash -ie /home/$USERNAME/script/husky-generate.sh
142+
143+
ENTRYPOINT []
144+
CMD ["/bin/bash"]

0 commit comments

Comments
 (0)