Skip
Arish's avatar

12. Dockerfile Fundamentals


What is a Dockerfile?

A text file with instructions to build a Docker image.

Basic Structure

dockerfile
1# Base image
2FROM ruby:3.2
3
4# Set working directory
5WORKDIR /app
6
7# Copy files
8COPY . .
9
10# Install dependencies
11RUN bundle install
12
13# Expose port
14EXPOSE 3000
15
16# Start command
17CMD ["rails", "server", "-b", "0.0.0.0"]

Common Instructions

InstructionDescription
FROMBase image
WORKDIRSet working directory
COPYCopy files into image
ADDCopy with extras (URLs, tar)
RUNExecute command
ENVSet environment variable
EXPOSEDocument port
CMDDefault command
ENTRYPOINTFixed command

Building

bash
1docker build -t myapp .
2docker build -t myapp:v1.0 -f Dockerfile.prod .

Build Context

The . in docker build -t myapp . is the build context - files available during build.