Docker is a platform that allows you to develop, ship, and run applications in isolated environments called containers. Containers are lightweight and consistent across development and production environments, making them a powerful tool for software deployment.
Here's a step-by-step guide to help you get started:
1. Install Docker
Go to Docker's official website and download Docker Desktop for your operating system (Windows, macOS, or Linux).
Follow the installation instructions for your platform. Docker Desktop includes Docker Engine, Docker CLI, and Docker Compose.
2. Understand Docker Components
Docker Engine: The core of Docker that runs containers.
Docker Images: A blueprint or template that contains everything needed to run a container (code, libraries, configuration files, etc.).
Docker Containers: Instances of Docker images that run your applications in isolated environments.
Docker Hub: A cloud-based registry where you can find and share Docker images.
3. Basic Docker Commands
Open your terminal or command prompt and try these essential commands:
Verify Docker Installation:
docker --version
This checks if Docker is installed correctly.
Run Your First Container:
docker run hello-world
This pulls a "hello-world" image from Docker Hub and runs it in a container. It’s a quick test to confirm Docker is working.
List Running Containers:
docker ps
This shows all running containers.
List All Containers (including stopped):
docker ps -a
Download (pull) an image from Docker Hub:
docker pull nginx
This downloads the official NGINX web server image.
Run an image as a container:
docker run -d -p 8080:80 nginx
This runs the NGINX image in a container, mapping port 8080 on your machine to port 80 inside the container. You can access the web server by visiting
http://localhost:8080
in your browser.Stop a running container:
docker stop <container-id>
Remove a stopped container:
docker rm <container-id>
Remove an image:
docker rmi <image-id>
4. Building Your Own Docker Image
You can create a custom Docker image by writing a Dockerfile
. Here’s an example:
Create a
Dockerfile
in an empty directory:# Use an official Python runtime as a base image FROM python:3.9 # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 80 available to the world outside this container EXPOSE 80 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"]
Then build the image with:
docker build -t my-python-app .
Run the container:
docker run -p 4000:80 my-python-app
5. Using Docker Compose
Docker Compose allows you to manage multi-container Docker applications. Create a docker-compose.yml
file to define services, networks, and volumes.
Example docker-compose.yml
:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: postgres
environment:
POSTGRES_PASSWORD: example
Then run:
docker-compose up
This will start both the web and db services defined in the docker-compose.yml
.
6. Explore Docker Hub
Docker Hub has thousands of images for different software. To search for images, use:
docker search <keyword>
7. Next Steps
Learn more about networking in Docker.
Explore volumes for persistent data storage.
Experiment with advanced Docker commands and features, like setting up Docker Swarm or Kubernetes for orchestration.
Docker's official documentation and Docker Labs are great resources for further learning.