Docker CLI commands for containers, images, volumes, networks, and Docker Compose
Create and start a new container
docker run nginx # run in foreground
docker run -d nginx # detached (background)
docker run -d -p 8080:80 nginx # map port 8080 -> 80
docker run -it ubuntu bash # interactive shell
docker run --name myapp -d nginx # named containerList running containers
docker ps # running containers
docker ps -a # all containers (including stopped)Manage container lifecycle
docker start myapp # start stopped container
docker stop myapp # gracefully stop
docker kill myapp # force stop
docker rm myapp # remove stopped container
docker rm -f myapp # force remove running containerRun a command in a running container
docker exec -it myapp bash # interactive shell
docker exec myapp ls /app # run single command
docker exec -it myapp sh -c "env"View container output logs
docker logs myapp # show logs
docker logs -f myapp # follow live logs
docker logs --tail 50 myapp # last 50 linesBuild an image from a Dockerfile
docker build . # build from current directory
docker build -t myapp:1.0 . # with tag
docker build -f Dockerfile.prod . # specify DockerfileDownload or upload images from a registry
docker pull node:18-alpine # pull from Docker Hub
docker push myorg/myapp:1.0 # push to registryList local images
docker images # list all images
docker images -a # include intermediate layers
docker rmi nginx # remove image
docker image prune # remove unused imagesCreate an alias for an image
docker tag myapp:latest myorg/myapp:1.0
docker tag abc123def myapp:backupPersist data outside container lifecycle
docker volume create mydata
docker run -v mydata:/app/data myapp # named volume
docker run -v $(pwd):/app myapp # bind mount
docker volume ls
docker volume rm mydataManage container networking
docker network create mynet
docker run --network mynet myapp
docker network ls
docker network inspect mynetStart and stop multi-container applications
docker compose up # start all services
docker compose up -d # detached mode
docker compose up --build # rebuild images
docker compose down # stop and remove containers
docker compose down -v # also remove volumesUseful compose commands for development
docker compose ps # list services
docker compose logs -f # follow all logs
docker compose exec web bash # shell into service
docker compose restart web # restart a serviceKey Dockerfile directives and their usage
FROM node:18-alpine # base image
WORKDIR /app # set working directory
COPY package*.json ./ # copy files
RUN npm ci --production # run command during build
ENV NODE_ENV=production # environment variable
EXPOSE 3000 # document port
CMD ["node", "server.js"] # default run command
ENTRYPOINT ["npm", "start"] # fixed entrypointProduction-ready Node.js Dockerfile with non-root user
FROM node:18-alpine AS base
WORKDIR /app
# Install dependencies
COPY package*.json ./
RUN npm ci --only=production
# Copy source
COPY . .
# Create non-root user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
USER appuser
EXPOSE 3000
CMD ["node", "server.js"]Multi-service compose file with database and web app
version: "3.9"
services:
web:
build: .
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgres://user:pass@db:5432/mydb
depends_on:
- db
volumes:
- .:/app
- /app/node_modules
db:
image: postgres:15-alpine
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydb
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:Smaller final image by separating build and runtime stages
# Build stage
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Runtime stage — only copy built artifacts
FROM node:18-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./
RUN npm ci --only=production
EXPOSE 3000
CMD ["npm", "start"]Always use a .dockerignore file to exclude node_modules, .git, and secrets from build context
Use multi-stage builds to keep final images small — copy only the built artifacts
Tag images with specific versions, not just latest, for reproducible deployments
Run containers as a non-root user in production for better security
Use docker system prune regularly to reclaim disk space from unused images and containers