Introduction
Welcome, future Docker pro! 🐳 Have you ever wondered how developers share applications in lightweight, portable containers? Or how to deploy the same environment from your laptop to a server in seconds? The answer lies in Docker images.
In this guide, you’ll learn:
✔ How to build a Docker image from a Dockerfile
✔ Push it to DockerHub (shareable anywhere!)
✔ Modify and update your image
Key Concepts
1. What’s a Container?
A container is a lightweight, standalone package that includes:
- OS-level virtualization (only the essentials, not the whole OS)
- Isolated dependencies (no more "works on my machine" issues)
- Portability (runs identically on any system)
Common Use Cases
- Running microservices
- CI/CD pipelines
- Ensuring dev/production parity
- Building scalable architectures
2. What’s a Docker Image?
A read-only template to create containers. Includes:
- Your application code
- Libraries/dependencies
- Configuration files
Popular Base Images:
ubuntu # Minimal OS
nginx # Web server
python # Python apps
redis # Database
3. What’s DockerHub?
A cloud registry to store/share Docker images. Alternatives:
- Azure Container Registry
- Google Container Registry
- Amazon ECR
Prerequisites
- Docker installed (Install guide)
- DockerHub account (Sign up)
Step-by-Step Guide
Step 1: Update System Packages
sudo su
apt update && apt upgrade -y
Step 2: Create a Project Directory
mkdir mydockerproject
cd mydockerproject
Step 3: Create a Dockerfile
vi Dockerfile
Paste this (press i
to insert, ESC
+ :wq
to save):
FROM ubuntu:latest
RUN apt update && apt install -y nginx
CMD ["echo", "Hello from your first Docker container!"]
Step 4: Build the Image
docker build -t mydocker .
Verify:
docker image ls # Lists all images
docker run --rm mydocker # Test the container
Step 5: Create a DockerHub Repository
- Go to DockerHub.
- Click "Create Repository".
- Name it (e.g.,
mydocker-image
), set visibility to Public, and add a description.
Step 6: Push to DockerHub
6.1 Log in to DockerHub
docker login
6.2 Tag Your Image
docker tag mydocker yourusername/repositoryname:tag
6.3 Push the Image
docker push yourusername/repositoryname:tag
Verify: Refresh your DockerHub repository—your image should appear!
🛠️ Updating Your Image
1. Modify the Dockerfile
Example changes:
RUN apt update && apt install -y curl # Added curl
CMD ["echo", "Welcome to Docker!"] # Updated message
2. Rebuild with a New Tag
docker build -t yourusername/repositoryname:newtag .
3. Push the Updated Image
docker push yourusername/repositoryname:newtag
Next Steps
- Pull the image elsewhere:
docker pull yourusername/repositoryname:tag
- Explore multi-stage builds for smaller images.
- Learn Docker Compose for multi-container apps.
🎉 Conclusion
You’ve now mastered:
✅ Building Docker images
✅ Sharing them via DockerHub
✅ Updating images efficiently
Happy Dockering! 🐋✨