DEV Community

janak0ff
janak0ff

Posted on

Day 43: Docker Ports Mapping

The Nautilus DevOps team is planning to host an application on a nginx-based container. There are number of tickets already been created for similar tasks. One of the tickets has been assigned to set up a nginx container on Application Server 3 in Stratos Datacenter. Please perform the task as per details mentioned below:

a. Pull nginx:alpine docker image on Application Server 3.

b. Create a container named cluster using the image you pulled.

c. Map host port 3001 to container port 80. Please keep the container in running state.


📋 Docker Ports Mapping

Challenge Requirements:

  1. On App Server 3
  2. Pull nginx:alpine image
  3. Create container named cluster
  4. Map host port 3001 to container port 80
  5. Keep container running

🔧 Step-by-Step Solution

Step 1: SSH to App Server 3

ssh banner@stapp03
# Password: BigGr33n
Enter fullscreen mode Exit fullscreen mode

Step 2: Switch to Root (if needed)

sudo -i
# Password: BigGr33n
Enter fullscreen mode Exit fullscreen mode

Step 3: Check Docker Status

# Check Docker version
docker --version

# Check Docker service status
sudo systemctl status docker
Enter fullscreen mode Exit fullscreen mode

Step 4: Pull the Nginx Alpine Image

# Pull the nginx:alpine image
docker pull nginx:alpine

# Verify the image is pulled
docker images | grep nginx
Enter fullscreen mode Exit fullscreen mode

Step 5: Create and Run the Container with Port Mapping

# Create and run container with port mapping
docker run -d --name cluster -p 3001:80 nginx:alpine

# The syntax is: docker run -d --name <container-name> -p <host-port>:<container-port> <image>
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify the Container

# Check container is running
docker ps

# Check specific container
docker ps | grep cluster

# Verify port mapping
docker port cluster

# Test the container
curl http://localhost:3001
Enter fullscreen mode Exit fullscreen mode

🔍 Additional Verification

# Check container details
docker inspect cluster

# Check container logs
docker logs cluster

# Check container IP
docker inspect cluster | grep IPAddress

# Check port mapping details
docker inspect cluster | grep -A 5 "PortBindings"

# Test from external (if accessible)
curl http://stapp03:3001
Enter fullscreen mode Exit fullscreen mode

🛠️ Troubleshooting

If Port 3001 is Already in Use:

# Check what's using port 3001
sudo ss -tulpn | grep 3001
sudo lsof -i :3001

# Stop the container using the port
docker stop <container-name>
docker rm <container-name>

# Or use a different port
docker run -d --name cluster -p 3002:80 nginx:alpine
Enter fullscreen mode Exit fullscreen mode

If Container Fails to Start:

# Check container logs
docker logs cluster

# Remove and recreate
docker rm -f cluster
docker run -d --name cluster -p 3001:80 nginx:alpine
Enter fullscreen mode Exit fullscreen mode

If Image Pull Fails:

# Check internet connectivity
ping -c 4 google.com

# Try pulling with different registry
docker pull docker.io/nginx:alpine
Enter fullscreen mode Exit fullscreen mode

🔑 Key Points

Parameter Value Description
Image nginx:alpine Lightweight nginx image
Container Name cluster Name of the container
Host Port 3001 Port on the host machine
Container Port 80 Port inside the container
Mapping Format -p 3001:80 host-port:container-port

✅ Task Summary

Requirement Status Command Used
SSH to App Server 3 ssh banner@stapp03
Switch to root sudo -i
Pull nginx:alpine image docker pull nginx:alpine
Create container named cluster docker run -d --name cluster
Map host port 3001 to container port 80 -p 3001:80
Container in running state docker ps shows Up status
Test successful curl http://localhost:3001

Top comments (0)