Skip to content

Commit c9213ce

Browse files
authored
Add multi-platform dockerfiles (#84)
* Add multi images and update Rust version to 1.74 * fixed links to files * Downgrade markdown link check version to fix regression causing failure
1 parent e46252c commit c9213ce

11 files changed

+281
-7
lines changed

.github/workflows/markdown-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ jobs:
2424
steps:
2525
- uses: actions/checkout@v2
2626
- run: |
27-
npm install markdown-link-check
27+
npm install markdown-link-check@3.11.2
2828
find . -type d \( -name node_modules -o -name .github \) -prune -o -type f -name '*.md' -print0 | xargs -0 -n1 node_modules/.bin/markdown-link-check --config ./tools/.markdownlinkcheck.jsonc --quiet

Dockerfile.amd64

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
################################################################################
1212
# Create a stage for building the application.
1313

14-
ARG RUST_VERSION=1.72.1
14+
ARG RUST_VERSION=1.74
1515
ARG APP_NAME=pub-sub-service
1616
ARG UID=10001
1717

Dockerfile.arm64

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
################################################################################
1212
# Create a stage for building the application.
1313

14-
ARG RUST_VERSION=1.72.1
14+
ARG RUST_VERSION=1.74
1515
ARG APP_NAME=pub-sub-service
1616
ARG UID=10001
1717

Dockerfile.mosquitto.multi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT license.
3+
# SPDX-License-Identifier: MIT
4+
5+
# syntax=docker/dockerfile:1
6+
7+
# Comments are provided throughout this file to help you get started.
8+
# If you need more help, visit the Dockerfile reference guide at
9+
# https://docs.docker.com/engine/reference/builder/
10+
11+
################################################################################
12+
# Create a stage for building the application.
13+
14+
FROM --platform=$TARGETPLATFORM docker.io/library/eclipse-mosquitto
15+
WORKDIR /mosquitto/config
16+
17+
COPY ./pub-sub-service/src/connectors/mosquitto.conf ./mosquitto.conf
18+
19+
# Expose the port that the mqtt broker listens on.
20+
EXPOSE 1883

Dockerfile.multi

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT license.
3+
# SPDX-License-Identifier: MIT
4+
5+
# syntax=docker/dockerfile:1
6+
7+
# Comments are provided throughout this file to help you get started.
8+
# If you need more help, visit the Dockerfile reference guide at
9+
# https://docs.docker.com/engine/reference/builder/
10+
11+
################################################################################
12+
# Create a stage for building the application.
13+
14+
ARG RUST_VERSION=1.74
15+
ARG APP_NAME=pub-sub-service
16+
ARG UID=10001
17+
18+
FROM --platform=$BUILDPLATFORM docker.io/library/rust:${RUST_VERSION} AS build
19+
20+
# Target architecture to cross-compile
21+
ARG TARGETARCH
22+
23+
ARG APP_NAME
24+
WORKDIR /sdv
25+
26+
COPY ./ .
27+
28+
# Check that APP_NAME argument is valid.
29+
RUN /sdv/container/scripts/argument_sanitizer.sh \
30+
--arg-value "${APP_NAME}" \
31+
--regex "^[a-zA-Z_0-9-]+$" || \
32+
( echo "Argument sanitizer failed for ARG 'APP_NAME'"; exit 1 )
33+
34+
# Check that TARGETARCH argument is valid.
35+
RUN /sdv/container/scripts/argument_sanitizer.sh \
36+
--arg-value "${TARGETARCH}" \
37+
--regex "^[a-zA-Z_0-9-]+$" || \
38+
( echo "Argument sanitizer failed for ARG 'TARGETARCH'"; exit 1 )
39+
40+
# Add Build dependencies.
41+
RUN apt update && apt upgrade -y && apt install -y \
42+
cmake \
43+
libssl-dev \
44+
pkg-config \
45+
protobuf-compiler
46+
47+
# Based on the target architecture, add the appropriate build target and build service.
48+
RUN if [ "$TARGETARCH" = "amd64" ]; then \
49+
CARGOARCH="x86_64-unknown-linux-gnu"; \
50+
elif [ "$TARGETARCH" = "arm64" ]; then \
51+
apt install -y gcc-aarch64-linux-gnu; \
52+
CARGOARCH="aarch64-unknown-linux-gnu"; \
53+
else \
54+
echo "Unsupported cross-compile architecture"; \
55+
exit 1; \
56+
fi; \
57+
rustup target add ${CARGOARCH}; \
58+
cargo build --release --target=${CARGOARCH} -p "${APP_NAME}"; \
59+
cp /sdv/target/${CARGOARCH}/release/"${APP_NAME}" /sdv/service
60+
61+
################################################################################
62+
# Create a new stage for running the application that contains the minimal
63+
# runtime dependencies for the application. This often uses a different base
64+
# image from the build stage where the necessary files are copied from the build
65+
# stage.
66+
#
67+
# The example below uses the debian bullseye image as the foundation for running the app.
68+
# By specifying the "bullseye-slim" tag, it will also use whatever happens to be the
69+
# most recent version of that tag when you build your Dockerfile. If
70+
# reproducability is important, consider using a digest
71+
# (e.g., debian@sha256:ac707220fbd7b67fc19b112cee8170b41a9e97f703f588b2cdbbcdcecdd8af57).
72+
FROM --platform=$TARGETPLATFORM docker.io/library/debian:bullseye-slim AS final
73+
ARG UID
74+
75+
# Copy container scripts.
76+
COPY ./container/scripts/*.sh /sdv/scripts/
77+
78+
# Check that UID argument is valid.
79+
RUN /sdv/scripts/argument_sanitizer.sh \
80+
--arg-value "${UID}" \
81+
--regex "^[0-9]+$" || \
82+
( echo "Argument sanitizer failed for ARG 'UID'"; exit 1 )
83+
84+
# Create a non-privileged user that the app will run under.
85+
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user
86+
RUN adduser \
87+
--disabled-password \
88+
--gecos "" \
89+
--home "/nonexistent" \
90+
--shell "/sbin/nologin" \
91+
--no-create-home \
92+
--uid "${UID}" \
93+
appuser
94+
95+
# Create and add user ownership to config directory.
96+
RUN mkdir -p /sdv/.agemo/config
97+
RUN chown appuser /sdv/.agemo/config
98+
99+
# Create mnt directory to copy override configs into.
100+
RUN mkdir -p /mnt/config
101+
102+
USER appuser
103+
104+
WORKDIR /sdv
105+
106+
ENV AGEMO_HOME=/sdv/.agemo
107+
108+
# Copy the executable from the "build" stage.
109+
COPY --from=build /sdv/service /sdv/
110+
111+
# Expose the port that the application listens on.
112+
EXPOSE 50051
113+
114+
# What the container should run when it is started.
115+
CMD ["/sdv/scripts/container_startup.sh"]

Dockerfile.samples.amd64

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
################################################################################
1212
# Create a stage for building the application.
1313

14-
ARG RUST_VERSION=1.72.1
14+
ARG RUST_VERSION=1.74
1515
ARG APP_NAME=simple-publisher
1616
ARG UID=10001
1717

Dockerfile.samples.arm64

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
################################################################################
1212
# Create a stage for building the application.
1313

14-
ARG RUST_VERSION=1.72.1
14+
ARG RUST_VERSION=1.74
1515
ARG APP_NAME=simple-publisher
1616
ARG UID=10001
1717

Dockerfile_integrated.amd64

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
################################################################################
1212
# Create a stage for building the application.
1313

14-
ARG RUST_VERSION=1.72.1
14+
ARG RUST_VERSION=1.74
1515
ARG APP_NAME=pub-sub-service
1616
ARG UID=10001
1717

Dockerfile_integrated.arm64

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
################################################################################
1212
# Create a stage for building the application.
1313

14-
ARG RUST_VERSION=1.72.1
14+
ARG RUST_VERSION=1.74
1515
ARG APP_NAME=pub-sub-service
1616
ARG UID=10001
1717

Dockerfile_integrated.multi

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT license.
3+
# SPDX-License-Identifier: MIT
4+
5+
# syntax=docker/dockerfile:1
6+
7+
# Comments are provided throughout this file to help you get started.
8+
# If you need more help, visit the Dockerfile reference guide at
9+
# https://docs.docker.com/engine/reference/builder/
10+
11+
################################################################################
12+
# Create a stage for building the application.
13+
14+
ARG RUST_VERSION=1.74
15+
ARG APP_NAME=pub-sub-service
16+
ARG UID=10001
17+
18+
FROM --platform=$BUILDPLATFORM docker.io/library/rust:${RUST_VERSION} AS build
19+
20+
# Target architecture to cross-compile
21+
ARG TARGETARCH
22+
23+
ARG APP_NAME
24+
WORKDIR /sdv
25+
26+
COPY ./ .
27+
28+
# Check that APP_NAME argument is valid.
29+
RUN /sdv/container/scripts/argument_sanitizer.sh \
30+
--arg-value "${APP_NAME}" \
31+
--regex "^[a-zA-Z_0-9-]+$" || \
32+
( echo "Argument sanitizer failed for ARG 'APP_NAME'"; exit 1 )
33+
34+
# Check that TARGETARCH argument is valid.
35+
RUN /sdv/container/scripts/argument_sanitizer.sh \
36+
--arg-value "${TARGETARCH}" \
37+
--regex "^[a-zA-Z_0-9-]+$" || \
38+
( echo "Argument sanitizer failed for ARG 'TARGETARCH'"; exit 1 )
39+
40+
# Add Build dependencies.
41+
RUN apt update && apt upgrade -y && apt install -y \
42+
cmake \
43+
libssl-dev \
44+
pkg-config \
45+
protobuf-compiler
46+
47+
# Based on the target architecture, add the appropriate build target and build service.
48+
RUN if [ "$TARGETARCH" = "amd64" ]; then \
49+
CARGOARCH="x86_64-unknown-linux-gnu"; \
50+
elif [ "$TARGETARCH" = "arm64" ]; then \
51+
apt install -y gcc-aarch64-linux-gnu; \
52+
CARGOARCH="aarch64-unknown-linux-gnu"; \
53+
else \
54+
echo "Unsupported cross-compile architecture"; \
55+
exit 1; \
56+
fi; \
57+
rustup target add ${CARGOARCH}; \
58+
cargo build --release --target=${CARGOARCH} -p "${APP_NAME}"; \
59+
cp /sdv/target/${CARGOARCH}/release/"${APP_NAME}" /sdv/service
60+
61+
################################################################################
62+
# Create a new stage for running the application that contains the minimal
63+
# runtime dependencies for the application. This often uses a different base
64+
# image from the build stage where the necessary files are copied from the build
65+
# stage.
66+
#
67+
# The example below uses the debian bullseye image as the foundation for running the app.
68+
# By specifying the "bullseye-slim" tag, it will also use whatever happens to be the
69+
# most recent version of that tag when you build your Dockerfile. If
70+
# reproducability is important, consider using a digest
71+
# (e.g., debian@sha256:ac707220fbd7b67fc19b112cee8170b41a9e97f703f588b2cdbbcdcecdd8af57).
72+
FROM --platform=$TARGETPLATFORM docker.io/library/debian:bullseye-slim AS final
73+
ARG UID
74+
75+
# Copy container scripts.
76+
COPY ./container/scripts/*.sh /sdv/scripts/
77+
78+
# Check that UID argument is valid.
79+
RUN /sdv/scripts/argument_sanitizer.sh \
80+
--arg-value "${UID}" \
81+
--regex "^[0-9]+$" || \
82+
( echo "Argument sanitizer failed for ARG 'UID'"; exit 1 )
83+
84+
# Create a non-privileged user that the app will run under.
85+
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user
86+
RUN adduser \
87+
--disabled-password \
88+
--gecos "" \
89+
--home "/nonexistent" \
90+
--shell "/sbin/nologin" \
91+
--no-create-home \
92+
--uid "${UID}" \
93+
appuser
94+
95+
# Create and add user ownership to config directory.
96+
RUN mkdir -p /sdv/.agemo/config
97+
RUN chown appuser /sdv/.agemo/config
98+
99+
# Create mnt directory to copy override configs into.
100+
RUN mkdir -p /mnt/config
101+
102+
USER appuser
103+
104+
WORKDIR /sdv
105+
106+
ENV AGEMO_HOME=/sdv/.agemo
107+
108+
# Copy the executable from the "build" stage.
109+
COPY --from=build /sdv/service /sdv/
110+
111+
# Copy the "integrated" config to the override config folder and rename it to what agemo expects
112+
COPY --from=build /sdv/config/pub_sub_service_settings.integrated.yaml /sdv/.agemo/config/pub_sub_service_settings.yaml
113+
114+
# Expose the port that the application listens on.
115+
EXPOSE 50051
116+
117+
# What the container should run when it is started.
118+
CMD ["/sdv/scripts/container_startup.sh"]

0 commit comments

Comments
 (0)