-
Notifications
You must be signed in to change notification settings - Fork 0
Advanced Features
This section covers some of Docker’s advanced features that help you optimize your workflow, debug issues, and manage containers at a deeper level.
Use Case: Optimize Docker images by separating build and runtime environments.
# Stage 1: Build
FROM node:16 AS builder
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
RUN npm run build
# Stage 2: Runtime
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]- The
builderstage installs dependencies and builds the app. - The final stage (
nginx:alpine) only includes the built app, reducing image size.
Use Case: Securely manage sensitive data like API keys or passwords in containers.
- Create a secret:
echo "my-secret-password" | docker secret create db_password -
- Use the secret in a service:
docker service create --name my_service --secret db_password my_image
- Access the secret in your container at
/run/secrets/db_password.
Use Case: Manage multiple Docker environments (e.g., local, cloud).
- List all contexts:
docker context ls
- Switch to a different context:
docker context use <context-name>
- Create a new context:
docker context create <context-name>
Use Case: Troubleshoot issues with containers or images.
- Inspect a container:
docker inspect <container-id>
- View container logs:
docker logs <container-id>
- Check resource usage:
docker stats
- View image layer history:
docker history <image-id>
Use Case: Speed up builds with advanced caching and parallelization.
- Use the
DOCKER_BUILDKITenvironment variable:DOCKER_BUILDKIT=1 docker build -t my_image . - Or enable it globally in Docker’s configuration:
- Edit
~/.docker/config.jsonand add:{ "features": { "buildkit": true } }
- Edit
Use Case: Monitor the health of your containers.
Add a health check to your Dockerfile:
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 CMD curl -f http://localhost/health || exit 1- Docker uses the health check to determine if the container is healthy.
Use Case: Control CPU and memory usage for containers.
- Limit CPU:
docker run --cpus="2.0" my_image - Limit memory:
docker run --memory="512m" my_image
Use Case: Deploy and manage multi-container applications with clustering.
- Initialize a swarm:
docker swarm init
- Deploy a service:
docker service create --name my_service -p 80:80 my_image
- Scale the service:
docker service scale my_service=3
Use Case: Create advanced container-to-container communication setups.
- Create a custom network:
docker network create --driver bridge my_network
- Run containers in the network:
docker run --network my_network --name container1 my_image docker run --network my_network --name container2 my_image
- Use container names to communicate:
curl http://container2:port
| Command | Description |
|---|---|
docker secret create <name> <file> |
Create a Docker secret. |
docker inspect <container-id> |
Inspect container details. |
docker stats |
Monitor container resource usage. |
docker context use <context-name> |
Switch between Docker contexts. |
docker build --secret |
Build images with secrets for BuildKit. |
docker network create --driver <type> |
Create a custom network. |
-
Use Multi-Stage Builds:
Keep images lightweight by separating build and runtime layers. -
Secure Sensitive Data:
Always use Docker Secrets for sensitive information. -
Optimize Build Performance:
Enable BuildKit for faster and efficient builds. -
Monitor Containers:
Implement health checks and monitor resource usage regularly.
| Issue | Solution |
|---|---|
| Build takes too long | Use BuildKit and cache layers effectively. |
| Secret data is exposed | Ensure you’re using Docker Secrets, not ENV variables. |
| Containers cannot communicate | Verify network configuration and container names. |