📖 Scenario: You are setting up a simple web server using Nginx inside a Docker container. To make your web server accessible from your computer, you need to configure container networking properly.
🎯 Goal: Learn how to create a Docker container running Nginx and configure its networking so that you can access the web server from your host machine.
📋 What You'll Learn
Create a Docker container running Nginx
Map the container's port 80 to the host's port 8080
Verify that the Nginx welcome page is accessible via http://localhost:8080
💡 Why This Matters
🌍 Real World
Developers and system administrators often run web servers inside containers. Proper networking setup lets them access these servers from their computers or other services.
💼 Career
Understanding container networking is essential for deploying applications in Docker and Kubernetes environments, a key skill for DevOps roles.
Progress0 / 4 steps
1
Create a Docker container running Nginx
Write the Docker command to run an Nginx container named mynginx in detached mode using the official nginx image.
Nginx
Hint
Use docker run with --name to name the container and -d to run it in the background.
2
Map container port 80 to host port 8080
Modify the Docker command to map the container's port 80 to the host's port 8080 so you can access Nginx from your browser at http://localhost:8080.
Nginx
Hint
Use the -p option with hostPort:containerPort format.
3
Check running containers and their ports
Write the Docker command to list all running containers with their port mappings to verify that mynginx is running and port 8080 is mapped.
Nginx
Hint
Use docker ps to see running containers and their port mappings.
4
Access Nginx welcome page
Write a command to fetch the homepage from http://localhost:8080 using curl to confirm the Nginx server is accessible.
Nginx
Hint
Use curl http://localhost:8080 to get the Nginx welcome page content.
Practice
(1/5)
1. What is the main purpose of container networking in Docker?
easy
A. To allow containers to communicate with each other
B. To increase container storage space
C. To speed up container startup time
D. To change container CPU limits
Solution
Step 1: Understand container networking basics
Container networking connects containers so they can share data and services.
Step 2: Identify the purpose
The main goal is to enable communication between containers.
Final Answer:
To allow containers to communicate with each other -> Option A
Quick Check:
Container networking = communication [OK]
Hint: Networking means containers talk to each other [OK]
Common Mistakes:
Confusing networking with storage or CPU settings
Thinking networking speeds up container startup
Assuming networking changes resource limits
2. Which Docker command creates a new custom network for containers?
easy
A. docker create mynet network
B. docker network create mynet
C. docker network new mynet
D. docker create network mynet
Solution
Step 1: Recall Docker network creation syntax
The correct command is docker network create <name>.
Step 2: Match the options
Only docker network create mynet matches the correct syntax exactly.
Final Answer:
docker network create mynet -> Option B
Quick Check:
docker network create = create network [OK]
Hint: Remember: 'docker network create' makes networks [OK]
Common Mistakes:
Swapping order of 'create' and 'network'
Using 'new' instead of 'create'
Adding extra words after network name
3. What will be the result of running this command?
docker run --network mynet nginx
medium
A. The nginx container connects to the 'mynet' network
B. The nginx container creates a new network named 'mynet'
C. The command fails because '--network' is invalid
D. The container runs without any network connection
Solution
Step 1: Understand the '--network' flag
The flag connects the container to an existing network named 'mynet'.
Step 2: Analyze the command effect
The command runs nginx container attached to 'mynet' network.
Final Answer:
The nginx container connects to the 'mynet' network -> Option A
Quick Check:
--network attaches container to network [OK]
Hint: Use --network to join existing networks [OK]
Common Mistakes:
Thinking it creates a new network
Believing '--network' is invalid syntax
Assuming container runs without network
4. You ran docker run --network mynet nginx but got an error: "network mynet not found". What is the likely fix?
medium
A. Restart Docker service and try again
B. Remove the '--network' option from the command
C. Create the network first using docker network create mynet
D. Use docker network connect mynet nginx instead
Solution
Step 1: Understand the error message
The error means the network 'mynet' does not exist yet.
Step 2: Fix by creating the network
Use docker network create mynet to create it before running the container.
Final Answer:
Create the network first using docker network create mynet -> Option C
Quick Check:
Network must exist before use [OK]
Hint: Create network before attaching containers [OK]
Common Mistakes:
Skipping network creation
Trying to connect without network existing
Assuming restart fixes network errors
5. You want two nginx containers to communicate using a custom network. Which steps correctly set this up?
hard
A. Run containers normally, then use docker network connect mynet <container> without creating network
B. Run containers with --link mynet option to connect them
C. Create network with docker create network mynet, then run containers without network option
D. Create network with docker network create mynet, then run both containers with --network mynet
Solution
Step 1: Create a custom network
Use docker network create mynet to make a network for containers.
Step 2: Run containers attached to the network
Start both nginx containers with --network mynet to connect them.
Final Answer:
Create network with docker network create mynet, then run both containers with --network mynet -> Option D
Quick Check:
Create network + attach containers = communication [OK]
Hint: Create network first, then run containers on it [OK]
Common Mistakes:
Using wrong command to create network
Not attaching containers to the network
Using deprecated --link option
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.