Logo

Dockerfile Basics Guide

This guide provides a quick reference for creating Dockerfiles, which are used to define the configuration and dependencies for building Docker images.

Table of Contents

  1. Understanding Dockerfile
  2. Basic Structure
  3. Common Instructions
  4. Building and Running Docker Image
  5. Best Practices

1. Understanding Dockerfile

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.

2. Basic Structure

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

3. Common Instructions

3.1. FROM


FROM ubuntu:latest

Specifies the base image for the Docker image.

3.2. WORKDIR


WORKDIR /app

Sets the working directory inside the container.

3.3. COPY


COPY . .

Copies files from the local machine to the container.

3.4. RUN


RUN apt-get update && apt-get install -y python3

Executes commands during image build.

3.5. CMD or ENTRYPOINT


CMD ["python3", "app.py"]

Specifies the default command to run when the container starts.

4. Building and Running Docker Image

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

5. Best Practices


Go back to Docker Guide.

Visit other Developer Guides.