Back to Engineering Notes
DockerEngineering Note

11. Docker Volumes Mapping

Create and test an Nginx container on App Server 3 in Stratos DC using Docker volume mapping.

🎯 Objective

Create and test an Nginx container on App Server 3 in Stratos DC using Docker volume mapping.

Requirements:

Pull the nginx Docker image
Create a container named games
Map host directory /opt/data to container directory /tmp
Copy the existing /tmp/sample.txt file on the host into /opt/data
Keep the games container running
Verify that the file is accessible inside the container through the mounted volume

Final mapping:

plain text
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:

plain text
Docker Host                 Container

/opt/data                   /tmp
    │                         │
    └── sample.txt            └── Independent storage

With the required mapping:

plain text
Docker Host                 Container

/opt/data  ───────────────► /tmp
    │                         │
    └── sample.txt ────────► sample.txt

Changes 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:

bash
docker ps

You can also check whether the Nginx image already exists:

bash
docker image ls

📥 Step 2: Pull the Nginx Image

Pull the latest Nginx image:

bash
docker pull nginx:latest

You can also use:

bash
docker pull nginx

When no tag is specified, Docker uses latest by default.

Verify:

bash
docker image ls nginx

You should see something similar to:

plain text
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    xxxxxxxxxxxx   ...           ...

📄 Step 3: Verify the Source File

The task states that sample.txt already exists under:

plain text
/tmp

Check it:

bash
ls -l /tmp/sample.txt

Optionally inspect its contents:

bash
cat /tmp/sample.txt

📂 Step 4: Create the Host Directory

The required host directory is:

plain text
/opt/data

Make sure it exists:

bash
sudo mkdir -p /opt/data

Verify:

bash
ls -ld /opt/data

📋 Step 5: Copy sample.txt

Copy the existing file:

bash
sudo cp /tmp/sample.txt /opt/data/

Verify:

bash
ls -l /opt/data

You should see:

plain text
sample.txt

Check the copied file:

bash
cat /opt/data/sample.txt

The host now looks like:

plain text
/opt/
 └── data/
      └── sample.txt

🚀 Step 6: Create the games Container

Create the container using the Nginx image:

bash
docker run -d \
  --name games \
  -v /opt/data:/tmp \
  nginx:latest

If Docker requires elevated privileges:

bash
sudo docker run -d \
  --name games \
  -v /opt/data:/tmp \
  nginx:latest

🧩 Command Breakdown

Detached Mode

bash
-d

Runs the container in the background.

This is important because the requirement says the container must remain in a running state.


Container Name

bash
--name games

Sets the container name to:

plain text
games

Volume Mapping

bash
-v /opt/data:/tmp

The syntax is:

plain text
-v <host_path>:<container_path>

Therefore:

plain text
Host Path      → /opt/data
Container Path → /tmp

The mapping becomes:

plain text
/opt/data:/tmp

Docker Image

bash
nginx:latest

Creates the container from the Nginx image.


🧠 How the Volume Mapping Works

On the host:

plain text
/opt/data/
    │
    └── sample.txt

The directory is mounted into:

plain text
Container
   │
   └── /tmp/
         │
         └── sample.txt

So:

plain text
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:

bash
docker ps

You should see:

plain text
NAMES
games

with a running status such as:

plain text
Up ...

You can also specifically check:

bash
docker ps --filter name=games

The container must remain running to satisfy the task.


🔎 Step 8: Verify the Mount

Inspect the container:

bash
docker inspect games

Look for the Mounts section.

It should show information equivalent to:

plain text
Source      → /opt/data
Destination → /tmp

This confirms:

plain text
/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:

bash
docker exec games ls -l /tmp

You should see:

plain text
sample.txt

Then:

bash
docker exec games cat /tmp/sample.txt

The content should match:

bash
cat /opt/data/sample.txt

This proves that the volume mapping works.


🧪 Understanding the Persistence

Because /tmp inside the container is mapped to /opt/data on the host:

plain text
Container
/tmp/sample.txt
      │
      │ Same mounted storage
      ▼
Host
/opt/data/sample.txt

For example, if you create:

bash
docker exec games touch /tmp/test.txt

the host should also see:

bash
ls /opt/data
plain text
sample.txt
test.txt

This 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:

plain text
/tmp/sample.txt
      │
      │ cp
      ▼
/opt/data/sample.txt

Then Docker exposes it inside the container:

plain text
/opt/data/sample.txt
      │
      │ -v /opt/data:/tmp
      ▼
/tmp/sample.txt

⚠️ Important Notes

docker pull nginx downloads the Nginx image.
docker run creates and starts a new container.
d keeps the Nginx container running in detached mode.
-name games names the container games.
v /opt/data:/tmp creates a bind mount.
/opt/data is the host path.
/tmp is the container path.
Files in the mounted host directory become visible inside the container.
Existing contents at the container's mount destination can be hidden while the bind mount is active.
The host directory must contain sample.txt as required by the task.

⚙️ Complete Workflow

Pull Nginx:

bash
docker pull nginx:latest

Check the source file:

bash
ls -l /tmp/sample.txt

Create the host directory:

bash
sudo mkdir -p /opt/data

Copy the file:

bash
sudo cp /tmp/sample.txt /opt/data/

Verify:

bash
ls -l /opt/data

Create the container:

bash
docker run -d \
  --name games \
  -v /opt/data:/tmp \
  nginx:latest

Check the container:

bash
docker ps

Verify the mounted file:

bash
docker exec games ls -l /tmp
bash
docker exec games cat /tmp/sample.txt

Inspect the mount:

bash
docker inspect games

🧪 Validation Checklist

[ ] Connected to App Server 3
[ ] Nginx image pulled
[ ] /tmp/sample.txt exists on the host
[ ] /opt/data directory exists
[ ] sample.txt copied to /opt/data
[ ] Container named games created
[ ] /opt/data mapped to /tmp
[ ] Container is running
[ ] docker ps shows games
[ ] /tmp/sample.txt is visible inside games
[ ] Mount source is /opt/data
[ ] Mount destination is /tmp

📌 Summary

Pull the image:

bash
docker pull nginx:latest

Prepare the host volume:

bash
sudo mkdir -p /opt/data
sudo cp /tmp/sample.txt /opt/data/

Create the container:

bash
docker run -d \
  --name games \
  -v /opt/data:/tmp \
  nginx:latest

Verify:

bash
docker ps
docker exec games ls -l /tmp
docker exec games cat /tmp/sample.txt

The complete flow is:

plain text
App Server 3
│
├── /tmp/sample.txt
│       │
│       │ cp
│       ▼
│
├── /opt/data/
│       └── sample.txt
│              │
│              │ -v /opt/data:/tmp
│              ▼
│
└── Docker
      │
      └── games
            │
            └── /tmp/
                  └── sample.txt ✅

The key command is:

bash
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.