Getting Started: Your First Containerš
Source: Dev.to
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
Open your terminal (Command Prompt, PowerShell, or Bash) and run:
docker --version
You should see output indicating the version of the Docker client you have installed.
The Core Concept: Image vs. Container
| Term | Description |
|---|---|
| Docker Image | A static, readāonly blueprint that contains the application code, dependencies, libraries, and configuration needed for your app. |
| Docker Container | A running instance of an image ā a lightweight, isolated environment that is executable. |
Your First Command: Pulling an Image
Docker needs the image before it can run a container. If the image isnāt available locally, Docker automatically pulls it from a container registry (default: Docker Hub).
Pull the official Nginx web server image:
docker pull nginx:latest
docker pullā command to download an image.nginxā repository name (the image).:latestā tag specifying the version (default if omitted).
You will see output showing Docker downloading the image in layers.
Running Your First Container (The Web Server)
Launch an instance of the Nginx image as an isolated web server:
docker run -d -p 8080:80 --name my-nginx-server nginx
Command breakdown
| Flag | Description |
|---|---|
docker run | Creates a new container and runs a command in it. |
-d | Detached mode ā runs the container in the background. |
-p 8080:80 | Publishes port 80 inside the container to port 8080 on the host. |
--name my-nginx-server | Assigns a humanāreadable name to the container. |
nginx | Image to run (Docker assumes :latest if no tag is specified). |
Verification
Open your web browser and navigate to http://localhost:8080. You should see the āWelcome to nginx!ā default page.
Managing Your Running Containers
Checking container status
docker ps
Shows a list of currently running containers, including Container ID, image, ports, and status.
Viewing container logs
docker logs my-nginx-server
Displays the containerās stdout/stderr output (e.g., access logs, errors).
Stopping and removing the container
docker stop my-nginx-server # Gracefully stops the container
docker rm my-nginx-server # Deletes the stopped container
Note: A container must be stopped before it can be removed.
Cleaning Up the Image
If you no longer need the Nginx image:
docker rmi nginx
rmi stands for āremove image.ā
You cannot remove an image while any containers (even stopped ones) still reference it.
Whatās Next?
Youāve successfully installed Docker and learned 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 containers are so fast and efficientāessential knowledge before we start building our own images!