DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Docker Volumes Mapping

The Nautilus DevOps team is testing applications containerization, which is supposed to be migrated on docker container-based environments soon. In today's stand-up meeting one of the team members has been assigned a task to create and test a docker container with certain requirements. Below are more details:

a. On App Server 1 in Stratos DC pull nginx image (preferably latest tag but others should work too).

b. Create a new container with name news from the image you just pulled.

c. Map the host volume /opt/itadmin with container volume /tmp. There is an sample.txt file present on same server under /tmp; copy that file to /opt/itadmin. Also please keep the container in running state.


Connect to App Server 1

ssh tony@stapp01
sudo -i
Enter fullscreen mode Exit fullscreen mode

Password for tony: Ir0nM@n


Pull the nginx Image

docker pull nginx:latest
Enter fullscreen mode Exit fullscreen mode

The image was pulled successfully with digest sha256:4c41e2d0a3a55b325ed572cd0200bdb72af1433c3daa2bca6ef8eea68ce504a2.

Verify the image:

docker images | grep nginx
Enter fullscreen mode Exit fullscreen mode

Output:

nginx        latest    14700d2ee6c4   5 days ago   163MB
Enter fullscreen mode Exit fullscreen mode

Verify and Copy the Sample File

Confirm the sample file exists:

ls -la /tmp/sample.txt
Enter fullscreen mode Exit fullscreen mode

Output:

-rw-r--r-- 1 root root 23 Oct 27 11:05 /tmp/sample.txt
Enter fullscreen mode Exit fullscreen mode

Create the target directory and copy the file:

mkdir -p /opt/itadmin
cp /tmp/sample.txt /opt/itadmin/
Enter fullscreen mode Exit fullscreen mode

Verify the copy:

ls -la /opt/itadmin/
Enter fullscreen mode Exit fullscreen mode

Output:

-rw-r--r-- 1 root root   23 Oct 27 11:09 sample.txt
Enter fullscreen mode Exit fullscreen mode

Create the Container with Volume Mapping

docker run -d --name news -v /opt/itadmin:/tmp nginx:latest
Enter fullscreen mode Exit fullscreen mode

The container was created with ID f6d16445bdf5.


Verify the Container is Running

docker ps | grep news
Enter fullscreen mode Exit fullscreen mode

Output:

f6d16445bdf5   nginx:latest   "/docker-entrypoint.…"   16 seconds ago   Up 15 seconds   80/tcp    news
Enter fullscreen mode Exit fullscreen mode

The container news is running.


Verify the Volume Mapping

docker inspect news --format='{{range .Mounts}}{{.Source}} -> {{.Destination}}{{end}}'
Enter fullscreen mode Exit fullscreen mode

Output:

/opt/itadmin -> /tmp
Enter fullscreen mode Exit fullscreen mode

The host /opt/itadmin is correctly mapped to container /tmp.


Verify the File Inside the Container

docker exec news ls -la /tmp/sample.txt
Enter fullscreen mode Exit fullscreen mode

Output:

-rw-r--r-- 1 root root 23 Oct 27 11:09 /tmp/sample.txt
Enter fullscreen mode Exit fullscreen mode

The file is accessible inside the container through the volume mount.


Complete Command Sequence

ssh tony@stapp01
sudo -i

docker pull nginx:latest

mkdir -p /opt/itadmin
cp /tmp/sample.txt /opt/itadmin/

docker run -d --name news -v /opt/itadmin:/tmp nginx:latest

docker ps | grep news
docker inspect news --format='{{range .Mounts}}{{.Source}} -> {{.Destination}}{{end}}'
docker exec news ls -la /tmp/sample.txt
Enter fullscreen mode Exit fullscreen mode

Top comments (0)