basics
Docker is a platform for developing, shipping, and running applications in containers. Containers allow you to package an application and its dependencies together in a standardized unit for easy deployment across different environments. Here are some basics to get you started with Docker:
1. Install Docker:
Install Docker on your machine by following the instructions on the official Docker website.
2. Docker Concepts:
Image: A lightweight, standalone, and executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, and system tools.
Container: An instance of a Docker image, running as a process on the host machine.
3. Docker Commands:
Pull an Image:
docker pull image_name:tag
Run a Container:
docker run image_name:tag
List Running Containers:
docker ps
List All Containers (including stopped ones):
docker ps -a
Stop a Running Container:
docker stop container_id
Remove a Container:
docker rm container_id
List Docker Images:
docker images
Remove an Image:
docker rmi image_id
4. Dockerfile:
Create a Dockerfile to define how your image should be built. Here's a basic example:
# Use an official base image FROM nginx:latest # Copy your application code COPY . /usr/share/nginx/html # Expose a port EXPOSE 80 # Define the command to run your application CMD ["nginx", "-g", "daemon off;"]
5. Build a Docker Image:
Navigate to the directory containing your Dockerfile and run:
docker build -t image_name:tag .
6. Run a Container from a Built Image:
After building your image, run a container:
docker run -p 8080:80 image_name:tag
This example maps port 8080 on your host machine to port 80 in the container.
7. Docker Volumes:
Use volumes to persist data outside the container:
docker run -v /path/on/host:/path/in/container image_name:tag
8. Docker Compose:
Docker Compose allows you to define and run multi-container Docker applications. Create a
docker-compose.yml
file to specify services, networks, and volumes.
9. Networking:
Docker containers can communicate with each other using container names or through defined networks.
10. Docker Hub:
Docker Hub is a registry service where you can find and share container images.
11. Docker Cheat Sheet:
Refer to the official Docker Cheat Sheet for quick reference.
Additional Resources:
Practice running containers, building images, and exploring more advanced features as you become more comfortable with Docker. It's a powerful tool for containerization and can significantly simplify application deployment and management.