Docker Syntax
Dockerfile – Build Blueprint
A Dockerfile contains step-by-step instructions to create a Docker image.
Basic Syntax:
# Base image
FROM node:18
# Set working directory
WORKDIR /app
# Copy files
COPY package*.json ./
RUN npm install
COPY . .
# Expose port and run app
EXPOSE 3000
CMD ["node", "index.js"]
Common Keywords:
FROM
– base image (e.g., ubuntu, node, python).COPY
– copies files from host to container.RUN
– executes a command during build (e.g., install dependencies).CMD
– defines the default command to run when container starts.WORKDIR
– sets the working directory inside the container.EXPOSE
– tells Docker what port the container listens to.
docker-compose.yml – Multi-Container Config
docker-compose.yml
is used to define and run multi-container applications.
Basic Syntax:
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- .:/app
depends_on:
- db
db:
image: postgres:15
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydb
Common Parts:
version
: Compose file format version.services
: Defines the containers (e.g., web, db).build
: Builds from a Dockerfile.image
: Uses an image from Docker Hub.ports
: Maps container to host ports.volumes
: Mounts host paths or named volumes.environment
: Set environment variables.depends_on
: Set container startup order.
Last updated