Table of Contents

Part 2:

What is a Dockerfile?

A Dockerfile is a script containing a set of instructions used by Docker to automate the process of building a new container image. It defines the environment inside the container, installs necessary software, sets up commands, and more.

Basic Structure of a Dockerfile

A Dockerfile consists of a series of instructions and arguments. Each instruction is an operation used to build the image, like installing a software package or copying files. The instruction is written in uppercase, followed by its arguments.

Key Dockerfile Instructions

  `FROM`: Specifies the base image to start from. It's usually an OS or another application.
      Example: `FROM ubuntu:20.04`
  `LABEL`: Adds metadata to the image, like maintainer information.
      Example: `LABEL maintainer="[email protected]"`
  `RUN`: Executes commands in a new layer on top of the current image and commits the result.
      Example: `RUN apt-get update && apt-get install -y nginx`
  `CMD`: Provides defaults for the executing container. There can only be one CMD instruction in a Dockerfile.
      Example:` CMD ["nginx", "-g", "daemon off;"]`
  `ENTRYPOINT`: Configures the container to run as an executable. It's often used in combination with CMD.
      Example: `ENTRYPOINT ["nginx"]`
  `COPY`: Copies files or directories from the host machine to the container.
      Example: `COPY ./webapp /var/www/webapp`
  `ADD`: Similar to COPY, but can also handle URLs and tarball extraction.
      Example: `ADD https://example.com/app.tar.gz /app/`
  `WORKDIR`: Sets the working directory for any subsequent `RUN`, `CMD`, `ENTRYPOINT`,` COPY`, and `ADD` instructions.
      Example: `WORKDIR /app`
  `EXPOSE`: Informs Docker that the container listens on the specified network port at runtime.
      Example: `EXPOSE 80`
  `ENV`: Sets environment variables.
      Example: `ENV MY_VARIABLE=value`
  `VOLUME`: Creates a mount point for external storage or other containers.
      Example: `VOLUME /data`

Let's create a Dockerfile for a basic web server using Nginx:

First, create a folder called `my-webserver` and go inside it `cd my-webserver`

Then create another folder inside that called 'website' and a file called 'index.html' within the folder 'website' with any content of your choice.

Create a file named 'dockerfile' with the following content within the 'my-webserver' folder. (ie. my-webserver/website/index.html and my-webserver/dockerfile)

lknog}

Building an Image from a Dockerfile

To build a Docker image from your Dockerfile, navigate to the directory containing the Dockerfile and run:

`docker build -t my-webserver:latest .`

This command tells Docker to build an image using the Dockerfile in the current directory (.) and tag it as `my-webserver:latest`.

Best Practices

• Minimize Layers: Try to reduce the number of layers in your image to make it lightweight. For instance, chain commands using && in a single RUN instruction. • Use .dockerignore: Just like .gitignore, you can use .dockerignore to exclude files that aren't needed in the container. • Avoid Installing Unnecessary Packages: Only install the packages that are necessary to run your application. • Clean Up: Remove temporary files and caches to reduce image size.