Master Docker fundamentals with our comprehensive tutorial. Learn containerization, image management, and orchestration through practical examples.
Docker is a platform that enables developers to package applications into containers—standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment.
docker --versionCheck your Docker installation version
docker run hello-worldRun your first Docker container to verify installation
Docker images are read-only templates used to create containers. Containers are running instances of Docker images.
docker pull nginx:latestPull the latest nginx image from Docker Hub
docker imagesList all Docker images on your system
docker run -d -p 8080:80 --name my-nginx nginxRun nginx container in detached mode -d: Detached mode -p: Port mapping (host:container) --name: Container name
docker psList all running containers
docker ps -aList all containers (including stopped ones)
A Dockerfile is a text document that contains all the commands to assemble a Docker image. It automates the image creation process.
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]A basic Dockerfile for a Node.js application FROM: Base image WORKDIR: Set working directory COPY: Copy files RUN: Execute commands EXPOSE: Document port CMD: Default command
docker build -t my-app:1.0 .Build a Docker image from Dockerfile -t: Tag the image .: Build context (current directory)
Learn essential commands to manage Docker containers throughout their lifecycle.
docker start my-nginxStart a stopped container
docker stop my-nginxStop a running container gracefully
docker restart my-nginxRestart a container
docker rm my-nginxRemove a stopped container
docker rm -f my-nginxForce remove a running container
docker logs my-nginxView container logs
docker exec -it my-nginx /bin/bashExecute interactive bash shell inside container -it: Interactive terminal
Docker volumes are the preferred mechanism for persisting data generated by and used by Docker containers.
docker volume create my-volumeCreate a named volume
docker run -d -v my-volume:/data --name my-container nginxMount a volume to a container -v: Volume mapping (volume:container-path)
docker volume lsList all volumes
docker volume inspect my-volumeView detailed information about a volume
Docker Compose is a tool for defining and running multi-container Docker applications using a YAML file.
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
db:
image: postgres:15
environment:
POSTGRES_PASSWORD: example
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:A docker-compose.yml file defining a web server and database services: Define containers ports: Port mappings volumes: Volume mounts environment: Environment variables
docker-compose up -dStart all services defined in docker-compose.yml in detached mode
docker-compose downStop and remove all containers, networks defined in compose file
docker-compose logs -fView logs from all services -f: Follow log output
Docker networking allows containers to communicate with each other and the outside world.
docker network create my-networkCreate a custom network
docker network lsList all networks
docker run -d --network my-network --name app1 nginxRun a container on a specific network
docker network inspect my-networkView detailed information about a network
Keep your Docker environment clean by removing unused containers, images, and volumes.
docker system pruneRemove all stopped containers, unused networks, dangling images
docker system prune -aRemove all unused images, not just dangling ones -a: All unused images
docker image pruneRemove unused images
docker volume pruneRemove unused volumes
docker container pruneRemove all stopped containers
docker ps - List running containersdocker images - List imagesdocker pull - Download imagedocker build - Build image