Skip to content

Commit 66e7e00

Browse files
authored
Merge pull request #143 from movementlabsxyz/l-monninger/docker-build-with-d-in-d
feat: podman-in-podman
2 parents b4e05b8 + e54eb7b commit 66e7e00

File tree

11 files changed

+454
-21
lines changed

11 files changed

+454
-21
lines changed

.github/workflows/build-push-containers-all.yml

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,31 @@ jobs:
2222
echo "INFO: github context object content below"
2323
echo "${GITHUB_CONTEXT}"
2424
25-
# build-push-checked-containers:
26-
# uses: ./.github/workflows/build-push-container.yml
27-
# name: Build Push Checked Containers
28-
# secrets: inherit
29-
# strategy:
30-
# matrix:
31-
# container_name:
25+
build-push-checked-containers:
26+
uses: ./.github/workflows/build-push-container.yml
27+
name: Build Push Checked Containers
28+
secrets: inherit
29+
strategy:
30+
matrix:
31+
container_name:
32+
- movement
33+
- movement-aptos
34+
- mtma
3235

33-
# with:
34-
# container_name: ${{ matrix.container_name }}
36+
with:
37+
container_name: ${{ matrix.container_name }}
3538

36-
# build-push-checked-manifest:
37-
# uses: ./.github/workflows/build-push-manifest.yml
38-
# name: Build Push Checked Manifest
39-
# needs:
40-
# - build-push-checked-containers
41-
# secrets: inherit
42-
# strategy:
43-
# matrix:
44-
# container_name:
45-
# with:
46-
# container_name: ${{ matrix.container_name }}
39+
build-push-checked-manifest:
40+
uses: ./.github/workflows/build-push-manifest.yml
41+
name: Build Push Checked Manifest
42+
needs:
43+
- build-push-checked-containers
44+
secrets: inherit
45+
strategy:
46+
matrix:
47+
container_name:
48+
- movement
49+
- movement-aptos
50+
- mtma
51+
with:
52+
container_name: ${{ matrix.container_name }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ debug
77
.direnv
88
.DS_Store
99
.vendors
10+
.vendor
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM nixos/nix:2.29.0 AS builder
2+
3+
RUN nix-env -iA nixpkgs.rsync nixpkgs.glibc nixpkgs.gawk
4+
5+
# Copy the source code into the container
6+
COPY . /tmp/build
7+
WORKDIR /tmp/build
8+
9+
# Set build to docker to skip the podman initilization while opening the flake
10+
ENV BUILD=docker
11+
12+
# Build the Rust application
13+
RUN nix --extra-experimental-features "nix-command flakes" \
14+
develop .#docker-build --command bash -c "cargo build --release -p movement-aptos"
15+
16+
RUN rust_binary="./target/release/movement-aptos"; dest_dir="/tmp/runtime"; \
17+
mkdir -p "$dest_dir"; ldd "$rust_binary" | awk '{print $3}' | \
18+
grep '^/' | xargs -I {} dirname {} | sort | uniq | xargs -I {} \
19+
bash -c 'mkdir -p "$0/$1" && rsync -a --copy-links "$1/" "$0/$1/"' "$dest_dir" {}
20+
21+
FROM alpine:3.22.0
22+
23+
# Install dependencies: git
24+
RUN apk add --no-cache git
25+
26+
# Copy the build artifact from the builder stage
27+
COPY --from=builder /tmp/build/target/release/movement-aptos /app/movement-aptos
28+
COPY --from=builder /tmp/runtime/nix/store /nix/store
29+
30+
# Set the binary as the entrypoint
31+
ENTRYPOINT ["/app/movement-aptos"]

docker/build/movement/Dockerfile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
FROM nixos/nix:2.29.0 AS builder
2+
3+
RUN nix-env -iA nixpkgs.rsync nixpkgs.glibc nixpkgs.gawk
4+
5+
# Copy the source code into the container
6+
COPY . /tmp/build
7+
WORKDIR /tmp/build
8+
9+
# Set build to docker to skip the podman initilization while opening the flake
10+
ENV BUILD=docker
11+
12+
# Build the Rust application
13+
RUN nix --extra-experimental-features "nix-command flakes" \
14+
develop .#docker-build --command bash -c "cargo build --release --bin movement"
15+
16+
RUN rust_binary="./target/release/movement"; dest_dir="/tmp/runtime"; \
17+
mkdir -p "$dest_dir"; ldd "$rust_binary" | awk '{print $3}' | \
18+
grep '^/' | xargs -I {} dirname {} | sort | uniq | xargs -I {} \
19+
bash -c 'mkdir -p "$0/$1" && rsync -a --copy-links "$1/" "$0/$1/"' "$dest_dir" {}
20+
21+
22+
FROM alpine:3.22.0
23+
24+
# Create non-root user
25+
RUN adduser -u 1000 -D -s /bin/bash movement
26+
27+
# Copy binary and runtime deps
28+
COPY --from=builder /tmp/build/target/release/movement /app/movement
29+
COPY --from=builder /tmp/runtime/nix/store /nix/store
30+
31+
# Environment setup
32+
ENV PATH="/nix/var/nix/profiles/default/bin:$PATH"
33+
ENV XDG_RUNTIME_DIR="/run/user/1000"
34+
ENV TMPDIR="/tmp"
35+
ENV DOCKER_HOST="unix:///run/user/1000/podman/podman-machine-default-api.sock"
36+
37+
# Create required runtime dirs with proper ownership
38+
RUN mkdir -p /run/user/1000/podman && \
39+
chown -R movement:movement /run/user/1000 /app /nix
40+
41+
# Copy runtime bootstrap script
42+
COPY docker/build/movement/entry.sh /app/entry.sh
43+
RUN chmod +x /app/entry.sh
44+
45+
# Switch to non-root user
46+
USER movement
47+
48+
# Entrypoint to bootstrap podman and launch movement
49+
ENTRYPOINT ["/app/entry.sh"]

docker/build/movement/entry.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Start Podman machine if not running
6+
if ! podman machine inspect podman-machine-default --format '{{.State}}' 2>/dev/null | grep -q 'running'; then
7+
echo "Starting podman machine..."
8+
podman machine start
9+
fi
10+
11+
# Wait for podman socket
12+
timeout=30
13+
elapsed=0
14+
while [ ! -S "$DOCKER_HOST" ]; do
15+
echo "Waiting for podman socket..."
16+
sleep 1
17+
elapsed=$((elapsed + 1))
18+
if [ "$elapsed" -ge "$timeout" ]; then
19+
echo "Timed out waiting for podman socket."
20+
exit 1
21+
fi
22+
done
23+
24+
echo "Podman socket ready. Launching application..."
25+
exec /app/movement "$@"

docker/build/mtma/Dockerfile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
FROM nixos/nix:2.29.0 AS builder
2+
3+
RUN nix-env -iA nixpkgs.rsync nixpkgs.glibc nixpkgs.gawk
4+
5+
# Copy the source code into the container
6+
COPY . /tmp/build
7+
WORKDIR /tmp/build
8+
9+
# Set build to docker to skip the podman initilization while opening the flake
10+
ENV BUILD=docker
11+
12+
# Build the Rust application
13+
RUN nix --extra-experimental-features "nix-command flakes" \
14+
develop .#docker-build --command bash -c "cargo build --release -p mtma"
15+
16+
RUN rust_binary="./target/release/mtma"; dest_dir="/tmp/runtime"; \
17+
mkdir -p "$dest_dir"; ldd "$rust_binary" | awk '{print $3}' | \
18+
grep '^/' | xargs -I {} dirname {} | sort | uniq | xargs -I {} \
19+
bash -c 'mkdir -p "$0/$1" && rsync -a --copy-links "$1/" "$0/$1/"' "$dest_dir" {}
20+
21+
FROM alpine:3.22.0
22+
23+
# Create non-root user
24+
RUN adduser -u 1000 -D -s /bin/bash mtma
25+
26+
# Copy binary and runtime deps
27+
COPY --from=builder /tmp/build/target/release/mtma /app/mtma
28+
COPY --from=builder /tmp/runtime/nix/store /nix/store
29+
30+
# Environment setup
31+
ENV PATH="/nix/var/nix/profiles/default/bin:$PATH"
32+
ENV XDG_RUNTIME_DIR="/run/user/1000"
33+
ENV TMPDIR="/tmp"
34+
ENV DOCKER_HOST="unix:///run/user/1000/podman/podman-machine-default-api.sock"
35+
36+
# Create required runtime dirs with proper ownership
37+
RUN mkdir -p /run/user/1000/podman && \
38+
chown -R mtma:mtma /run/user/1000 /app /nix
39+
40+
# Copy runtime bootstrap script
41+
COPY docker/build/mtma/entry.sh /app/entry.sh
42+
RUN chmod +x /app/entry.sh
43+
44+
# Switch to non-root user
45+
USER mtma
46+
47+
# Entrypoint to bootstrap podman and launch mtma
48+
ENTRYPOINT ["/app/entry.sh"]

docker/build/mtma/entry.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Start Podman machine if not running
6+
if ! podman machine inspect podman-machine-default --format '{{.State}}' 2>/dev/null | grep -q 'running'; then
7+
echo "Starting podman machine..."
8+
podman machine start
9+
fi
10+
11+
# Wait for podman socket
12+
timeout=30
13+
elapsed=0
14+
while [ ! -S "$DOCKER_HOST" ]; do
15+
echo "Waiting for podman socket..."
16+
sleep 1
17+
elapsed=$((elapsed + 1))
18+
if [ "$elapsed" -ge "$timeout" ]; then
19+
echo "Timed out waiting for podman socket."
20+
exit 1
21+
fi
22+
done
23+
24+
echo "Podman socket ready. Launching application..."
25+
exec /app/mtma "$@"

flake.nix

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@
5454
zlib
5555
pandoc
5656
postgresql
57+
qemu_kvm
58+
qemu-utils
59+
libvirt
5760
] ++ lib.optionals stdenv.isDarwin [
5861
fixDarwinDylibNames
62+
] ++ lib.optionals stdenv.isLinux [
63+
virtiofsd
5964
];
6065

