如何使用Docker在树莓派上运行Spring Boot应用程序

Docker is a really useful tool for running applications because it enables you to easily pack, deploy, and run any application, which can run anywhere. As Docker containers don’t have much overhead you can even run them on low-end IoT devices. Like Raspberry Pi (even though they’re quite powerful nowadays!)

Docker是运行应用程序的真正有用的工具,因为它使您能够轻松打包,部署和运行任何可在任何地方运行的应用程序。 由于Docker容器没有太多开销,您甚至可以在低端IoT设备上运行它们。 就像Raspberry Pi(即使它们现在很强大!)

Building the Docker images for Raspberry Pi is a bit different due to the ARM-based CPUs it has but luckily Docker’s new multi-arch builds makes this much easier. Let’s go through how to build a custom Docker image for Raspberry Pi and run a Spring Boot application with it.

由于Raspberry Pi具有基于ARM的CPU,因此为Raspberry Pi构建Docker映像有些不同,但是幸运的是Docker的新多体系结构使此操作变得更加容易。 让我们看一下如何为Raspberry Pi构建自定义Docker映像并使用它运行Spring Boot应用程序。

设置Raspberry Pi (Setting up Raspberry Pi)

I’m using the great chilipie-kiosk image from Futurice. It allows booting directly into full-screen Chrome so you can easily start your web app on boot.

我正在使用来自Futurice的出色的辣椒酱-kiosk图像。 它允许直接启动到全屏Chrome浏览器,因此您可以在启动时轻松启动Web应用程序。

After you have the Raspberry Pi running chilipie-kiosk, you can go into installing Docker. Running the standard installation script will do the trick:

在Raspberry Pi运行chilipie-kiosk之后,您可以开始安装Docker。 运行标准安装脚本将达到目的:

curl -fsSL https://get.docker.com -o get-docker.shsh get-docker.sh

建立应用程式 (Building the application)

You obviously need to package your application into a Docker image first. There are many ways to do it so I’m not going into details on how to do it.

显然,您首先需要将应用程序打包到Docker映像中。 有很多方法可以做到这一点,因此我不再赘述如何做。

Here’s a Dockerfile for an application with Spring boot backend and AngularJs front end (yeah, it’s an old app). The following multi-stage build first builds the backend & front end and then combines them into the final image:

这是用于具有Spring引导后端和AngularJs前端的应用程序的Dockerfile(是的,这是一个旧应用程序)。 以下多阶段构建首先构建后端和前端,然后将它们组合为最终映像:

# Frontend
# -----------------
FROM tiangolo/node-frontend:10 as frontend


WORKDIR /tmp/app


ADD frontend/package.json .
ADD frontend/package-lock.json .
ADD frontend/bower.json .
RUN npm install
RUN bower update --allow-root
ADD frontend .
RUN gulp build


# Backend
# -----------------
FROM gradle:jdk11 as backend


ADD init.gradle /root/.gradle/init.gradle
WORKDIR /tmp/app


# Download gradle
ADD gradlew .
ADD gradle ./gradle
RUN touch build.gradle
RUN ./gradlew tasks --no-daemon


ADD settings.gradle .
ADD build.gradle .
ADD backend ./backend


RUN ./gradlew :backend:installDist --no-daemon


# Final image
# -----------------
FROM arm32v7/adoptopenjdk:11-jre-hotspot


COPY --from=backend /tmp/app/backend/build/install/myproject /myproject
COPY --from=frontend /tmp/app/dist /myproject/static


ENV SPRING_RESOURCES_STATICLOCATIONS file:/myproject/static
EXPOSE 8080


ENTRYPOINT /myproject/bin/myproject

A couple of things to notice here:

这里需要注意几件事:

  • As you’re building for Raspberry Pi (i.e. ARMv7 architecture), you must use ARM-compatible images. Luckily many images are.

    在为Raspberry Pi(即ARMv7架构)构建时,必须使用与ARM兼容的映像。 幸运的是有很多图像。
  • With multi-stage builds, also the intermediate images must be built for ARM.

    对于多阶段构建,还必须为ARM构建中间映像。
  • Here we’re using ARM32 specific base image so you can’t actually run this on any other architecture.

    在这里,我们使用的是ARM32特定的基本映像,因此您实际上不能在任何其他体系结构上运行它。

建立多架构图像 (Building multi-architecture image)

Docker’s buildx functionality enables you to build Docker images for different architecture than where you’re building the image in. E.g. you can build an image for ARM32 on a machine running on AMD64. You can use either remote builders that are running on different architectures or QEMU emulator. Since you probably don’t have any build farm, let’s use the QEMU emulator.

