Docker Tutorial for Beginners
Docker is an open-source platform that automates the deployment, scaling, and management of applications within lightweight containers. These containers provide a consistent environment for software development, allowing developers to build, ship, and run applications seamlessly across different computing environments. If you’re new to Docker and looking to get started, this tutorial will guide you through the basics.
What is Docker?
At its core, Docker is designed to simplify the process of packaging applications and their dependencies into a single unit, called a container. Containers are lightweight and can run on any machine that has the Docker engine installed, regardless of the underlying infrastructure. This eliminates the "it works on my machine" problem that developers often face.
Installing Docker
Before diving into the practical aspects of Docker, you need to install it on your machine. Here’s how to get started:
For Windows:
- Download Docker Desktop: Go to the Docker Hub website and download Docker Desktop.
- Install: Run the installer and follow the prompts. Make sure to enable the WSL 2 feature if you’re on Windows 10 or later.
- Start Docker: After installation, launch Docker Desktop. It may take a few moments to initialize.
For macOS:
- Download Docker Desktop: Similar to Windows, download Docker Desktop from the Docker Hub.
- Install: Open the downloaded file and drag Docker to your Applications folder.
- Start Docker: Open Docker from your Applications folder. It may take some time to start up for the first time.
For Linux:
- Update the package index:
sudo apt-get update
- Install dependencies:
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
- Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Set up the stable repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- Install Docker Engine:
sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io
Once installed, you can check if Docker is running correctly by executing:
docker --version
Your First Docker Container
Now that you have Docker installed, let’s run your first container. Docker allows you to run images, which are the blueprints for containers. For this tutorial, we will use the popular hello-world
image.
Run the following command in your terminal:
docker run hello-world
What happens here is Docker checks to see if you already have the hello-world
image on your machine. If not, it pulls it from Docker Hub. Once it’s downloaded, Docker runs the container and you should see a message confirming that your installation appears to be working correctly!
Basic Docker Commands
Now that you have a grasp of running your first container, let’s look at a few basic Docker commands:
Listing Docker Images
To see what images you have downloaded, run:
docker images
This will display a list of all images stored locally.
Running a Container in Interactive Mode
You can start a container in interactive mode, which allows you to execute commands inside the container. For example, let’s run an Ubuntu container:
docker run -it ubuntu
Once inside, you can run Linux commands. To exit the container, type exit
.
Stopping a Running Container
To stop a running container, you first need to find out its container ID or name. Use:
docker ps
This command shows all currently running containers. To stop a specific container, use:
docker stop <container_id>
Removing Containers and Images
You can remove containers using:
docker rm <container_id>
To remove an image:
docker rmi <image_name>
Building Your Own Docker Image
Creating your own Docker images is straightforward. You need a Dockerfile
— a text file that contains instructions on how to build your image.
Here’s a simple example. Create a file named Dockerfile
:
# Start with a base image
FROM ubuntu:latest
# Install dependencies
RUN apt-get update && apt-get install -y python3
# Specify the command to run when the container starts
CMD ["python3", "--version"]
To build your image, run:
docker build -t my-python-image .
Replace my-python-image
with a name you prefer for your image. The .
refers to the current directory where your Dockerfile
is located.
Running Your Custom Image
After building your custom image, you can run it using:
docker run my-python-image
You should see the Python version printed in the terminal.
Docker Compose
For managing multi-container applications, Docker provides a tool called Docker Compose. It allows you to define services, networks, and volumes in a YAML file, making it easier to manage complex applications. Here’s a simple example of a docker-compose.yml
file:
version: '3'
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
To start this application, run:
docker-compose up
This command initializes both the web server and the database defined in your YAML configuration.
Exploring Docker Hub
Docker Hub is a public repository allowing users to share container images. You can search for images using the Docker CLI or through the Docker Hub website. To pull an image like nginx
, you would run:
docker pull nginx
Once pulled, you can start up an Nginx server with a simple command:
docker run -d -p 8080:80 nginx
This starts the Nginx server in detached mode and maps port 80 in the container to port 8080 on your host machine.
This tutorial has covered the basics of Docker, from installation to running containers, creating images, and using Docker Compose. As you continue to explore Docker, you’ll find it to be an invaluable tool in streamlining development and deployment processes.