1. Installation
Begin by installing Docker on your machine. Visit the official Docker website and follow the instructions for your specific operating system.
2. Hello World Example:
Create a simple "Hello World" Docker container using the following steps:
Step 1: Create a Dockerfile
# Dockerfile
FROM alpine:latest
CMD echo "Hello, Docker World!"
This Dockerfile uses the Alpine Linux base image and specifies a command (`CMD`) to print the "Hello, Docker World!" message.
Step 2: Build the Docker Image
Open a terminal, navigate to the directory containing your Dockerfile, and run the following command:
docker build -t hello-docker
This command builds a Docker image named `hello-docker` using the instructions in the Dockerfile.
Step 3: Run the Docker Container
Once the image is built, run the following command to start a container based on that image:
docker run hello-docker
You should see the "Hello, Docker World!" message printed in the terminal.
3. Explore Docker Hub:
Visit Docker Hub to discover and use pre-built images for popular applications. Let's pull and run a simple Nginx web server as an example:
Step 1: Pull the Nginx Image
docker pull nginx:latest
This command downloads the latest Nginx image from Docker Hub.
Step 2: Run the Nginx Container
docker run -d -p 8080:80 --name my-nginx nginx:latest
This command runs an Nginx container in the background, mapping port 8080 on your host to port 80 in the container.
Now, if you open a web browser and navigate to `http://localhost:8080`, you should see the default Nginx welcome page.
4. Docker Compose Example:
Create a simple Docker Compose file (`docker-compose.yml`) to define a multi-container application:
version: '3'
services:
web:
image: nginx:alpine
ports:
- "8080:80"
app:
image: busybox
command: echo "Hello from the app container!"
Run the following command to start both containers defined in the Docker Compose file:
docker-compose up
This will start a Nginx container and a BusyBox container, each serving its purpose.
These examples provide a glimpse into the power and simplicity of Docker. As you explore further, you'll discover how Docker's containerization facilitates consistency, portability, and scalability in your development and deployment workflows.
Comments
Post a Comment