Docker Tutorial for Beginners: Getting Started
Docker has become an essential tool for developers and system administrators, simplifying the process of deploying applications in a consistent environment. If you’re new to Docker and looking to get started, this tutorial will walk you through the basics of what Docker is, how to install it, and some fundamental commands to get you up and running.
What is Docker?
Docker is an open-source platform that allows developers to automate the deployment of applications inside lightweight, portable containers. These containers package up an application and all of its dependencies, ensuring that it runs the same way on any system—regardless of the underlying infrastructure. This makes it easier to develop, ship, and run applications in different environments.
Installing Docker
Step 1: Check System Requirements
Before downloading Docker, ensure your system meets the requirements. Docker is compatible with:
- Windows 10 or later (Pro or Enterprise version)
- macOS (latest versions)
- Any Linux distribution, including Ubuntu, CentOS, and Debian
Step 2: Download Docker
-
Windows: Go to the Docker Hub and download Docker Desktop for Windows. Run the installer and follow the prompts. After installation, you’ll need to enable WSL 2 (Windows Subsystem for Linux).
-
macOS: Download Docker Desktop for Mac from the Docker Hub and run the installer.
-
Linux: The installation will vary by distribution. For Ubuntu, you can use the following commands:
sudo apt update sudo apt install apt-transport-https ca-certificates curl software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" sudo apt update sudo apt install docker-ce
Step 3: Start Docker
Once installed, launch Docker Desktop. On Linux, you might need to start the Docker service manually:
sudo systemctl start docker
You can then verify that Docker is running by executing:
docker --version
This command should return the version of Docker installed on your machine.
Running Your First Docker Container
Now that Docker is installed, let’s run your first container. Docker Hub is a public registry that houses a vast selection of pre-built containers. We can use it to pull and run a simple “Hello World” application.
docker run hello-world
If everything is set up correctly, this command will download the hello-world
image (if it’s not already on your system) and run it. You should see a message indicating that Docker is working correctly.
Understanding Docker Images and Containers
Images
An image in Docker is a read-only template used to create containers. It contains the application code, libraries, and dependencies necessary for your application to run. You can think of it as a snapshot of the application at a particular moment.
Containers
A container, on the other hand, is a running instance of an image. Containers can be started, stopped, deleted, and moved, but they are ephemeral by nature. When you stop a container, it ceases to exist unless you explicitly create a new one from its image.
Managing Docker Containers
Here are some essential commands to help you manage your Docker containers.
Listing Containers
To list all running containers, you can use:
docker ps
To list all containers (including stopped ones), use:
docker ps -a
Stopping and Removing Containers
If you want to stop a running container:
docker stop <container_id>
Replace <container_id>
with the ID or name of the container you want to stop. You can find this ID by using the docker ps
command.
To remove a stopped container:
docker rm <container_id>
Running Containers in the Background
To run a container in detached mode (in the background), use the -d
flag:
docker run -d <image>
This way, the container runs in the background, allowing you to use the terminal for other commands.
Building Your Own Docker Image
With Docker, you can also create your own images using a Dockerfile
. A Dockerfile
is a text document that contains all the commands to assemble an image.
Here’s a simple example of a Dockerfile
:
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the current directory contents into the container
COPY . .
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Run app.py when the container launches
CMD ["python", "./app.py"]
Building the Image
To build an image from your Dockerfile, navigate to the directory containing the Dockerfile and run:
docker build -t my-python-app .
This command builds the image and tags it as my-python-app
.
Running the Custom Image
Now that you have created your own image, you can run it just like any other Docker image:
docker run my-python-app
You can also map ports and volumes to customize container behavior, which we’ll explore in further tutorials.
Summary of Useful Docker Commands
Here’s a quick reference of commands you may find useful as you start working with Docker:
docker --version
– Check the installed Docker versiondocker run <image>
– Run a Docker containerdocker ps
– List running containersdocker ps -a
– List all containers (running and stopped)docker stop <container_id>
– Stop a running containerdocker rm <container_id>
– Remove a stopped containerdocker build -t <name> .
– Build an image from a Dockerfile
By now, you should have a good understanding of the basics of Docker, from installation to running your first container and creating custom images. As you continue to explore Docker, you’ll discover its powerful capabilities that facilitate modern application development and deployment.