0% found this document useful (0 votes)
12 views5 pages

Docker Basics: Installation & Usage Guide

This document serves as an introductory guide to Docker, covering its installation, key concepts, and practical usage for containerizing applications. It includes sections on working with containers, building images, using Docker Compose, and provides practice exercises to reinforce learning. The material aims to establish a foundational understanding of Docker for further exploration of advanced topics.

Uploaded by

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

Docker Basics: Installation & Usage Guide

This document serves as an introductory guide to Docker, covering its installation, key concepts, and practical usage for containerizing applications. It includes sections on working with containers, building images, using Docker Compose, and provides practice exercises to reinforce learning. The material aims to establish a foundational understanding of Docker for further exploration of advanced topics.

Uploaded by

Joshua Huang
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Docker Learning Material

This document provides an introduction to Docker, a platform for containerizing


applications to ensure consistency across environments. It covers the basics of
Docker, including installation, key concepts, container management, building
images, and using Docker Compose, with practical examples.

Table of Contents
1. What is Docker?
2. Installing Docker
3. Key Docker Concepts
4. Working with Containers
5. Building Docker Images
6. Using Docker Compose
7. Common Docker Commands
8. Practice Exercises

What is Docker?
Docker is an open-source platform that uses containers to package applications
and their dependencies, ensuring they run consistently across different environ-
ments. Containers are lightweight, portable, and isolated, making Docker ideal
for development, testing, and production.

Installing Docker
To start using Docker:
1. Download Docker Desktop:
• Visit [Link] and download Docker Desktop for Windows or
macOS.
• For Linux, follow distribution-specific instructions (e.g., sudo apt
install [Link] for Ubuntu).
2. Install: Run the installer and follow the prompts. On Linux, ensure you
add your user to the docker group:
sudo usermod -aG docker $USER
1. Verify Installation: Open a terminal and run:
docker --version
docker run hello-world
The hello-world container confirms Docker is working.

1
Key Docker Concepts
• Container: A lightweight, isolated environment running an application
and its dependencies.
• Image: A read-only template used to create containers (e.g., nginx, ubuntu).
• Dockerfile: A script to define how an image is built.
• Registry: A storage for images (e.g., Docker Hub).
• Docker Compose: A tool for defining and running multi-container
applications.

Working with Containers


Containers are created from images and run applications.

Basic Commands
• docker pull <image>: Download an image from Docker Hub
(e.g., docker pull nginx).
• docker run <image>: Start a container from an image.
• docker ps: List running containers (add -a to see all containers).
• docker stop <container_id>: Stop a container.
• docker rm <container_id>: Remove a container.

Example: Run an Nginx Container


docker pull nginx
docker run -d -p 8080:80 nginx
• -d: Run in detached mode (background).
• -p 8080:80: Map port 8080 (host) to port 80 (container).
Access [Link] in a browser to see the Nginx welcome
page.

Building Docker Images


A Dockerfile defines the steps to create a custom image.

Example: Simple Python App


Create a file named [Link]:
from flask import Flask
app = Flask(__name__)
@[Link]('/')
def hello():
return "Hello, Docker!"
if __name__ == "__main__":
[Link](host="[Link]", port=5000)

2
Create a Dockerfile:
FROM python:3.9
WORKDIR /app
COPY [Link] .
RUN pip install flask
EXPOSE 5000
CMD ["python", "[Link]"]
Build and run the image:
docker build -t my-python-app .
docker run -d -p 5000:5000 my-python-app
Access [Link] to see "Hello, Docker!".

Using Docker Compose


Docker Compose simplifies multi-container applications using a YAML file.

Example: Web App with Database


Create [Link]:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
Run the application:
docker-compose up -d
• Access Nginx at [Link]
• Stop and remove: docker-compose down.

Common Docker Commands


• docker pull <image>: Download an image.
• docker run <image>: Run a container.

3
• docker build -t <name> .: Build an image from a Dockerfile.
• docker ps: List running containers.
• docker stop/rm <container_id>: Stop or remove a container.
• docker images: List images.
• docker rmi <image>: Remove an image.
• docker-compose up/down: Start/stop a multi-container app.

Practice Exercises
1. Pull and run a redis container, mapping port 6379.
2. Create a Dockerfile for a [Link] app that displays "Welcome to Node!" on
port 3000.
3. Write a Docker Compose file to run a web server (e.g., httpd) and a Redis
instance.
4. List all containers (running and stopped) and remove one.
5. Build and run a custom image for a simple static HTML page.

Example Solutions
Exercise 1: Run Redis docker pull redis
docker run -d -p 6379:6379 redis

Exercise 2: [Link] Dockerfile Create [Link]:


const http = require('http');
[Link]((req, res) => {
[Link](200, {'Content-Type': 'text/plain'});
[Link]('Welcome to Node!');
}).listen(3000);
Create Dockerfile:
FROM node:16
WORKDIR /app
COPY [Link] .
EXPOSE 3000
CMD ["node", "[Link]"]
Build and run:
docker build -t my-node-app .
docker run -d -p 3000:3000 my-node-app

Exercise 3: Docker Compose with httpd and Redis Cre-


ate [Link]:
version: '3'
services:
web:

4
image: httpd
ports:
- "8081:80"
cache:
image: redis
ports:
- "6379:6379"
Run:
docker-compose up -d

Exercise 4: List and Remove Containers docker ps -a # List all


containers
docker rm <container_id> # Replace with actual ID

Exercise 5: Static HTML Image Create [Link]:


<!DOCTYPE html>
<html>
<body>
<h1>Hello, Docker HTML!</h1>
</body>
</html>
Create Dockerfile:
FROM nginx
COPY [Link] /usr/share/nginx/html/
EXPOSE 80
Build and run:
docker build -t my-html-app .
docker run -d -p 8082:80 my-html-app

Conclusion
This material provides a foundation for learning Docker. Practice these ex-
amples on a system with Docker installed to build proficiency. For advanced
topics, explore Docker networking, volumes, Docker Swarm, or integration with
Kubernetes.

You might also like