Working with Docker Volumes

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Intermediate

In this Live Environment, you are working on a personal project for a friend who wants to host a website. You have a server running Docker and figure this would be a good use case to containerize the small HTML website. Create a Dockerfile that will use Nginx as the base image and set up two volumes: one volume for the HTML files and the second for logs. Then run the container using the image built from the Dockerfile.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Working with Docker Volumes

Introduction

In this hands-on lab, we need to create a Dockerfile that uses nginx as the base image with two volumes. This image will be used to create a Docker container.

Solution

Open a terminal session, and log in to the lab server via SSH using the credentials provided on the lab page:

ssh cloud_user@<PUBLIC_IP_ADDRESS>

Then, become the root user:

sudo su -

Create a Dockerfile

  1. Create a Dockerfile:

    vi Dockerfile
  2. Enter the following:

    FROM nginx
    VOLUME /usr/share/nginx/html
    VOLUME /var/log/nginx
    WORKDIR /usr/share/nginx/html

    Save and exit the file.

  3. Build the image:

    docker build -t la/nextgen:latest -f Dockerfile  .

Create a Volume for the HTML Files

  1. Create a volume called nginx-code:

    docker volume create nginx-code

Create a Volume for Nginx Logs

  1. Create a second volume called nginx-logs:

    docker volume create nginx-logs

Create a Docker Container

  1. Create a container called next nextgen-dev:

    docker run -d --name=nextgen-dev -p 80:80 --mount source=nginx-code,target=/usr/share/nginx/html --mount source=nginx-logs,target=/var/log/nginx la/nextgen:latest
  2. Check the container status:

    docker ps
  3. Check out the volume:

    cd /var/lib/docker/volumes/
    
    ls
  4. Change directory:

    cd nginx-code/_data/
    
    ls
  5. Modify the HTML file:

    vi index.html
  6. In the first <p> tag, add:

    Hello there!

    Save and exit the file.

  7. In a browser tab, paste in the public IP address of the lab server. This should return the "Welcome to nginx!" web page.

Conclusion

Congratulations on completing this hands-on lab!