Skip
Arish's avatar

13. Dockerfile Instructions Deep Dive


FROM

dockerfile
1# Official image
2FROM ruby:3.2
3
4# Specific version
5FROM ruby:3.2.2-slim-bullseye
6
7# Multi-stage base
8FROM ruby:3.2 AS builder

WORKDIR

dockerfile
1WORKDIR /app
2# All following commands run from /app

COPY vs ADD

dockerfile
1# COPY - simple copy
2COPY package.json .
3COPY src/ ./src/
4
5# ADD - extra features
6ADD https://example.com/file.tar.gz /tmp/
7ADD archive.tar.gz /app/  # Auto-extracts

Prefer COPY unless you need ADD features.

RUN

dockerfile
1# Shell form
2RUN apt-get update && apt-get install -y git
3
4# Exec form
5RUN ["apt-get", "install", "-y", "git"]
6
7# Multi-line
8RUN apt-get update && \
9    apt-get install -y \
10    git \
11    curl \
12    && rm -rf /var/lib/apt/lists/*

ENV

dockerfile
1ENV RAILS_ENV=production
2ENV NODE_ENV=production APP_HOME=/app

ARG

dockerfile
1ARG RUBY_VERSION=3.2
2FROM ruby:${RUBY_VERSION}
3
4# Build with: docker build --build-arg RUBY_VERSION=3.1 .