Welcome to the 👋
Whether you're a novice exploring the world of or a seasoned pro seeking a quick reference, you're in the right place. This comprehensive guide is designed to address both beginners and experienced users, offering a thorough exploration of essential Docker commands and concepts.
Explore creating and managing Docker images, understand , and master Dockerfile syntax, dive into complexities, and learn the latest features like with clear explanations, step-by-step guidance, and practical illustrative examples.
With all the necessary commands and concepts, this guide will help you get started with Docker real quickly. Go ahead and give it a read!
Happy Dockering!
docker build -t myapp .
docker image ls
docker pull nginx:latest
docker rmi myapp:latest
or
docker rm fd484f19954f
docker tag myapp:latest myapp:v1
docker push myapp:v1
docker image inspect myapp:v1
docker save -o myapp.tar myapp:v1
docker load -i myapp.tar
docker image prune
docker run myapp
docker run --name my_container myapp:v1
docker ps
docker ps -a
docker stop my_container
docker start my_container
docker run -it my_container
docker run -it my_container sh
docker rm my_container
docker rm -f my_container
docker inspect my_container
docker logs my_container
docker pause my_container
docker unpause my_container
docker volume create my_volume
docker volume ls
docker volume inspect my_volume
docker volume rm my_volume
docker run --name my_container -v my_volume:/app/data myapp:v1
docker cp data.txt my_container:/app/data
docker run --name my_container -p 8080:80 myapp
docker network ls
docker network inspect bridge
docker network create my_network
docker network connect my_network my_container
docker network disconnect my_network my_container
docker compose up
This command reads the file and starts the defined services in the background.
docker compose down
This command stops & removes the containers, networks, and volumes defined in the file.
docker compose build
This command builds or rebuilds the Docker images for the services defined in the file.
docker compose ps
This command lists the containers for the services defined in the file.
docker compose logs
This command shows the logs for all services defined in the file.
docker compose up -d --scale web=3
This command scales the specified service to the desired number of containers.
docker compose run web npm install
This command runs a one-time command in the specified service container.
docker volume ls
Docker Compose creates volumes for services. This command helps you see them.
docker volume pause service_name
This command pauses the specified service.
docker volume unpause service_name
This command unpauses the specified service.
docker compose ps service_name
This command provides detailed information about a specific service.
docker init
This command initializes Docker inside the application directory, setting up the necessary configuration files.
docker compose watch
It watches the build context for the service and rebuilds/refreshes containers when files are updated.
A is a script that contains instructions for building a . It defines the , sets up , installs software, and configures the container for a specific application or service.
Specifies the of the Docker Compose file format.
version: '3.8'
Defines the /containers that make up the application.
services:
web:
image: nginx:latest
Configures custom for the application.
networks:
my_network:
driver: bridge
Defines named that the services can use.
volumes:
my_volume:
Sets for a service.
environment:
- NODE_ENV=production
Maps host to container ports.
ports:
- "8080:80"
Specifies between services, ensuring one service starts before another.
depends_on:
- db
Configures the for a service.
build:
context: .
dockerfile: Dockerfile.dev
Mounts from another service or container.
volumes_from:
- service_name
Overrides the default specified in the Docker image.
command: ["npm", "start"]
Here's a simple Docker Compose file example for a web and database service:
version: '3.8'
// Define services for the MERN stack
services:
// MongoDB service
mongo:
image: mongo:latest
ports:
- "27017:27017"
volumes:
- mongo_data:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
// Node.js (Express) API service
api:
build:
// Specify the build context for the API service
context: ./api
// Specify the Dockerfile for building the API service
dockerfile: Dockerfile
ports:
- "5000:5000"
// Ensure the MongoDB service is running before starting the API
depends_on:
- mongo
environment:
MONGO_URI: mongodb://admin:admin@mongo:27017/mydatabase
networks:
- mern_network
// React client service
client:
build:
// Specify the build context for the client service
context: ./client
// Specify the Dockerfile for building the client service
dockerfile: Dockerfile
ports:
- "3000:3000"
// Ensure the API service is running before starting the client
depends_on:
- api
networks:
- mern_network
// Define named volumes for persistent data
volumes:
mongo_data:
// Define a custom network for communication between services
networks:
mern_network:
You’ve successfully completed the guide to essential Docker commands and techniques! 🚢 You now have a solid foundation to:
Remember, is a powerful tool that grows with you. The more you practice, the more proficient you’ll become. Dive into advanced features, experiment with real-world projects, and discover how Docker can transform your development process.
Explore Docker Compose, volumes, and networking to create even more efficient and scalable solutions.
Keep pushing boundaries, building amazing applications, and enjoying the journey!
Happy containerizing! 🚀
Congratulations for completing this resource! Mark it as completed to track your progress.