Skip to content

Instantly share code, notes, and snippets.

@tonistiigi
Last active September 16, 2021 20:51
Show Gist options
  • Save tonistiigi/239ed89a2cc70c086141def545890f44 to your computer and use it in GitHub Desktop.
Save tonistiigi/239ed89a2cc70c086141def545890f44 to your computer and use it in GitHub Desktop.
variable "DOCKER_IMAGE" {
default = "tonistiigi/hello-world"
}
group "default" {
targets = ["zstd-all"]
}
target "_all_arch" {
platforms = ["linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/ppc64le", "linux/s390x", "linux/riscv64"]
}
target "hello-world" {
tags = [DOCKER_IMAGE]
args = {
DOCKER_IMAGE = DOCKER_IMAGE
}
}
target "zstd" {
inherits = ["hello-world"]
args = {
DOCKER_GREETING = "Hello from Zstandard!"
}
output = [
"type=image,oci-mediatypes=true,compression=zstd,force-compression=true,compression-level=10,push=true"
]
}
target "zstd-all" {
inherits = ["_all_arch", "zstd"]
}
FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.0.0-rc.2 AS xx
FROM --platform=$BUILDPLATFORM alpine AS build
RUN apk add --no-cache clang lld llvm
COPY --from=xx / /
WORKDIR /src
COPY hello.c .
ARG TARGETPLATFORM
RUN xx-apk add --no-cache gcc musl-dev
ARG CFLAGS=-Os -fdata-sections -ffunction-sections -s
ARG DOCKER_IMAGE=tonistiigi/hello-world
ARG DOCKER_GREETING="Hello from Docker!"
RUN mkdir -p /out && \
xx-clang $CFLAGS -Wl,--gc-sections -static -D DOCKER_ARCH=\"$(xx-info arch)\" -D DOCKER_GREETING="\"${DOCKER_GREETING}\"" -D DOCKER_IMAGE=\"${DOCKER_IMAGE}\" hello.c -o /out/hello && \
llvm-strip --strip-all --remove-section=.comment /out/hello && \
xx-verify --static /out/hello
FROM scratch
COPY --from=build /out/hello /
ENTRYPOINT [ "/hello" ]
#include <sys/syscall.h>
#include <unistd.h>
#ifndef DOCKER_IMAGE
#define DOCKER_IMAGE "hello-world"
#endif
#ifndef DOCKER_GREETING
#define DOCKER_GREETING "Hello from Docker!"
#endif
#ifndef DOCKER_ARCH
#define DOCKER_ARCH "amd64"
#endif
const char message[] =
"\n"
DOCKER_GREETING "\n"
"This message shows that your installation appears to be working correctly.\n"
"\n"
"To generate this message, Docker took the following steps:\n"
" 1. The Docker client contacted the Docker daemon.\n"
" 2. The Docker daemon pulled the \"" DOCKER_IMAGE "\" image from the Docker Hub.\n"
" (" DOCKER_ARCH ")\n"
" 3. The Docker daemon created a new container from that image which runs the\n"
" executable that produces the output you are currently reading.\n"
" 4. The Docker daemon streamed that output to the Docker client, which sent it\n"
" to your terminal.\n"
"\n"
"To try something more ambitious, you can run an Ubuntu container with:\n"
" $ docker run -it ubuntu bash\n"
"\n"
"Share images, automate workflows, and more with a free Docker ID:\n"
" https://hub.docker.com/\n"
"\n"
"For more examples and ideas, visit:\n"
" https://docs.docker.com/get-started/\n"
"\n";
int main() {
//write(1, message, sizeof(message) - 1);
syscall(SYS_write, STDOUT_FILENO, message, sizeof(message) - 1);
//_exit(0);
//syscall(SYS_exit, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment