Skip to content

Commit

Permalink
Adds instructions to build a docker image in an x86/x64 architecture …
Browse files Browse the repository at this point in the history
…for arm64/v8 and avoid installing everything in the RPI.

Signed-off-by: Agustin Alba Chicar <[email protected]>
  • Loading branch information
agalbachicar committed Dec 29, 2023
1 parent 2e552b5 commit bfa1c3d
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
58 changes: 58 additions & 0 deletions docker/Dockerfile.arm64
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
FROM arm64v8/ros:humble as base

# Arguments for building
ARG USERID
ARG USER

# Setup environment
ENV TERM linux
ENV DEBIAN_FRONTEND noninteractive
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

# Copy requirement files and install dependencies
COPY docker/requirements.txt .
RUN apt-get update && apt-get install --no-install-recommends -y $(cat requirements.txt)
RUN rm requirements.txt

# Create a user with passwordless sudo
RUN adduser --uid $USERID --gecos "ekumen developer" --disabled-password $USER
RUN adduser $USER sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
RUN echo "export QT_X11_NO_MITSHM=1" >> /home/$USER/.bashrc
USER $USER

# Adds USER to dialout and plugdev group.
# This is needed to access the serial ports, for further references check
# the libserial documentation.
RUN sudo usermod -a -G dialout $USER
RUN sudo usermod -a -G plugdev $USER

# Creates the src folder of the workspace.
RUN mkdir -p /home/$USER/ws/src

# Adds to bashrc the ros humble overlay sourcing.
RUN echo "source /opt/ros/humble/setup.bash" >> /home/$USER/.bashrc
# Adds colcon autocomplete
RUN echo "source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash" >> /home/$USER/.bashrc

# Updates and initializes rosdep.
RUN sudo apt upgrade -y && sudo apt update && rosdep update

# Defines a workspace folder.
WORKDIR /home/$USER/ws

CMD ["/bin/bash"]

FROM base AS dev

# Clone the repository.
RUN git clone https://github.com/Ekumen-OS/andino.git src/
# Install the workspace dependencies and the repository.
# TODO: fix dependencies so rosdep does not fail.
RUN rosdep install --from-paths src --ignore-src -i -y -r || true
SHELL ["/bin/bash", "-c"]
RUN source /opt/ros/humble/setup.bash && \
colcon build --event-handlers console_direct+ && \
colcon test --event-handlers console_direct+
# Source the andino workspace.
RUN echo "source /ws/install/setup.bash" >> /home/$USER/.bashrc
89 changes: 89 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,92 @@ file the repositories in your workspace.
```sh
colcon build
```
### Building a docker image for the Raspberry PI

This is convenient to avoid running the installation process in the Raspberry Pi, and to have a unified environment. The following will guide you to cross compile the docker image from an x86/x64 computing platform into an arm64 compatible image.

#### Configure the `buildx` driver

Run the following to see the `docker buildx` nodes:

```
$ docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS BUILDKIT PLATFORMS
default docker
default default running v0.11.7+d3e6c1360f6e linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/386
```

As you can see, the `default` cannot build images for `linux/arm64/v8`. Those are required for running in the Raspberry Pi. You can create a node that works for that the following way:


```
docker buildx create --name andino_arm64_arch --platform "linux/arm64/v8" --driver "docker-container"
```

And select it then:

```
$ docker buildx use andino_arm64_arch
$ docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS BUILDKIT PLATFORMS
andino_arm64_arch * docker-container
andino_arm64_arch0 unix:///var/run/docker.sock running v0.12.4 linux/arm64*, linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/386
default docker
default default running v0.11.7+d3e6c1360f6e linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/386
```

#### Build the arm64/v8 image

At this point, the only required step is building the image:


Run the following (known bug: https://github.com/multiarch/alpine/issues/32 ) so sudo can execute:

```
docker run --rm --privileged multiarch/qemu-user-static:register --reset --credential yes
```
and then:

```
docker buildx build -t andino:arm64 \
--platform linux/arm64/v8 \
--build-arg USERID=$(id -u) \
--build-arg USER=$(whoami) \
--load \
--file docker/Dockerfile.arm64 .
```

The previous command will create a docker image called `andino` and tag it with `arm64`.

That's it! You have an andino docker image to run in your Raspberry Pi.

#### Transfer it to the Raspberry Pi

After a successful build, consider running the following to save to a tarball the docker image and copying it to the Raspberry Pi:

```
docker save andino:arm64 > andino_arm64.tar
```

You can copy it via `scp`:

```
scp andino_arm64.tar <andino_ip>@<andino_ip>:/tmp
```

And then load and execute the docker image there:

```
ssh <andino_ip>@<andino_ip>
docker load < andino_arm64.tar
docker run \
--rm \
--privileged \
--net=host \
-it \
-v /dev:/dev \
--name andino_container \
andino:arm64
```

As you can see, you will need docker installed in the Raspberry Pi, [here](https://docs.docker.com/engine/install/ubuntu/) are the installation docs.

0 comments on commit bfa1c3d

Please sign in to comment.