Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up cross compiling for a Docker container that runs on Raspberry Pi #176

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions .github/workflows/flow--new-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,48 @@ jobs:
test:
uses: ./.github/workflows/job--test-and-lint.yml

build:
build-server:
uses: ./.github/workflows/job--build-and-tag.yml
needs: test
with:
TAG: latest
BASE_IMAGE: node:16-slim
DOCKER_DIRECTORY: docker/server
DOCKER_IMAGE: cimonitor/server:latest
secrets:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}

build-server-arm:
uses: ./.github/workflows/job--build-and-tag.yml
needs: test
with:
BASE_IMAGE: arm32v7/node:16-slim
DOCKER_DIRECTORY: docker/server
DOCKER_IMAGE: cimonitor/server:latest-arm
QEMU_ARCH: arm
secrets:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}

build-module-client:
uses: ./.github/workflows/job--build-and-tag.yml
needs: test
with:
BASE_IMAGE: node:16-slim
DOCKER_DIRECTORY: docker/module-client
DOCKER_IMAGE: cimonitor/module-client:latest
secrets:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}

build-module-client-arm:
uses: ./.github/workflows/job--build-and-tag.yml
needs: test
with:
BASE_IMAGE: arm32v7/node:16-slim
DOCKER_DIRECTORY: docker/module-client
DOCKER_IMAGE: cimonitor/module-client:latest-arm
QEMU_ARCH: arm
secrets:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
41 changes: 39 additions & 2 deletions .github/workflows/flow--new-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,48 @@ jobs:
test:
uses: ./.github/workflows/job--test-and-lint.yml

