(From Docker documentation)
Dockerfileis a text document that contains all the commands a user could call on the command line to assemble an image. Using
docker buildusers can create an automated build that executes several command-line instructions in succession.
Let's take a look at our first
FROM alpine:latest RUN adduser -D johndoe USER johndoe CMD echo "Hello from John's image!"`
Dockerfilealways starts with a
FROMinstruction, which specifies the base image to use. In this example we stick to
RUNtells Docker to execute a command, for instance, to create a user
USERtells Docker the user to run as when a container is created from this image.
CMDspecifies the default command to run when a container is created. It can be overridden by appending to the end of the
docker runcommand, which you have already seen in previous scenarios.
docker build -t johnsimage:auto ~/johnsimage/ to build an image called
johnsimage:auto. The path at the end of the command line is the build context. Docker will by default look for a file named "
Dockerfile" in that path.
As you can see from the output, Docker starts a temporary container (
---> Running in <container-id>) for each instruction in the
Dockerfile, execute the instruction, and commit it as a temporary image (
---> <image-id> below each
These temporary images are visible via
docker images -a
Now try start a container from this image:
docker run --rm johnsimage:auto
docker run --rm johnsimage:auto whoami
docker run --rm -u nobody johnsimage:auto whoami
docker run --rm johnsimage:auto echo "Hej hej."