Skip to content

Instantly share code, notes, and snippets.

@lantrix
Created June 21, 2021 06:25
Show Gist options
  • Save lantrix/4138333011bdbc2ffdb07768fbc86964 to your computer and use it in GitHub Desktop.
Save lantrix/4138333011bdbc2ffdb07768fbc86964 to your computer and use it in GitHub Desktop.
Compile and run a simple C Hello World app using Docker scratch & Distroless
#!/bin/bash -e
docker build -t myapp -f Dockerfile.distroless .
docker run --rm -t myapp
#!/bin/bash -e
docker run --rm -v $PWD:/build ubuntu:latest /bin/bash -c \
"apt-get update && apt-get -y install build-essential && \
cd /build && \
gcc -o hello -static -nostartfiles hello.c"
docker build --tag hello -f Dockerfile.scratch .
docker run --rm -t hello
FROM ubuntu:latest as builder
WORKDIR /build
ADD . /build
RUN apt-get update && apt-get -y install build-essential
RUN gcc -o hello -static -nostartfiles hello.c
# Now copy it into our base image.
FROM gcr.io/distroless/static-debian10
COPY --from=builder /build/hello /
CMD ["/hello"]
FROM scratch
ADD hello /
CMD ["/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;
}
@lantrix
Copy link
Author

lantrix commented Jun 21, 2021

The docker image sizes are:

$ docker image ls
REPOSITORY           TAG        IMAGE ID       CREATED          SIZE
hello                latest     82cbe50a3059   3 minutes ago    10.4kB
myapp                latest     b778045f2958   7 minutes ago    1.83MB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment