Dockerfile

Introduction to Dockerfile

A Dockerfile is a text document that contains all the commands to assemble an image. Using docker build, users can create an automated build that executes several command-line instructions in succession.

Basic Structure of a Dockerfile

Here's a simple example of a Dockerfile:

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

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the current directory contents into the container at /usr/src/app
COPY . .

# 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"]

Key Directives

  • FROM: Specifies the base image.

  • WORKDIR: Sets the working directory inside the container.

  • COPY: Copies files or directories from the host.

  • RUN: Executes a command in a new layer on top of the current image.

  • EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.

  • CMD: Provides the default command to run when the container starts.

Last updated