🎯 Objective
Create and test an Nginx container on App Server 3 in Stratos DC using Docker volume mapping.
Requirements:
Final mapping:
Host Container
/opt/data ───────────► /tmp
│ │
└── sample.txt └── sample.txt🧠 Concept
A Docker volume/bind mount allows a directory from the Docker host to be mounted inside a container.
Without a mount:
Docker Host Container
/opt/data /tmp
│ │
└── sample.txt └── Independent storageWith the required mapping:
Docker Host Container
/opt/data ───────────────► /tmp
│ │
└── sample.txt ────────► sample.txtChanges made inside the mounted directory are reflected on the host because both paths refer to the same mounted storage.
🔍 Step 1: Check Docker
Check the running containers:
docker psYou can also check whether the Nginx image already exists:
docker image ls📥 Step 2: Pull the Nginx Image
Pull the latest Nginx image:
docker pull nginx:latestYou can also use:
docker pull nginxWhen no tag is specified, Docker uses latest by default.
Verify:
docker image ls nginxYou should see something similar to:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest xxxxxxxxxxxx ... ...📄 Step 3: Verify the Source File
The task states that sample.txt already exists under:
/tmpCheck it:
ls -l /tmp/sample.txtOptionally inspect its contents:
cat /tmp/sample.txt📂 Step 4: Create the Host Directory
The required host directory is:
/opt/dataMake sure it exists:
sudo mkdir -p /opt/dataVerify:
ls -ld /opt/data📋 Step 5: Copy sample.txt
Copy the existing file:
sudo cp /tmp/sample.txt /opt/data/Verify:
ls -l /opt/dataYou should see:
sample.txtCheck the copied file:
cat /opt/data/sample.txtThe host now looks like:
/opt/
└── data/
└── sample.txt🚀 Step 6: Create the games Container
Create the container using the Nginx image:
docker run -d \
--name games \
-v /opt/data:/tmp \
nginx:latestIf Docker requires elevated privileges:
sudo docker run -d \
--name games \
-v /opt/data:/tmp \
nginx:latest🧩 Command Breakdown
Detached Mode
-dRuns the container in the background.
This is important because the requirement says the container must remain in a running state.
Container Name
--name gamesSets the container name to:
gamesVolume Mapping
-v /opt/data:/tmpThe syntax is:
-v <host_path>:<container_path>Therefore:
Host Path → /opt/data
Container Path → /tmpThe mapping becomes:
/opt/data:/tmpDocker Image
nginx:latestCreates the container from the Nginx image.
🧠 How the Volume Mapping Works
On the host:
/opt/data/
│
└── sample.txtThe directory is mounted into:
Container
│
└── /tmp/
│
└── sample.txtSo:
App Server 3
│
├── /tmp/sample.txt
│
│ cp
│ ↓
│
└── /opt/data/
│
└── sample.txt
│
│ Docker bind mount
▼
games container
│
└── /tmp/sample.txt🔍 Step 7: Verify the Container Is Running
Run:
docker psYou should see:
NAMES
gameswith a running status such as:
Up ...You can also specifically check:
docker ps --filter name=gamesThe container must remain running to satisfy the task.
🔎 Step 8: Verify the Mount
Inspect the container:
docker inspect gamesLook for the Mounts section.
It should show information equivalent to:
Source → /opt/data
Destination → /tmpThis confirms:
/opt/data
↓
Docker Mount
↓
/tmp📄 Step 9: Verify sample.txt Inside the Container
There is no need to manually enter the container.
Use docker exec:
docker exec games ls -l /tmpYou should see:
sample.txtThen:
docker exec games cat /tmp/sample.txtThe content should match:
cat /opt/data/sample.txtThis proves that the volume mapping works.
🧪 Understanding the Persistence
Because /tmp inside the container is mapped to /opt/data on the host:
Container
/tmp/sample.txt
│
│ Same mounted storage
▼
Host
/opt/data/sample.txtFor example, if you create:
docker exec games touch /tmp/test.txtthe host should also see:
ls /opt/datasample.txt
test.txtThis demonstrates that data in the mounted path is stored through the host directory rather than only in the container's writable layer.
🆚 Host Directory vs Container Directory
For this task:
The file first moves:
/tmp/sample.txt
│
│ cp
▼
/opt/data/sample.txtThen Docker exposes it inside the container:
/opt/data/sample.txt
│
│ -v /opt/data:/tmp
▼
/tmp/sample.txt⚠️ Important Notes
⚙️ Complete Workflow
Pull Nginx:
docker pull nginx:latestCheck the source file:
ls -l /tmp/sample.txtCreate the host directory:
sudo mkdir -p /opt/dataCopy the file:
sudo cp /tmp/sample.txt /opt/data/Verify:
ls -l /opt/dataCreate the container:
docker run -d \
--name games \
-v /opt/data:/tmp \
nginx:latestCheck the container:
docker psVerify the mounted file:
docker exec games ls -l /tmpdocker exec games cat /tmp/sample.txtInspect the mount:
docker inspect games🧪 Validation Checklist
📌 Summary
Pull the image:
docker pull nginx:latestPrepare the host volume:
sudo mkdir -p /opt/data
sudo cp /tmp/sample.txt /opt/data/Create the container:
docker run -d \
--name games \
-v /opt/data:/tmp \
nginx:latestVerify:
docker ps
docker exec games ls -l /tmp
docker exec games cat /tmp/sample.txtThe complete flow is:
App Server 3
│
├── /tmp/sample.txt
│ │
│ │ cp
│ ▼
│
├── /opt/data/
│ └── sample.txt
│ │
│ │ -v /opt/data:/tmp
│ ▼
│
└── Docker
│
└── games
│
└── /tmp/
└── sample.txt ✅The key command is:
docker run -d --name games -v /opt/data:/tmp nginx:latest> Docker bind mounts map a host directory into a container. Here, /opt/data on App Server 3 is mounted at /tmp inside the games container, allowing sample.txt to be accessed through both locations while keeping the Nginx container running.