0% found this document useful (0 votes)
3 views12 pages

Docker Full Tutorial

This document is a comprehensive guide for beginners on using Docker, covering installation steps for Windows, macOS, and Linux, as well as key concepts like images, containers, and Dockerfiles. It includes instructions for creating and running Docker containers, using Docker Compose for multi-container applications, and best practices for efficient Docker usage. Additionally, it provides troubleshooting tips and resources for further learning.

Uploaded by

Imad Benzaid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Docker Full Tutorial

This document is a comprehensive guide for beginners on using Docker, covering installation steps for Windows, macOS, and Linux, as well as key concepts like images, containers, and Dockerfiles. It includes instructions for creating and running Docker containers, using Docker Compose for multi-container applications, and best practices for efficient Docker usage. Additionally, it provides troubleshooting tips and resources for further learning.

Uploaded by

Imad Benzaid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Complete Docker Guide for Beginners What is Docker? Docker is a platform that allows you to package applications and their dependencies into containers. These containers can run consistently across different environments, solving the "it works on my machine" problem. Think of containers as lightweight, portable boxes that contain everything your application ne Step 1: Installing Docker Windows 1. Download Docker Desktop from [Link] 2. Run the installer and follow the setup wizard Enable WSL 2 (Windows Subsystem for Linux) if prompted 4, Restart your computer after installation 5. Launch Docker Desktop from the Start menu macOS 1. Download Docker Desktop for Mac from [Link] 2. Open the downloaded .dmg file 3. Drag Docker to your Applications folder 4, Launch Docker from Applications 5. Grant necessary permissions when prompted Linux (Ubuntu/Debian) bash # Update package index sudo apt-get update 4 Install required packages sudo apt-get install ca-certificates curl gnupg Isb-release Add Docker's official GPG key sudo mkdir -p /ete/aptkeyrings curl -fsSL hitps://[Link]/linux/ubuntw/gpg | sudo # Set up the repository # Install Docker Engine sudo apt-get update Add your user to docker group (to run without sudo) sudo usermed -1G docker SUSER # Log out and back in for group changes to take effect mor -0 /ete/aptkeyrings/docker. echo "deb [arch=8 dpkg --print-architecture) signed-by+/eto/apt/keyrings/docker gpg] htps://[Link]/uby sudo apt-get install docker-ce docker-ce-cli [Link] dockercompose-plugin Verify Installation bash docker --version docker run hello-world If successful, you'll see a welcome message from Docker. Step 2: Understanding Key Concepts Images Images are read-only templates that contain the application code, runtime, libraries, and dependencies. They're like blueprints for containers, Containers Containers are running instances of images. They're isolated environments where your application executes, Dockerfile A text file containing instructions to build a Docker image. It defines what goes into your container. Docker Hub A cloud-based registry where you can find and share container images. Step 3: Your First Docker Container Running a Pre-built Container bash # Pull and run an nginx web server docker 8080: ## Explanation: #-d: Run in detached mode (background) #-p 8080:80: Map port 8080 on host to port 80 in container fname: Give the container a name # nginx: The image to use ‘Visit (litp:/Tocalhost-8080) in your browser to see nginx running. Basic Docker Commands bash # List running containers docker ps # List all containers (including stopped) docker ps -a # Stop a container docker stop my-nginx ¥ Start a stopped container docker start my-nginx 4# Remove a container docker rm my-nginx 1 List downloaded images docker images # Remove an image docker rmi nginx # View container logs docker logs my-nginx # Execute commands inside a running container docker exec -it my-nginx bash Step 4: Creating Your First Dockerfile Create a simple Node,js application: 1, Create project directory bash mkdir my-docker-app ‘ed my-docker-app 2. Create a simple app ([Link]) javascript require(hutp); const server = hitp,createServer( (req, res) => [Link](200, {"Content-Type’: text/plain’); [Link]('Hello from Docker!\n) serverlisten(3000, () => { [Link](’Server running on port 3000’); 3. Create package,json *my-docker-app", "version"; "1.0.00 "scripts": { "start"; "node [Link]" 4. Create Dockerfile dockerfile # Use official Node,js image as base FROM # Set working directory inside container WORKDIR Copy package files copy # Install dependencies RUN 4 Copy application code copy # Expose port 3000 EXPOSE # Command to run the application CMD ["npm’, "start" 5. Create dockerignore 6. Build and run your image bash # Build the image docker # Run the container docker 3000: 4H Test it curl Step 5: Docker Compose for Multi-Container Applications Docker Compose allows you to define and run multi-container applications. Create [Link] yam version: 3.8 services: webs build: ports: ="3000;3000" environment: + NODE_ENV=production depends_on; = database volumes: = Japp /app/node_modules database: image: postgres:15 environment « POSTGRES_USER=myuset - POSTGRES_PASSWORD=mypassword = POSTGRES_DB=mydb volumes: ostgresdata;/var/lib/postgresql/data ports: = "5432:5432" volumes: postgres-data: Docker Compose Commands bash # Start all services docker-compose up -d # Stop all services docker-compose down View logs docker-compose logs -f # Rebuild services docker-compose up # List running services docker-compose ps Step 6: Best Practices 1. Keep Images Small + Use alpine-based images when possible + Use multi-stage builds + Remove unnecessary files 2, Multi-stage Build Example dockerfile # Build stage FROM node:18-alpine AS builder WORKDIR /app COPY package*.json RUN npm ci --only=production if Production stage FROM node:18-alpine WORKDIR /app COPY ~from=builder /app/node_module odules copy EXPOSE 300( CMD ["node", "app js" 3. Security Practices * Don't run containers as root + Scan images for vulnerabilities + Use official images from trusted sources + Keep base images updated 4. Use Em ‘onment Variables dockerfile ENV NODE_ENV=production ENV PORT=3000 5. Health Checks dockerfile HEALTHCHECK ~interval=30s CMD node healthcheck,j || exit 1 timeout=3s \ Step 7: Common Workflows Development Workflow bash Build image docker build -t myapp:dev # Run with volume mounting for live reload docker run __# Download an image docker build t, —# Build from Dockerfile docker images # List images docker mi _# Remove image # Containers docker run _# Create and start container docker ps # List running. containers docker stop Stop container docker start # Start stopped container dockor rm — # Remove container docker exec -it bash #Access container shell W System docker system df # Show disk usage docker system prune ## Clean up unused resources docker stats # Show resource usage # Docker Compose dockercompose up # Start services dockercompose down # Stop services dockercompose logs # Hew logs dockercompose ps List services Happy containerizing!

You might also like