Dockerfiles - docker/genai-stack

Dockerfiles are configuration files used by Docker to build automated images for your application. They are a series of instructions, written in a specific format, that Docker follows to create a Docker image. This article will explain the purpose and contents of a Dockerfile, using the project “genai-stack” as an example.

Purpose of a Dockerfile

The purpose of a Dockerfile is to automate the creation of a Docker image. It contains all the necessary instructions to build a Docker image, such as installing dependencies, copying files, and setting environment variables. By using a Dockerfile, you can ensure that your application is consistently built and deployed, regardless of the environment.

Contents of a Dockerfile

A Dockerfile contains a series of instructions, each on a new line, that Docker follows to build an image. The following is a list of common instructions found in a Dockerfile:

  • FROM: specifies the parent image for the new image.
  • RUN: runs a command in the image.
  • COPY: copies files or directories from the host machine to the image.
  • ADD: similar to COPY, but with additional features such as automatic extraction of tar files.
  • ENV: sets environment variables in the image.
  • WORKDIR: sets the working directory for subsequent instructions.
  • EXPOSE: informs Docker that the image listens on a specific network port at runtime.
  • CMD: specifies the default command to run when the container starts.

Example Dockerfile

Here is an example Dockerfile for the genai-stack project:

# Use an official Python runtime as a parent image
FROM python:3.9-slim-buster

# Set the working directory in the container
WORKDIR /app

# Add the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Run app.py when the container launches
CMD ["python", "app.py"]

This Dockerfile uses the official Python 3.9 image as the parent image, sets the working directory to /app, copies the contents of the current directory into the container, installs any required packages, exposes port 80, and runs the app.py script when the container starts.

Conclusion

Dockerfiles are essential for automating the creation of Docker images. By using a Dockerfile, you can ensure that your application is consistently built and deployed, regardless of the environment. The genai-stack project provides an excellent example of a Dockerfile, demonstrating common instructions and best practices.