SoFunction
Updated on 2025-03-05

The golang program uses alpine to compile the minimum arm image implementation

The golang program uses alpine to compile the minimum arm image

Basic mirror preparation

If you want to create an image of the arm architecture, it will be executed on the arm machine. If you want to create an image of the amd, it will be executed on the amd machine, and you will download the basic image by yourself according to the machine architecture.

Here I take the machine with an arm architecture as an example

View Dockerfile

FROM alpine:latest
# Set the time zone to ShanghaiRUN apk add tzdata &&cp /usr/share/zoneinfo/Asia/Shanghai/etc/localtime \
	&&echo "Asia/Shanghai">/etc/timezone \
	&&apk del tzdata

Compile the mirror command and use it as the basic image docker build -t 192.168.1.187/library/alpine-arm64-go:v1.0.

golang program compilation image

Compile the Dockerfile as follows

FROM 192.168.1.187/library/alpine-arm64-go:v1.0
COPY analysis /opt/analysis
RUN chmod +x /opt/analysis
ENTRYPOINT ./opt/analysis -MQTT_IP $MQTT_IP -MQTT_PORT $MQTT_PORT -MQTT_USERNAME $MQTT_USERNAME -MQTT_PASSWORD $MQTT_PASSWORD

Compile command docker build -t 192.168.1.187/library/arm64-analysis:v1.0.

run

In the previous Dockerfile, we added the environment variables. When starting, we need to set the environment variables to start.

docker run -d --name analysis --restart=always -e MQTT_IP=192.168.1.188 -e MQTT_PORT=1883 -e MQTT_USERNAME=admin -e MQTT_PASSWORD=liang_2023 192.168.1.187/library/arm64-analysis:v1.0

The above is the detailed content of the golang program using alpine to compile the minimum arm image. For more information about go alpine compiling arm image, please pay attention to my other related articles!