Docker Tutorial
Overview
Docker is an open-source platform that automates the deployment, scaling, and management of applications using containerization. Containers are lightweight, portable, and ensure consistency across different environments.
Installing Docker
On Linux
To install Docker on a Linux system, follow these steps:
On macOS
- Download Docker Desktop for Mac from the Docker website.
- Open the downloaded
.dmg
file and drag Docker to your Applications folder. - Open Docker from your Applications folder.
On Windows
- Download Docker Desktop for Windows from the Docker website.
- Run the installer and follow the installation instructions.
- Start Docker Desktop from your Start menu.
Basic Docker Commands
1. docker --version
Check the installed Docker version.
Example
2. docker pull
Download an image from Docker Hub.
Example
3. docker images
List all available Docker images on your system.
Example
4. docker rmi
Remove a Docker image.
Example
5. docker run
Run a Docker container from an image.
Example
6. docker ps
List running Docker containers.
Example
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abcd1234efgh nginx "nginx -g 'daemon of…" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp quizzical_hermann
7. docker stop
Stop a running Docker container.
Example
8. docker rm
Remove a stopped Docker container.
Example
9. docker exec
Execute a command inside a running container.
Example
10. docker-compose
docker-compose
is a tool for defining and running multi-container Docker applications.
Example docker-compose.yml
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: postgres
environment:
POSTGRES_DB: exampledb
POSTGRES_USER: user
POSTGRES_PASSWORD: password
Commands
- Start Services:
docker-compose up
- Stop Services:
docker-compose down
- View Logs:
docker-compose logs
Dockerfile
A Dockerfile is a script that contains a series of instructions to create a Docker image.
Example Dockerfile
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the current directory contents into the container at /usr/src/app
COPY . .
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Build Image
Run Container
Summary
Docker simplifies application deployment by using containers that package code and dependencies together. Understanding these basic commands will help you get started with Docker and manage your containerized applications effectively. For more detailed information, refer to the official Docker documentation.