Docker的buildx功能使您可以为与构建映像所在位置不同的体系结构构建Docker映像。例如,可以在运行AMD64的计算机上为ARM32生成映像。 您可以使用在不同体系结构上运行的远程构建器,也可以使用QEMU仿真器 。 由于您可能没有任何构建场,因此让我们使用QEMU仿真器。

Docker buildx is still an experimental feature so you must enable it with “DOCKER_CLI_EXPERIMENTAL” environment variable. Otherwise, you’ll see the “docker: ‘buildx’ is not a docker command.” error from Docker CLI.

Docker buildx仍是一项试验性功能,因此您必须使用“ DOCKER_CLI_EXPERIMENTAL”环境变量启用它。 否则,您将看到“ docker:'buildx'不是docker命令。 ”来自Docker CLI的错误。

Here’s a Gitlab .gitlab-ci.yml for building the multi-architecture images for the Spring boot app:

这是一个Gitlab .gitlab-ci.yml,用于为Spring启动应用程序构建多体系结构映像:

deploy:
  image: docker:19.03.6
  services:
    - name: docker:19.03.6-dind
      command: ["--experimental"]
  stage: deploy
  variables:
    IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
    DOCKER_BUILDKIT: 1
    DOCKER_CLI_EXPERIMENTAL: enabled
    BUILDX_URL: https://github.com/docker/buildx/releases/download/v0.4.2/buildx-v0.4.2.linux-amd64
    BUILDX_BUILDER_NAME: mybuilder
    BUILDX_TARGET_PLATFORMS: linux/arm/v7


  before_script:
    - mkdir -p $HOME/.docker/cli-plugins/
    - wget -O $HOME/.docker/cli-plugins/docker-buildx $BUILDX_URL
    - chmod a+x $HOME/.docker/cli-plugins/docker-buildx
    - docker run --rm --privileged multiarch/qemu-user-static:register --reset -p yes
    - docker buildx create --use --driver docker-container --name ${BUILDX_BUILDER_NAME} --platform=${BUILDX_TARGET_PLATFORMS}
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY


  script:
    - docker buildx build --platform "$BUILDX_TARGET_PLATFORMS" -t "$IMAGE_TAG" . --push

It uses Docker version 19.03.6 and first installs the latest version of buildx from its Git repository in the before_script block. Then we register the QEMU binary using the multiarch/qemu-user-static Docker image.

它使用Docker版本19.03.6,并首先从其Git存储库中的before_script块中安装了最新版本的buildx。 然后,我们使用multiarch / qemu-user-static Docker映像注册QEMU二进制文件。

Next, you need to create a buildx builder for building images for the platforms you’re interested in. Here, we’re only specifying one target platform, linux/arm/v7, which is suitable for Raspberry Pi. Finally, running “docker buildx build” will build the image and push it to the Gitlab’s container registry.

接下来,您需要创建一个buildx构建器,用于为您感兴趣的平台构建映像。在这里,我们仅指定一个适用于Raspberry Pi的目标平台linux / arm / v7 。 最后,运行“ docker buildx build”将构建映像并将其推送到Gitlab的容器注册表。

在Raspberry Pi上运行应用程序 (Running the app on Raspberry Pi)

Running the application on Raspberry Pi is as easy as running any Docker container anywhere. For pulling the images from Gitlab’s Container Registry on Raspberry Pi, you first need to create Personal Access Token and the log in as “gitlab-ci-token”.

在Raspberry Pi上运行应用程序就像在任何地方运行任何Docker容器一样容易。 要从Raspberry Pi上的Gitlab的Container Registry中提取图像,首先需要创建Personal Access Token,并以“ gitlab-ci-token”身份登录。

docker login -u gitlab-ci-token -p <TOKEN> registry.gitlab.com
docker run -p 8080:8080 registry.gitlab.com/YOUR_NAME/myproject:latest &

There you go! All set! Running the application as a Docker container makes it super easy to update it as well. Just have a cron job to periodically pull the images from the registry and restart the container when there is a new version of the image.

你去! 可以了,好了! 将应用程序作为Docker容器运行使其也非常容易更新。 只需执行cron作业即可定期从注册表中提取图像,并在有图像的新版本时重新启动容器。

翻译自: https://medium.com/swlh/how-to-run-spring-boot-application-on-raspberry-pi-using-docker-d633e15ffff2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值