This guide provides a quick reference for creating Dockerfiles, which are used to define the configuration and dependencies for building Docker images.
A Dockerfile is a text document containing a set of instructions that Docker uses to create a Docker image. It defines the base image, sets up the environment, and specifies the commands to run within the container.
A simple Dockerfile typically consists of the following elements:
# Use an official base image
FROM ubuntu:latest
# Set the working directory
WORKDIR /app
# Copy application files into the container
COPY . .
# Install dependencies
RUN apt-get update && apt-get install -y python3
# Define the default command to run when the container starts
CMD ["python3", "app.py"]
FROM
: Specifies the base image. WORKDIR
: Sets the working directory inside the container. COPY
: Copies files from the local machine to the container. RUN
: Executes commands during image build. CMD
or ENTRYPOINT
: Specifies the default command to run when the container starts.
FROM ubuntu:latest
Specifies the base image for the Docker image.
WORKDIR /app
Sets the working directory inside the container.
COPY . .
Copies files from the local machine to the container.
RUN apt-get update && apt-get install -y python3
Executes commands during image build.
CMD ["python3", "app.py"]
Specifies the default command to run when the container starts.
Build the Docker image from the Dockerfile:
docker build -t my-app-image .
Run a container from the built image:
docker run my-app-image
.dockerignore
file to exclude unnecessary files during the build. Go back to Docker Guide.
Visit other Developer Guides.