build:
build-server:
uses: ./.github/workflows/job--build-and-tag.yml
needs: test
with:
TAG: ${GITHUB_REF/refs\/tags\//}
BASE_IMAGE: node:16-slim
DOCKER_DIRECTORY: docker/server
DOCKER_IMAGE: cimonitor/server:${GITHUB_REF/refs\/tags\//}
secrets:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}

build-server-arm:
uses: ./.github/workflows/job--build-and-tag.yml
needs: test
with:
BASE_IMAGE: arm32v7/node:16-slim
DOCKER_DIRECTORY: docker/server
DOCKER_IMAGE: cimonitor/server:${GITHUB_REF/refs\/tags\//}-arm
QEMU_ARCH: arm
secrets:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}

build-module-client:
uses: ./.github/workflows/job--build-and-tag.yml
needs: test
with:
BASE_IMAGE: node:16-slim
DOCKER_DIRECTORY: docker/module-client
DOCKER_IMAGE: cimonitor/module-client:${GITHUB_REF/refs\/tags\//}
secrets:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}

build-module-client-arm:
uses: ./.github/workflows/job--build-and-tag.yml
needs: test
with:
BASE_IMAGE: arm32v7/node:16-slim
DOCKER_DIRECTORY: docker/module-client
DOCKER_IMAGE: cimonitor/module-client:${GITHUB_REF/refs\/tags\//}-arm
QEMU_ARCH: arm
secrets:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
27 changes: 24 additions & 3 deletions .github/workflows/job--build-and-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ name: Build and tag CIMonitor container
on:
workflow_call:
inputs:
TAG:
BASE_IMAGE:
required: true
type: string
DOCKER_DIRECTORY:
default: docker/server
required: false
type: string
QEMU_ARCH:
required: false
type: string
DOCKER_IMAGE:
required: true
type: string
secrets:
Expand All @@ -27,8 +37,19 @@ jobs:
- name: Install production dependencies
run: yarn install --production --ignore-scripts

- name : Install QEMU emulator
run: |
if [ -n "${{ inputs.QEMU_ARCH }}" ]; then
wget -q https://github.com/multiarch/qemu-user-static/releases/download/v3.0.0/x86_64_qemu-${{ inputs.QEMU_ARCH }}-static.tar.gz
tar xfvz x86_64_qemu-${{ inputs.QEMU_ARCH }}-static.tar.gz
docker run --rm --privileged multiarch/qemu-user-static:register --reset
fi

- name: Copy Docker files
run: cp ${{ inputs.DOCKER_DIRECTORY }}/Dockerfile ${{ inputs.DOCKER_DIRECTORY }}/.dockerignore .

- name: Build CIMonitor image
run: docker build --target production --tag cimonitor/server:${{ inputs.TAG }} .
run: docker build --target production --tag ${{ inputs.DOCKER_IMAGE }} --build-arg BASE_IMAGE=${{ inputs.BASE_IMAGE }} --build-arg QEMU_BINARY=qemu-${{ inputs.QEMU_ARCH }}-static .

- name: Log in to Docker Hub
uses: docker/login-action@v1
Expand All @@ -37,4 +58,4 @@ jobs:
password: ${{ secrets.DOCKER_TOKEN }}

- name: Push docker image to the hub
run: docker push cimonitor/server:${{ inputs.TAG }}
run: docker push ${{ inputs.DOCKER_IMAGE }}
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:

server:
build:
context: .
context: docker/server
target: server
volumes:
- .:/CIMonitor
Expand All @@ -15,16 +15,16 @@ services:

module-client:
build:
context: .
target: server
context: docker/module-client
target: module-client
volumes:
- .:/CIMonitor
user: ${USERID}:${GROUPID}
command: sh -c "npm run module-client"

dashboard:
build:
context: .
context: docker/server
target: dashboard
volumes:
- .:/CIMonitor
Expand Down
10 changes: 10 additions & 0 deletions docker/module-client/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Ignore everything...
**

# ...but keep files/dirs:
!/app
!/node_modules
!/package.json

# ... and also keep the QEMU emulator for cross compiling:
!/qemu-*-static
51 changes: 51 additions & 0 deletions docker/module-client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
ARG QEMU_BINARY=notset
ARG BASE_IMAGE=node:16-slim

# ==================================
# Base stage
# ==================================

FROM ${BASE_IMAGE} as base

WORKDIR /CIMonitor

# ==================================
# Local development module-client
# ==================================

FROM base as module-client

ENV NODE_PATH=/CIMonitor

CMD ["npm", "run", "module-client"]

# ==================================
# Production build - QEMU emulator
# ==================================

FROM base as production-qemu

COPY ./${QEMU_BINARY}* /usr/bin/

RUN echo "deb http://deb.debian.org/debian unstable main contrib non-free" >> /etc/apt/sources.list \
&& apt-get update \
&& apt-get install dumb-init

# ==================================
# Production
# ==================================

FROM base as production

ENV NODE_ENV=production
ENV NODE_PATH=/CIMonitor/app

# Copy dumb-init from the QEMU stage
COPY --from=production-qemu /usr/bin/dumb-init /usr/bin/dumb-init

# Make sure "yarn build" is ran before building the production container
COPY . /CIMonitor

ENTRYPOINT ["/usr/bin/dumb-init", "--"]

CMD ["npm", "run", "start-client"]
5 changes: 4 additions & 1 deletion .dockerignore → docker/server/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Ignore everything...
*
**

# ...but keep files/dirs:
!/app
!/dashboard
!/node_modules
!/package.json

# ... and also keep the QEMU emulator for cross compiling:
!/qemu-*-static
23 changes: 19 additions & 4 deletions Dockerfile → docker/server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
ARG QEMU_BINARY=notset
ARG BASE_IMAGE=node:16-slim

# ==================================
# Base stage
# ==================================

FROM node:16-alpine as base
FROM ${BASE_IMAGE} as base

WORKDIR /CIMonitor

Expand All @@ -24,21 +27,33 @@ FROM base as dashboard

CMD ["npm", "run", "dashboard"]

# ==================================
# Production build - QEMU emulator
# ==================================

FROM base as production-qemu

COPY ./${QEMU_BINARY}* /usr/bin/

RUN echo "deb http://deb.debian.org/debian unstable main contrib non-free" >> /etc/apt/sources.list \
&& apt-get update \
&& apt-get install dumb-init

# ==================================
# Production
# ==================================

FROM base as production

ENV NODE_ENV=production

ENV NODE_PATH=/CIMonitor/app

# Copy dumb-init from the QEMU stage
COPY --from=production-qemu /usr/bin/dumb-init /usr/bin/dumb-init

# Make sure "yarn build" is ran before building the production container
COPY . /CIMonitor

RUN apk add --no-cache dumb-init

ENTRYPOINT ["/usr/bin/dumb-init", "--"]

CMD ["npm", "run", "start"]