Closed burdzwastaken closed 3 months ago
FYI docker images are already built with CGO_ENABLED=0 by default. Relevant config piece: https://github.com/sosedoff/pgweb/blob/master/.github/workflows/release.yml#L10
I don't believe that env var it is being passed along to the docker image
[statically-compiled-binaries] burdz@~/code/pgweb: cat Dockerfile.test
FROM docker.io/sosedoff/pgweb@sha256:18226b304e976592d87d5fc578c5da292d59b5c9e77f960cb5638a488ea3dbcb
USER root
RUN apt-get update && \
apt-get install -qq --no-install-recommends file
RUN file /usr/bin/pgweb
[statically-compiled-binaries] burdz@~/code/pgweb: docker build -f Dockerfile.test .
STEP 1/4: FROM docker.io/sosedoff/pgweb@sha256:18226b304e976592d87d5fc578c5da292d59b5c9e77f960cb5638a488ea3dbcb
STEP 2/4: USER root
--> Using cache 7d6a54297a4fc02addd8e3195ef650b11146ab9e5fff4615813ef0e1286fab14
--> 7d6a54297a4f
STEP 3/4: RUN apt-get update && apt-get install -qq --no-install-recommends file
Hit:1 http://deb.debian.org/debian bullseye InRelease
Get:2 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
Get:3 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
Get:4 http://deb.debian.org/debian-security bullseye-security/main amd64 Packages [270 kB]
Get:5 http://apt.postgresql.org/pub/repos/apt bullseye-pgdg InRelease [123 kB]
Get:6 http://apt.postgresql.org/pub/repos/apt bullseye-pgdg/main amd64 Packages [309 kB]
Fetched 795 kB in 2s (392 kB/s)
Reading package lists...
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libmagic-mgc.
(Reading database ... 9568 files and directories currently installed.)
Preparing to unpack .../libmagic-mgc_1%3a5.39-3+deb11u1_amd64.deb ...
Unpacking libmagic-mgc (1:5.39-3+deb11u1) ...
Selecting previously unselected package libmagic1:amd64.
Preparing to unpack .../libmagic1_1%3a5.39-3+deb11u1_amd64.deb ...
Unpacking libmagic1:amd64 (1:5.39-3+deb11u1) ...
Selecting previously unselected package file.
Preparing to unpack .../file_1%3a5.39-3+deb11u1_amd64.deb ...
Unpacking file (1:5.39-3+deb11u1) ...
Setting up libmagic-mgc (1:5.39-3+deb11u1) ...
Setting up libmagic1:amd64 (1:5.39-3+deb11u1) ...
Setting up file (1:5.39-3+deb11u1) ...
Processing triggers for libc-bin (2.31-13+deb11u8) ...
--> 558700a6436b
STEP 4/4: RUN file /usr/bin/pgweb
/usr/bin/pgweb: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, Go BuildID=K7xgUVwJgQUwaVJaEYVG/MTIDcSFDZZUppDZx9Mib/AvOIxxYUNCmaWruT2DfE/1AqmfWvCoiX1F4fYSNdY, stripped
COMMIT
--> 86d644ef5e18
86d644ef5e18ab20ed3c54a122ae6b150e0fdc4810cd621b5f0c9a46c97f41c2
sorry for missing the env var in the GitHub action, I'm happy to adjust my PR to pass the supplied env var in your GitHub action along to the docker build step
Hm i thought Actions env vars get propagated to steps without additional configuration, are you saying this is not the case with Pgweb?
actions environment variables get propagated to the steps however since the build is inside docker it does not inherit them. to handle this I propose we can use ARG
and ENV
inside docker and pass it along from actions via build-args. here is a sample pipeline I created to show the difference in behaviour:
branch: https://github.com/sosedoff/pgweb/compare/master...burdzwastaken:pgweb:statically-compiled-binaries-envvar?expand=1
build: https://github.com/burdzwastaken/pgweb/actions/runs/8419078468/job/23050925506
the change then which I am happy to make on my branch is to remove the make target that I added and update the Dockerfile/github actions files like so
[statically-compiled-binaries] burdz@~/code/pgweb: gd
diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml
index abcd259..d1ed1ca 100644
--- a/.github/workflows/docker.yml
+++ b/.github/workflows/docker.yml
@@ -33,3 +33,5 @@ jobs:
push: false
tags: pgweb:latest
platforms: linux/amd64,linux/arm64,linux/arm/v5,linux/arm/v7
+ build-args: |
+ "CGO_ENABLED=${{ env.CGO_ENABLED }}"
diff --git a/Dockerfile b/Dockerfile
index 6ef5d4f..9f9c70f 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -3,6 +3,10 @@
# ------------------------------------------------------------------------------
FROM golang:1.20-bullseye AS build
+# Set default build argument for CGO_ENABLED to 0
+ARG CGO_ENABLED=0
+ENV CGO_ENABLED ${CGO_ENABLED}
+
WORKDIR /build
RUN git config --global --add safe.directory /build
please let me know whichever method you prefer and I can update this branch accordingly
You're totally right about the env var propagation.
PR updated to just propagate the environment variable that already exists in the github action
@sosedoff thanks for merging, do you mind cutting a release so we can utilise the changes in docker behaviour?
I'll do some testing before cutting a new release but that should not take too long.
@sosedoff thank you so much and I appreciate your efforts maintaining this great tool
Description
This PR aims to enable the generation of statically linked binaries for increased portability and ease of deployment. Selfishly we would like to use your upstream docker images and copy the binary across to our internal base image so we can use a tool like dependabot/renovate to keep up to date with the latest version.
Please let me know if you would like this be handled in a different way
Changes
Testing