>_ IzzyLab

Hands-on task

This task is step 1 of the course “Docker: everyday work”

Site files from the server: -v

easy~8 min

A site page lives in /root/site. Start nginx in the background from the nginx:1.27-alpine image, named shop, so that:

  • the site opens from the server at http://127.0.0.1:8081/;
  • nginx serves the files straight from /root/site — an edit to a file on the server shows up on the site right away, with no copying into the container.

nginx in the container takes the site files from /usr/share/nginx/html and listens on port 80.

What you'll practise:
  • docker run -v
  • bind mount
  • volumes

Topic breakdown

A reminder: docker run -d --name name starts a container in the background under a readable name, and -p server_port:container_port publishes a container port on the server.

Everything a process writes inside a container lives in the container's layer and disappears along with the container. Data that has to live longer, and files you want to edit from outside, are attached with the -v flag:

docker run -d -v /srv/conf:/etc/app busybox:1.36 sleep 600
  • bind mount — -v /path/on/server:/path/in/container: the server directory is visible in the container as is. Changes on either side are visible on the other right away. The directory in the container gets covered — its previous contents are no longer visible.
  • named volume — -v name:/path: Docker creates the storage itself and keeps it separate from the container (docker volume ls). That's how database data is stored.

They differ by the first character: a path starting with / is a server directory, a plain word is a volume name. You can't add -v to a container that already exists — only recreate it.

What's checked

  1. shop from nginx:1.27-alpine is running
  2. /root/site is attached as the nginx site directory
  3. http://127.0.0.1:8081/ serves the page from /root/site

What it looks like