Essential Docker Commands
Docker CLIEssential Docker commands
# Build an image from Dockerfile in current directory
docker build -t myapp:v1 .
# List local images
docker images
# Run a container (detached, port mapping)
docker run -d -p 8080:3000 --name mycontainer myapp:v1
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# View container logs
docker logs mycontainer
# Execute command inside running container
docker exec -it mycontainer /bin/sh
# Stop a container
docker stop mycontainer
# Remove a container
docker rm mycontainer
# Remove an image
docker rmi myapp:v1
# Pull image from registry
docker pull nginx:latest
Building and Tagging Images
Image tags follow the format: registry/repository:tag
Docker CLIBuild and tag for Azure Container Registry
# Build with a tag
docker build -t myapp:v1.0.0 .
# Tag for Azure Container Registry
docker tag myapp:v1.0.0 myregistry.azurecr.io/myapp:v1.0.0
# Also tag as latest
docker tag myapp:v1.0.0 myregistry.azurecr.io/myapp:latest
Pushing to Azure Container Registry
Azure CLI + DockerLogin to ACR and push image
# Login to Azure Container Registry
az acr login --name myregistry
# Push image to ACR
docker push myregistry.azurecr.io/myapp:v1.0.0
docker push myregistry.azurecr.io/myapp:latest
# Or use ACR build (builds in Azure — no local Docker needed)
az acr build \
--registry myregistry \
--image myapp:v1.0.0 \
--file Dockerfile .
ACR Build vs Local Build
az acr build sends your source code to Azure and builds the image there — no Docker installed locally required. Perfect for CI/CD pipelines and cloud shell workflows.Running Containers
Docker CLIRunning containers with options
# Run with port mapping (host:container)
docker run -p 8080:3000 myapp:v1
# Run detached (background)
docker run -d -p 8080:3000 myapp:v1
# Run with environment variables
docker run -d \
-p 8080:3000 \
-e NODE_ENV=production \
-e DATABASE_URL=mongodb://localhost/mydb \
myapp:v1
# Run with volume mount (persist data)
docker run -d \
-p 8080:3000 \
-v /host/data:/app/data \
myapp:v1
# Run and remove when stopped
docker run --rm -p 8080:3000 myapp:v1
Docker Compose
Docker Compose defines and runs multi-container applications. A single docker-compose.yml file describes all services, networks, and volumes:
YAMLdocker-compose.yml — web app + database
version: '3.8'
services:
web:
build: .
ports:
- "8080:3000"
environment:
- DATABASE_URL=mongodb://db:27017/myapp
depends_on:
- db
db:
image: mongo:6
volumes:
- mongo-data:/data/db
ports:
- "27017:27017"
volumes:
mongo-data:
Docker CLIDocker Compose commands
# Start all services
docker compose up -d
# View logs for all services
docker compose logs -f
# Stop all services
docker compose down
# Stop and remove volumes
docker compose down -v
Multi-Stage Builds
Multi-stage builds produce smaller production images by separating the build environment from the runtime environment:
DockerfileMulti-stage build — build then copy
# Stage 1: Build
FROM node:20 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# Stage 2: Production — only copy built output
FROM node:20-alpine AS production
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/server.js"]
AZ-204 Exam Tip Know that
docker build creates an image, docker run starts a container, docker push uploads to a registry. Know that -p host:container maps ports. Know that az acr build builds directly in Azure. Know that multi-stage builds reduce final image size.