>_ IzzyLab

Hands-on task

This task is step 9 of the course “Docker from scratch”

Settings through environment variables

easy~7 min

The service from the izzylab/minihttp:1 image takes its settings from environment variables: NAME — the name it introduces itself with, PORT — the port it listens on inside the container (8080 by default).

Start a container hello from this image in the background so that the service introduces itself as hello-svc, listens on port 9090 inside and opens from the server at http://127.0.0.1:9090/.

What you'll practise:
  • docker run -e
  • environment variables

Topic breakdown

You run the same image with different settings without rebuilding it: settings are passed as environment variables. The -e flag of docker run sets a variable inside the container; you can use as many of them as you like.

docker run -d --name api -e NAME=orders -e LANG=C.UTF-8 izzylab/minihttp:1

Which variables an image understands is written in its documentation. To see what a container was started with:

docker exec name printenv         # all variables inside
docker inspect -f '{{.Config.Env}}' name

Variables, like ports, are fixed when the container is created: to change a value, you recreate the container. If a variable changes the service's port inside, the -p publishing must point to the new port.

What's checked

  1. hello from izzylab/minihttp:1 is running
  2. NAME and PORT from the task are set in the container
  3. The service responds from the server at http://127.0.0.1:9090/

What it looks like