Bookmark this page

Guided Exercise: Attaching Persistent Storage to a Container

In this exercise, you will create a container that accesses web content in persistent storage provided by the container host.

Outcomes

You should be able to deploy a container with persistent storage.

On the workstation machine, log in as the student user with student as the password.

On the workstation machine, run the lab containers-storage start command. This command runs a start script that determines if the servera machine is reachable on the network. It also installs the container tools on servera.

[student@workstation ~]$ lab containers-storage start

Procedure 13.4. Instructions

  1. Use the ssh command to log in to servera as the student user. The systems are configured to use SSH keys for authentication, so a password is not required.

    [student@workstation ~]$ ssh student@servera
    ...output omitted...
    [student@servera ~]$ 
  2. Create the /home/student/webcontent/html/ directory, and then create an index.html test page. You will use this directory as persistent storage when you deploy a web server container.

    1. Create the ~/webcontent/html/ directory.

      [student@servera ~]$ mkdir -p ~/webcontent/html/
      [student@servera ~]$ 
    2. Create the index.html file and add some content.

      [student@servera ~]$ echo "Hello World" > ~/webcontent/html/index.html
      [student@servera ~]$ 
    3. Confirm that everyone has access to the directory and the index.html file. The container uses an unprivileged user that must be able to read the index.html file.

      [student@servera ~]$ ls -ld webcontent/html/
      drwxrwxr-x. 2 student student 24 Aug 28 04:56 webcontent/html/
      [student@servera ~]$ ls -l webcontent/html/index.html
      -rw-rw-r--. 1 student student 12 Aug 28 04:56 webcontent/html/index.html
  3. Create an Apache HTTP Server container instance with persistent storage.

    1. Log in to the registry.lab.example.com registry as the admin user with redhat321 as the password.

      [student@servera ~]$ podman login registry.lab.example.com
      Username: admin
      Password: redhat321
      Login Succeeded!
    2. Create a detached container instance named myweb. Redirect port 8080 on the local host to the container port 8080. Mount the ~/webcontent directory from the host to the /var/www directory in the container. Add the :Z suffix to the volume mount option to instruct the podman command to relabel the directory and its content. Use the registry.lab.example.com/rhel8/httpd-24:1-98 image. The following podman run command is very long and should be entered as a single line.

      [student@servera ~]$ podman run -d --name myweb -p 8080:8080 -v ~/webcontent:/var/www:Z registry.lab.example.com/rhel8/httpd-24:1-98
      ...output omitted...
    3. Run the podman ps command to confirm that the container is running, and then use the curl command to access the web content on port 8080.

      [student@servera ~]$ podman ps
      CONTAINER ID  IMAGE                                          COMMAND               CREATED             STATUS                 PORTS                   NAMES
      2f4844b376b7  registry.lab.example.com/rhel8/httpd-24:1-98   /usr/bin/run-http...  About a minute ago  Up About a minute ago  0.0.0.0:8080->8080/tcp  myweb
      [student@servera ~]$ curl http://localhost:8080/
      Hello World
  4. In the preceding step, you used the 1-98 tag to select a particular version of the httpd-24 image. There is a more recent version of that image in the classroom registry. Use the skopeo inspect command to get the registry.lab.example.com/rhel8/httpd-24 image details and retrieve the tag for that version. The following skopeo inspect command is very long and should be entered as a single line.

    [student@servera ~]$ skopeo inspect docker://registry.lab.example.com/rhel8/httpd-24
    {
        "Name": "registry.lab.example.com/rhel8/httpd-24",
        "Digest": "sha256:bafa...a12a",
        "RepoTags": [
            "1-98",
            "1-104",
            "1-105",
            "latest"
        ],
    ...output omitted...

    Under the RepoTags section, notice that there is a more recent version with the tag 1-105.

  5. Stop and delete the myweb container, and then start a new container using the 1-105 image tag. Confirm that the web content in persistent storage has not changed.

    1. Stop and then delete the container.

      [student@servera user]$ podman stop myweb
      2f4844b376b78f8f7021fe3a4c077ae52fdc1caa6d877e84106ab783d78e1e1a
      [student@servera user]$ podman rm myweb
      2f4844b376b78f8f7021fe3a4c077ae52fdc1caa6d877e84106ab783d78e1e1a
    2. Rerun the podman run command that you used in a previous step to start the myweb container, but replace the image tag 1-98 with 1-105. The following podman run command is very long and should be entered as a single line.

      [student@servera ~]$ podman run -d --name myweb -p 8080:8080 -v ~/webcontent:/var/www:Z registry.lab.example.com/rhel8/httpd-24:1-105
      ...output omitted...
    3. Run the podman ps command to verify that the container is running. Use the curl command to confirm that your persistent volume data are preserved, even though you started a new container.

      [student@servera ~]$ podman ps
      CONTAINER ID  IMAGE                                          COMMAND               CREATED             STATUS                 PORTS                   NAMES
      a648c286c653  registry.lab.example.com/rhel8/httpd-24:1-105  /usr/bin/run-http...  About a minute ago  Up About a minute ago  0.0.0.0:8080->8080/tcp  myweb
      [student@servera ~]$ curl http://localhost:8080/
      Hello World
    4. Exit from servera.

      [student@servera ~]$ exit
      logout
      Connection to servera closed.
      [student@workstation ~]$ 

Finish

On the workstation machine, run the lab containers-storage finish script to complete this exercise.

[student@workstation ~]$ lab containers-storage finish

This concludes the guided exercise.

Revision: rh134-8.2-f0a9756