Docker is a platform for developing, shipping, and running applications in containers. Containers allow you to package an application and its dependencies into a standardized unit for software development. Here’s a basic guide on how to use Docker:
Installation:
- Before you start, make sure Docker is installed on your machine. You can download and install Docker from the official website: Docker Desktop.
Verify Installation:
- Open a terminal or command prompt and run the following commands to verify that Docker is installed correctly:
docker --version docker run hello-world
Docker Images:
- Docker images are the building blocks of containers. Images are lightweight, standalone, and executable packages that include everything needed to run an application, including the code, runtime, libraries, and dependencies.
- Pull an existing image from Docker Hub:
docker pull image_name:tag
Docker Containers:
- Containers are instances of Docker images. You can run multiple containers from the same image.
- Run a container:
docker run image_name:tag
- Run a container in detached mode (in the background):
docker run -d image_name:tag
- List running containers:
docker ps
- List all containers (including stopped ones):
docker ps -a
Dockerfile:
- Dockerfile is a script that contains instructions to build a Docker image. Create a file named
Dockerfile
in your project directory. - Example
FROM ubuntu:latest
RUN apt-get update && apt-get install -y python3
CMD ["python3", "-c", "print('Hello, Docker!')"]
- Build an image from a Dockerfile:
docker build -t image_name:tag .
Volumes:
- Volumes allow you to persist data outside of the container. This is useful for storing database files, logs, or other data that should survive container restarts.
- Mount a volume when running a container:
docker run -v /host/path:/container/path image_name:tag
Networking:
- Docker containers can communicate with each other and the outside world through networking. You can expose ports and link containers.
- Expose a port when running a container:
docker run -p host_port:container_port image_name:tag
Compose:
- Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define services, networks, and volumes in a
docker-compose.yml
file. - Example docker-compose.yml file:
version: '3'
services:
web:
image: nginx:alpine
ports:
- "8080:80"
- Run the services defined in a docker-compose.yml file:
docker-compose up
This is a basic overview of Docker usage. Docker provides a powerful set of commands and features for container management, orchestration, and more. Refer to the official Docker documentation for more in-depth information: Docker Documentation.