Docker is a containerization platform that simplifies application development, testing, and deployment by packaging code and its dependencies into portable containers. This guide introduces Docker basics and demonstrates how to use it effectively for development workflows.


1. Installing Docker

a) Linux

Install Docker using your package manager:

bash
 
sudo apt-get update sudo apt-get install docker.io

b) MacOS and Windows

Download and install Docker Desktop from the official website.


2. Key Docker Concepts

  • Images: Preconfigured environments with application code and dependencies.
  • Containers: Running instances of images.
  • Dockerfile: A script that defines how to build a Docker image.
  • Volumes: Persistent storage for containers.

3. Basic Docker Commands

a) Pulling Images

Download official images from Docker Hub:

bash
 
docker pull [image_name]

Example:

bash
 
docker pull nginx

b) Running Containers

Start a container from an image:

bash
 
docker run -d -p [host_port]:[container_port] [image_name]

Example:

bash
 
docker run -d -p 8080:80 nginx

c) Viewing Running Containers

List all active containers:

bash
 
docker ps

d) Stopping a Container

Stop a running container:

bash
 
docker stop [container_id]

4. Building Custom Docker Images

Create a Dockerfile with instructions for building your image:

Dockerfile
 
# Use a base image FROM python:3.9 # Set working directory WORKDIR /app # Copy project files COPY . . # Install dependencies RUN pip install -r requirements.txt # Command to run the application CMD ["python", "app.py"]

Build the image:

bash
 
docker build -t my_app .

Run the container:

bash
 
docker run -d -p 5000:5000 my_app

5. Using Volumes for Persistent Data

Mount a volume to persist data between container restarts:

bash
 
docker run -d -v /path/on/host:/path/in/container [image_name]

6. Managing Multi-Container Applications with Docker Compose

Docker Compose allows you to manage multi-container applications using a single YAML file.

Example docker-compose.yml:

yaml
 
version: '3.9' services: web: image: nginx ports: - "8080:80" database: image: mysql environment: MYSQL_ROOT_PASSWORD: secret

Start the services:

bash
 
docker-compose up -d

7. Best Practices for Using Docker

  • Use lightweight base images like alpine to reduce image size.
  • Keep your Dockerfile simple and clean.
  • Regularly remove unused containers and images:
    bash
     
    docker system prune -f
  • Use .dockerignore to exclude unnecessary files during image builds.

Common Issues and Troubleshooting

  • Port Already in Use: Ensure no other process is using the specified host port.
  • Container Won’t Start: Check logs with:
    bash
     
    docker logs [container_id]
  • High Disk Usage: Clean up dangling images and stopped containers:
    bash
     
    docker image prune -a

Need Assistance?

If you encounter issues while working with Docker, our team at Cybrohosting can assist. Open a support ticket in your Client Area or email us at support@cybrohosting.com.

Esta resposta lhe foi útil? 0 Usuários acharam útil (0 Votos)