Skip to content

Latest commit

 

History

History
88 lines (60 loc) · 2.83 KB

Docker Volumes Mapping.md

File metadata and controls

88 lines (60 loc) · 2.83 KB

Problem Statement

The Nautilus DevOps team is testing application containerization, with plans to migrate applications to Docker container-based environments soon. In today's stand-up meeting, one of the team members has been assigned the task to:

  • Pull the nginx Docker image (preferably the latest tag) on App Server 3 in the Stratos Datacenter.
  • Create a new container named official from the pulled image.
  • Map the host volume /opt/itadmin to the container volume /usr/src/.
  • Copy a file named sample.txt from /tmp to /opt/itadmin on the host.
  • Ensure the container is in a running state.

Solution

  1. Pull the Docker Image

    Start by pulling the nginx Docker image. It is preferable to use the latest tag, but other tags will work as well.

    docker pull nginx:latest

    This command downloads the nginx:latest image from Docker Hub to App Server 3.

  2. Create and Run the Container

    Create a new container named official using the pulled nginx:latest image. Bind the host volume /opt/itadmin to the container volume /usr/src/ to ensure file sharing between the host and the container.

    docker run -it -d --name official -v /opt/itadmin:/usr/src/ nginx:latest
    • -it runs the container in interactive mode with a terminal attached.
    • -d runs the container in detached mode (in the background).
    • --name official assigns the name official to the container.
    • -v /opt/itadmin:/usr/src/ mounts the host directory /opt/itadmin to the container directory /usr/src/.
  3. Verify Volume Binding

    To check whether the volume binding was successful, inspect the container’s configuration. Look for the volume mounts section in the container details.

    docker inspect official

    Example output (relevant part):

    "Mounts": [
        {
            "Type": "bind",
            "Source": "/opt/itadmin",
            "Destination": "/usr/src",
            "Mode": "",
            "RW": true,
            "Propagation": "rprivate"
        }
    ]

    This output confirms that /opt/itadmin on the host is correctly bound to /usr/src in the container.

  4. Copy the File to the Host Volume

    Move the sample.txt file from /tmp to /opt/itadmin on the host. This file will be accessible from within the container through the mounted volume.

    mv /tmp/sample.txt /opt/itadmin/
  5. Verify File Access in the Container

    To ensure that the sample.txt file is accessible within the container, execute a shell in the official container and check the file’s contents.

    docker exec -it official /bin/bash

    Inside the container, use the following command to view the file:

    cat /usr/src/sample.txt

    Expected output:

    This is a sample file!!