Skip to content

Dockerfile for building example application #131

@aldas

Description

@aldas

It would be cool if there would be Dockerfile(s) in repo instead/in addition to Docker image binaries from website. With file like that you have good base for building your own application images for FastDDS.

Currently I have made something like that for HelloWorldPublisher/Subscriber. Image size comes is 181MB.

Dockerfile

ARG  BUILDER_IMAGE=python:3.12-slim-bookworm
###############################################
# STEP 1 build FastDDS python library and generate code from project IDL files
###############################################
FROM ${BUILDER_IMAGE} as builder

ARG fastdds_version=1.4.1
ENV fastdds_version=${fastdds_version}

RUN apt update &&  \
    apt install -y --no-install-recommends \
        git \
        cmake \
        build-essential \
        wget \
        libasio-dev \
        libtinyxml2-dev \
        swig \
        libpython3-dev \
        openjdk-17-jdk

# Install `colcon` and `vcs`
RUN pip install -U colcon-common-extensions vcstool

RUN mkdir -p /fastdds_python_ws/src && \
    cd /fastdds_python_ws && \
    wget "https://raw.githubusercontent.com/eProsima/Fast-DDS-python/v${fastdds_version}/fastdds_python.repos" && \
    vcs import src < fastdds_python.repos

# Build python library
RUN cd /fastdds_python_ws && \
    colcon build

# Create fastddsgen jar file to be able to generate code from IDL files
RUN cd /fastdds_python_ws/src/fastddsgen/ && \
    ./gradlew assemble

WORKDIR /project
COPY . .

# Generate code from IDL files using fastddsgen
RUN cd /project && \
    . /fastdds_python_ws/install/setup.sh && \
    /usr/bin/java -jar /fastdds_python_ws/src/fastddsgen/share/fastddsgen/java/fastddsgen.jar \
      -python HelloWorld.idl && \
    cmake . && \
    make

#########################
# STEP 2 build the image
#########################
FROM python:3.12-slim-bookworm

ENV USER=appuser
ENV UID=10001

# Create appuser to run code as an unprivileged user.
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
#    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    "${USER}"

RUN apt update &&  \
    apt install -y --no-install-recommends \
        ca-certificates \
        tzdata \
        libtinyxml2-dev && \
    update-ca-certificates

# Import from builder.
COPY --from=builder /fastdds_python_ws/install /fastdds_python_ws/install
COPY --from=builder /project/entrypoint.sh /project/entrypoint.sh
COPY --from=builder /project /project

WORKDIR /project

# Use an unprivileged user to run the code.
USER ${USER}:${UID}


ENTRYPOINT ["/project/entrypoint.sh"]
CMD ["/project/HelloWorldSubscriber.py"]

and entrypoint.sh is

#!/usr/bin/env sh
set -e

DIR=$(dirname "$0")
cd "$DIR"

. /fastdds_python_ws/install/setup.sh
# `-u` disables python output buffering so output will be immediately visible from container
exec python3 -u "$@"

These files assume that you have following files in same folder

  • HelloWorld.idl
  • HelloWorldPublisher.py
  • HelloWorldSubscriber.py
  • DEFAULT_FASTRTPS_PROFILES.xml (in my case I want to try with udp only transport)

and I build Docker image for the application with

sudo docker build . -t fastdds-python-example

and run subscriber with:

sudo docker run --rm --network host fastdds-python-example

and publisher with:

sudo docker run --rm --network host fastdds-python-example /project/HelloWorldPublisher.py

or using docker IPC to use shared memory

sudo docker run --rm --ipc=shareable fastdds-python-example
sudo docker run --rm --ipc=container:$(sudo docker container ls -a --format "{{.Names}}" -f ancestor=fastdds-python-example) fastdds-python-example /project/HelloWorldPublisher.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions