Introduction to Docker
Table of Contents
Introduction
Docker has revolutionized the way we develop, deploy, and manage applications. Its containerization technology provides a lightweight, portable, and efficient solution for packaging software. In this hands-on guide, we’ll explore the fundamentals of Docker containers and walk through practical examples to get you started.
Prerequisites:
Before diving into Docker, ensure you have the following installed on your system:
- Docker Desktop or Docker Engine
- A basic understanding of the command line
Getting Started:
1. Install Docker:
If you haven’t installed Docker yet, head to the official Docker website to download and install Docker Desktop or Docker Engine based on your operating system.
2. Verify Installation:
Open a terminal or command prompt and run the following commands to verify that Docker is installed correctly:
docker --version
docker run hello-world
If everything is set up correctly, you should see a welcome message confirming your installation.
Building Your First Container:
3. Create a Dockerfile:
A Dockerfile is a script that contains instructions for building a Docker image. Create a file named Dockerfile
in an empty directory with the following content:
# Use an official base image
FROM alpine:latest
# Set the working directory
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Define environment variable
ENV NAME World
# Run an application when the container starts
CMD ["sh", "-c", "echo Hello $NAME"]
4. Build the Docker Image:
In the same directory as your Dockerfile, run the following command to build your Docker image:
docker build -t my-first-container .
This command uses the -t
flag to tag your image with the name “my-first-container.”
5. Run the Container:
Now that you have built your Docker image, run a container using the following command:
docker run my-first-container
You should see the output: Hello World
.
Exploring Container Basics:
6. List and Manage Containers:
To see a list of running containers, use the following command:
docker ps
To stop a running container, use:
docker stop <container_id>
7. Remove Containers and Images:
To remove a stopped container, use:
docker rm <container_id>
To remove an image, use:
docker rmi <image_id>
Networking in Docker:
8. Networking Basics:
Docker provides a range of networking options. By default, containers can communicate with each other. Run two instances of your container:
docker run -d --name container1 my-first-container
docker run -d --name container2 my-first-container
Find the IP addresses of your containers:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container1
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container2
Conclusion:
Congratulations! You’ve taken your first steps into the world of Docker containers. This hands-on guide covered the basics of creating Docker images, running containers, and managing them. As you continue your Docker journey, explore additional features such as volumes, Docker Compose, and orchestration tools like Kubernetes for more advanced container management.
Remember, practice makes perfect, so feel free to experiment and build on what you’ve learned here. Happy containerizing!