Understand Docker Images and Containers
Before creating our own application, let's understand two important Docker concepts.
1. What is a Docker Image?
A Docker Image is a read-only template containing everything required to run an application.
For example:
-
Application code
-
Runtime (such as .NET)
-
Dependencies
-
Configuration
Think of an image as a blueprint.
2. What is a Docker Container?
A Docker Container is a running instance of a Docker Image.
Think of it this way:
-
Image = Blueprint
-
Container = Running application created from that blueprint.
3. Practical Exercise
Open Command Prompt and execute the following commands one by one.
Command 1: List Docker Images
docker images
This displays downloaded images.
Command 2: List Running Containers
docker ps
This displays currently running containers.
Command 3: List All Containers
docker ps -a
This displays running and stopped containers.
Command 4: Download an Nginx Image
docker pull nginx
Nginx is a web server. Docker downloads its image from Docker Hub.
Command 5: Verify the Image
docker images
You should now see nginx in the image list.
Expected learning outcome
You should now understand:
|
Command |
Purpose |
|---|---|
|
|
List images |
|
|
List running containers |
|
|
List all containers |
|
|
Download an image |
Create and Run Your First Docker Container
Objective: Run an Nginx web server inside a Docker container and access it through your Windows browser.
1. Create an Nginx Container
Open Command Prompt and execute:
docker run -d -p 8080:80 --name my-nginx nginx
Explanation:
|
Parameter |
Description |
|---|---|
|
|
Creates and starts a container |
|
|
Runs in background (detached mode) |
|
|
Maps Windows port 8080 to container port 80 |
|
|
Assigns a container name |
|
|
Docker image name |
2. Verify the Container
Execute:
docker ps
You should see something similar to:
CONTAINER ID IMAGE COMMAND STATUS
xxxxxxxx nginx ... Up ...
3. Access Nginx from Browser
Open Google Chrome or Edge.
Navigate to:
http://localhost:8080You should see:
Welcome to nginx!
Congratulations! You have successfully deployed a web server inside a Docker container.
4. Stop the Container
Execute:
docker stop my-nginx
Refresh your browser. The website should no longer be accessible.
5. Start the Container Again
docker start my-nginx
Refresh the browser. Nginx should work again.
Commands learned
|
Command |
Purpose |
|---|---|
|
|
Create and run container |
|
|
Check running containers |
|
|
Stop container |
|
|
Start stopped container |