SyntaxStudy
Sign Up
Docker Writing a Dockerfile
Docker Beginner 12 min read

Writing a Dockerfile

A Dockerfile is a text file that contains instructions for building a Docker image. Docker reads the Dockerfile and executes each instruction in order to build the image layer by layer.

Example
# Dockerfile for a Node.js application

# Base image (always start from an existing image)
FROM node:18-alpine

# Set working directory inside the container
WORKDIR /app

# Copy package files first (for caching)
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production

# Copy the rest of the application
COPY . .

# Expose port (documentation only - doesn't publish it)
EXPOSE 3000

# Environment variable
ENV NODE_ENV=production

# Default command to run when container starts
CMD ["node", "server.js"]

# Build the image:
# docker build -t my-node-app .

# Run a container from the image:
# docker run -d -p 3000:3000 my-node-app