6166
sysDependencies = with pkgs; []
@@ -79,6 +84,7 @@
7984
process-compose
8085
jq
8186
docker
87+
podman
8288
solc
8389
grpcurl
8490
grpcui
@@ -120,6 +126,29 @@
120126
export CPPFLAGS="-I/opt/homebrew/opt/zlib/include"
121127
fi
122128
129+
# Check if podman machine exists and is running
130+
if [ "$BUILD" != "docker" ]; then
131+
if ! podman machine inspect podman-machine-default &>/dev/null; then
132+
echo "Initializing podman machine..."
133+
podman machine init
134+
podman machine start
135+
elif ! podman machine inspect podman-machine-default --format '{{.State}}' | grep -q 'running'; then
136+
echo "Starting podman machine..."
137+
podman machine start
138+
fi
139+
140+
# Find the actual podman socket location
141+
PODMAN_SOCKET=$(find /tmp/nix-shell.*/podman -name "podman-machine-default-api.sock" -type s 2>/dev/null | head -n 1)
142+
if [ -n "$PODMAN_SOCKET" ]; then
143+
export DOCKER_HOST="unix://$PODMAN_SOCKET"
144+
echo "Set DOCKER_HOST to Podman socket: $DOCKER_HOST"
145+
else
146+
echo "Warning: Could not find Podman socket"
147+
fi
148+
else
149+
echo "Build is docker podman will not be started."
150+
fi
151+
123152
# Add ./target/debug/* to PATH
124153
export PATH="$PATH:$(pwd)/target/debug"
125154

0 commit comments

Comments
 (0)