Docker Beginner's Guide
What is Docker?
Docker is a tool that allows you to create, deploy, and run applications in containers.
Containers are lightweight, portable, and easy to manage, which makes them ideal for software
development.
Think of a container like a small, self-contained package with everything your application needs to
run: the code,
libraries, and settings.
Why Use Docker?
- Consistency: Containers ensure that software runs the same way on different machines,
avoiding 'it works on my machine' problems.
- Isolation: Docker keeps your applications and services separate, so they don't interfere with each
other.
- Efficiency: Containers use fewer resources than traditional virtual machines, making them fast to
start and stop.
Getting Started with Docker
1. Install Docker
- Download Docker from Docker's official site and follow the instructions for your operating system.
2. Basic Docker Concepts
- Images: A Docker image is a template for creating containers. It includes your app code, runtime,
and system tools.
- Containers: A container is a running instance of a Docker image.
- Docker Hub: A repository where you can find thousands of images ready to use.
Basic Docker Commands
- Check Docker Version
docker --version
- Pull an Image from Docker Hub
docker pull ubuntu
- List Docker Images
docker images
- Run a Container
docker run -it ubuntu
- Stop a Container
docker stop <container_id>
- Remove a Container
docker rm <container_id>
Building Your Own Docker Image
1. Create a Dockerfile
Example:
# Use an existing image
FROM ubuntu
# Install a package
RUN apt-get update && apt-get install -y curl
# Add a custom message
CMD ["echo", "Hello from my first Docker container!"]
2. Build an Image
docker build -t my-first-image .
3. Run Your Image
docker run my-first-image
Key Docker Commands Summary
| Command | Description |
|---------------------------------------|---------------------------------------------|
| docker pull <image> | Download an image |
| docker run <image> | Start a new container from an image |
| docker ps | List all running containers |
| docker stop <container_id> | Stop a container |
| docker rm <container_id> | Remove a stopped container |
| docker build -t <name> . | Build an image from a Dockerfile |
| docker images | List all images on your system |