Docker Community Forums

Share and learn in the Docker community.

Hello.c and scratch


(Jerryb) #1

Trying to follow instructions here: https://docs.docker.com/engine/userguide/eng-image/baseimages/#create-a-simple-parent-image-using-scratch

Did this:

$ docker run --rm -it -v $PWD:/build ubuntu:16.04
container# apt-get update && apt-get install build-essential
container# cd /build
container# gcc -o hello -static -nostartfiles hello.c

but after that, this step

Then you can run it (on Linux, Mac, or Windows) using: docker run --rm hello

Failed with:

PS C:\Users\Jerry> docker run --rm hello
Unable to find image ‘hello:latest’ locally

What am I missing?


(Sam) #2

yeh, they did a poor job on the sample…

there are three steps…

  1. build the hello binary
$ docker run --rm -it -v $PWD:/build ubuntu:16.04
container# apt-get update && apt-get install build-essential
container# cd /build
container# gcc -o hello -static -nostartfiles hello.c

the binary is put in the current directory because of the volume mapping

-v $PWD:/build
  1. build the hello image (build uses the binary built in step 1, ADD hello /)
You can use Docker’s reserved, minimal image, scratch, as a starting point for building containers. Using the scratch “image” signals to the build process that you want the next command in the Dockerfile to be the first filesystem layer in your image.

While scratch appears in Docker’s repository on the hub, you can’t pull it, run it, or tag any image with the name scratch. Instead, you can refer to it in your Dockerfile. For example, to create a minimal container using scratch:

create a Dockerfile  like this

FROM scratch
ADD hello /
CMD ["/hello"]

Assuming you built the “hello” executable example from the Docker GitHub example C-source code, and you compiled it with the -static flag, you can then build this Docker image using: 
 docker build --tag hello .
  1. THEN you can run it
Then you can run it (on Linux, Mac, or Windows) using: 
docker run --rm hello