>_ IzzyLab

Hands-on task

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

Inside a container: docker exec

easy~7 min

nginx runs in the web container. Without recreating the container:

  1. Find out the Alpine Linux version inside web (it's in the container's /etc/alpine-release file) and write it to /root/os.txt on the server.
  2. Replace the site's main page — the /usr/share/nginx/html/index.html file in the container — with a single line Hello from inside.
What you'll practise:
  • docker exec
  • docker exec -it

Topic breakdown

A container isn't a virtual machine, you don't need SSH to get into it. docker exec starts one more process inside a running container — with its files, network and variables:

docker exec api ls /etc        # one command, the output comes to you
docker exec -it api sh         # a shell inside, exit to leave

-i keeps input open, -t gives a terminal — together they're needed for interactive work. Minimal images often have no bash, so you use sh.

When the exec process ends, the container keeps running: its life is tied to the main process, not to your shell. Changes made inside live as long as the container does — after docker rm and a new docker run they're gone.

What's checked

  1. /root/os.txt has the Alpine version from the web container
  2. web is the same container, and its site serves the new page

What it looks like