Beginner's Guide to Docker
Page 1: Introduction to Docker
What is Docker?
Docker is a platform that allows developers to build, package, and run applications in isolated environments
called containers. Containers make it easier to ensure that an application runs consistently across different
computing environments.
Why Use Docker?
• Portability: Applications can run anywhere without worrying about differences in environments.
• Efficiency: Containers are lightweight and start quickly compared to virtual machines.
• Scalability: Docker works seamlessly with cloud platforms, making it easier to scale applications.
• Consistency: Eliminates the "it works on my machine" problem.
Key Concepts
• Image: A template used to create containers. It contains everything needed to run the application.
• Container: A running instance of an image.
• Docker Hub: A repository where images can be stored and shared.
• Dockerfile: A script containing instructions to build an image.
Page 2: Setting Up Docker
Installing Docker
1. Go to the Docker website.
2. Download Docker Desktop for your operating system (Windows, macOS, or Linux).
3. Follow the installation instructions.
Verifying Installation
Open your terminal (or command prompt) and run:
docker --version
You should see the installed Docker version.
To check if Docker is running properly:
1
docker run hello-world
This will download and run a test container.
Docker Desktop
Docker Desktop provides an easy-to-use interface to manage containers, images, and settings.
Page 3: Working with Docker Containers
Pulling an Image
Download an image from Docker Hub:
docker pull ubuntu
This pulls the latest Ubuntu image.
Running a Container
docker run -it ubuntu bash
- -it : Interactive terminal. - ubuntu : The image to use. - bash : Command to run.
Listing Containers
docker ps # Shows running containers
docker ps -a # Shows all containers (running and stopped)
Stopping and Removing Containers
docker stop <container_id>
docker rm <container_id>
Example: Running Nginx
docker run -d -p 8080:80 nginx
2
- Runs Nginx in the background ( -d ). - Maps port 8080 on your computer to port 80 inside the container.
Page 4: Building Your Own Docker Image
Dockerfile Basics
A Dockerfile is a text file with instructions to build an image.
Example Dockerfile:
# Use official Python image
FROM python:3.9
# Set working directory
WORKDIR /app
# Copy files
COPY . .
# Install dependencies
RUN pip install -r [Link]
# Run the application
CMD ["python", "[Link]"]
Building an Image
docker build -t myapp .
- -t myapp : Tags the image with the name myapp . - . : Uses the current directory as the build context.
Running Your Image
docker run -p 5000:5000 myapp
Now you can access your app at [Link] .
3
Page 5: Docker Compose and Best Practices
What is Docker Compose?
Docker Compose allows you to define and run multi-container applications using a single YAML
configuration file.
Example [Link] :
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
Run with:
docker-compose up
Best Practices
• Keep images small by using lightweight base images (e.g., alpine ).
• Use .dockerignore to exclude unnecessary files.
• Tag images with meaningful versions.
• Regularly remove unused containers and images:
docker system prune
Next Steps
• Learn about Docker networking.
• Explore Docker volumes for data persistence.
• Try Kubernetes for container orchestration.
4
Conclusion
Docker is a powerful tool for developers and system administrators. By learning the basics of images,
containers, and Dockerfiles, you can package and run applications consistently across different
environments. With Docker Compose and best practices, you can scale up to managing more complex
applications.