Docker file explained - mnhmilu/poc-kubernets GitHub Wiki

A Dockerfile is a plain text configuration file used to define the steps and instructions for building a Docker image. A Docker image is a lightweight, standalone, and executable package that contains everything needed to run a piece of software, including the code, runtime, libraries, environment variables, and system tools. Docker images are created from Dockerfiles, and they serve as the basis for running containers.

A typical Dockerfile consists of a series of instructions that Docker uses to assemble the image layer by layer. Each instruction in the Dockerfile adds a new layer to the image, and these layers are cached to speed up the image building process.

Here's a basic explanation of some common instructions found in a Dockerfile:

  1. FROM: This is the first instruction in a Dockerfile and specifies the base image on which the new image will be built. It is like choosing an operating system for the image. For example, FROM ubuntu:20.04 selects Ubuntu 20.04 as the base image.

  2. RUN: The RUN instruction is used to execute commands inside the image during the build process. These commands install software packages, configure settings, or perform other tasks required to set up the environment. For example, RUN apt-get update && apt-get install -y nginx installs Nginx web server in the image.

  3. COPY or ADD: These instructions are used to copy files or directories from the host machine into the image. For example, COPY app.py /app/ copies the file app.py from the host's current directory into the /app/ directory of the image.

  4. WORKDIR: This sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, or ADD instructions that follow it. It is used to define the working directory within the container where commands will be executed.

  5. EXPOSE: This instruction informs Docker that the container will listen on the specified network ports at runtime. It does not actually publish the ports; it is merely a documentation feature. For example, EXPOSE 80 indicates that the container will listen on port 80.

  6. CMD or ENTRYPOINT: These instructions define the command that will be executed when a container is run. CMD is used to provide default arguments to the entrypoint command, while ENTRYPOINT sets the main command without any default arguments.

A basic Dockerfile might look like this:

# Use an official Python image as the base
FROM python:3.9

# Set the working directory inside the container
WORKDIR /app

# Copy the current directory's contents into the container's working directory
COPY . .

# Install any necessary dependencies
RUN pip install -r requirements.txt

# Expose port 80 to the host machine
EXPOSE 80

# Set the command to run the application
CMD ["python", "app.py"]

To build a Docker image from a Dockerfile, you would use the docker build command, providing the path to the directory containing the Dockerfile:

docker build -t my_image_name .

After building the image, you can create and run a container from it using the docker run command:

docker run -d -p 80:80 my_image_name

This command runs the container in the background (-d), mapping port 80 of the host to port 80 of the container, and using the specified image (my_image_name).