In our first post, we explored why Docker matters. Now, let's get our hands dirty and learn how to use it. By the end of this post, you'll have Docker installed and be running your very own web server in a container!
1. Setting Up Docker Desktop:
The easiest way to start is by installing Docker Desktop. This package includes the Docker Engine, Docker CLI (Command Line Interface), Docker Compose, and a user-friendly GUI for Windows, macOS, and Linux.
Installation: Go to the official Docker website and download Docker Desktop for your operating system.
Installation Note: Follow the default installation steps. You will likely need to restart your computer once the installation is complete.
Verification: Once installed, open your terminal (Command Prompt, PowerShell, or Bash) and run the following command to ensure Docker is running correctly:
docker --version
You should see an output indicating the version of the Docker client you have installed.
2. The Core Concept: Image vs. Container
Before running anything, let's briefly review the two most important terms:
Docker Image: This is the static blueprint. It's a read-only template that contains the application code, dependencies, libraries, and configuration needed for your application.
Docker Container: This is the running instance of an image. When you run an image, you create a containerāa lightweight, isolated environment that is executable.
3. Your First Command: Pulling an Image
When you tell Docker to run a container, it first needs the blueprint (the image). It looks for the image locally. If it doesn't find it, it automatically pulls it from a Container Registry, with the default being Docker Hub.
Let's pull the official Nginx web server image:
docker pull nginx:latest
- docker pull: The command to download an image.
- nginx: The name of the repository (the image).
- :latest: The tag, which specifies the version. latest is the default if no tag is specified.
You will see output showing Docker downloading the image in layers.
4. Running Your First Container (The Web Server)
Now for the magic! We will launch an instance of that Nginx image, which will run as an isolated web server.
docker run -d -p 8080:80 --name my-nginx-server nginx
Let's break down this powerful command:
| Flag | Description | Value |
|---|---|---|
| docker run | This is the main command to create a new container and run a command in it. | |
| -d | Stands for "detached" mode. This runs the container in the background, freeing up your terminal. | |
| -p | Stands for "publish" or "port mapping." This links a port on your local machine (the host) to a port inside the container. | 8080:80 means: Host Port 8080 $\rightarrow$ Container Port 80 (Nginx's default). |
| --name | Assigns a human-readable name to your container. | my-nginx-server |
| nginx | The image you want to run (Docker assumes :latest if no tag is specified). |
Verification:
Open your web browser.
Navigate to http://localhost:8080.
You should see the "Welcome to nginx!" default page. Congratulations! You are running a full web server inside a lightweight, isolated container.
5. Managing Your Running Containers
Since your container is running in the background, you need commands to interact with it.
A. Checking Container Status
Use docker ps to see a list of all currently running containers:
docker ps
You will see details like the Container ID, the image used, the ports mapped, and the status.
B. Viewing Container Logs
If you want to see what the container is printing (like server access logs or errors), use docker logs:
docker logs my-nginx-server
C. Stopping and Removing the Container
A container takes up resources while running, so let's shut it down.
Stop the Container: This sends a signal to the container to shut down gracefully.
docker stop my-nginx-server
Remove the Container: A stopped container is still on your system. To completely delete the instance, use docker rm.
docker rm my-nginx-server
Note: You must stop a container before you can remove it.
6. Cleaning Up the Image
If you no longer need the Nginx image itself, you can remove it from your local system cache:
docker rmi nginx
rmi stands for "remove image."
A word of caution: You cannot remove an image if any containers (even stopped ones) are still referencing it.
Whatās Next?
Youāve successfully installed Docker and navigated the basic commands to run and manage your first container. In the next post, weāll dive deeper into Docker Images and Layers to understand why these containers are so fast and efficient, which is crucial knowledge before we start building our own images!
Top comments (0)