Difficulty: beginner
Estimated Time: 10 minutes

In this scenario, we'll explore how you can build a docker image from a Dockerfile.

Docker can build images automatically by reading the instructions from a Dockerfile, a text file containing all the commands, in order, needed to build a given image. Dockerfiles adhere to a specific format and use a specific set of instructions.

In this scenario we explored how to build an image from Dockerfile.

We also learned how to use COPY instruction to copy files from host to container at the time of build.

In the last step we explored the difference between CMD and ENTRYPOINT.

Building images with Dockerfile

Step 1 of 6

Step 1

Write Dockerfile

In this step we shall create Dockerfile.

Create a Dockerfile with following content in current directory. cat > Dockerfile << EOF FROM ubuntu:18.04 RUN apt-get update && apt-get install apache2 -y && apt-get clean CMD ["apache2ctl", "-DFOREGROUND"] EOF

Verify the file: cat Dockerfile

Above Dockerfile content describes a Debian jessie Docker image with Apache web server installed.

  • FROM indicates the base image for our build
  • Each RUN line will be executed by Docker during the build
  • Our RUN commands must be non-interactive. (You can‘t provide input to Docker during the build.)
  • In many cases, we will add the -y flag to apt-get.