• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Docker | Container Configuration
  1. Set Environment Variables
  2. Set Labels
  3. Set the Container's Hostname

  1. Set Environment Variables
    The "docker container run" command has two options that allow you to set environment variables for your containers:
    -e, --env NAME=VALUE         Set environment variables
        --env-file file          Read in a file of environment variables
    Let's try the option "-e" ("--env"):
    $ docker container run --rm --env my_env_var_1="my env var 1 value" --env my_env_var_2="my env var 2 value" ubuntu:latest /bin/bash -c "env | grep my_env_var_"
    my_env_var_1=my env var 1 value
    my_env_var_2=my env var 2 value
    The two environment variables were set properly. The applications running inside the container can use them as they would in a regular deployment in a native host.

    In some cases, setting environment variables using the option "--env" can be cumbersome especially if we want to set many of them. Optionally, you can group all the environment variables in a property file and use the option "--env-file" to pass the file to "docker container run" command.

    Let's create a property file:
    $ vi env-file-1
    my_env_var_1=my env var 1 value
    my_env_var_2=my env var 2 value
    my_env_var_3=my env var 3 value
    Let's use the option "--env-file":
    $ docker container run --rm --env-file env-file-1 ubuntu:latest /bin/bash -c "env | grep my_env_var_"
    my_env_var_1=my env var 1 value
    my_env_var_2=my env var 2 value
    my_env_var_3=my env var 3 value
  2. Set Labels
    The "docker container run" command has two options that allow you to set labels for your containers:
    -l, --label list                       Set meta data on a container
        --label-file list                  Read in a line delimited file of labels
    Let's try the option "-l" ("--label"):
    $ docker container run --rm -d --label my_label_1="my label 1 value" --label my_label_2="my label 2 value" nginx:latest
    9da697d8336291e8afb968224ff9402bf2d1dfc904275c7c392e44d8c1155e6b
    Let's check the labels:
    $ docker container inspect b0411f9636 --format '{{json .Config.Labels }}' | jq
    {
      "my_label_1": "my label 1 value",
      "my_label_2": "my label 2 value"
    }
    The two labels were set properly. We can use them to filter containers:
    $ docker container ls -a --filter label="my_label_1=my label 1 value"
    CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
    b0411f9636d8   nginx:latest   "/docker-entrypoint.…"   9 minutes ago   Up 9 minutes   80/tcp    modest_ishizaka

    In some cases, setting labels using the option "--label" can be cumbersome especially if we want to set many of them. Optionally, you can group all the labels in a property file and use the option "--label-file" to pass the file to "docker container run" command.

    Let's create a property file:
    $ vi label-file-1
    my_label_1=my label 1 value
    my_label_2=my label 2 value
    my_label_3=my label 3 value
    Let's use the option "--env-file":
    $ docker container run --rm -d --label-file label-file-1 nginx:latest
    cc5164ddc8993f3d383e00345195dc495d72479271752bf812f11024208cc109
    Let's check the labels:
    $ docker container inspect cc5164ddc --format '{{json .Config.Labels }}' | jq
    {
      "my_label_1": "my label 1 value",
      "my_label_2": "my label 2 value",
      "my_label_3": "my label 3 value"
    }
  3. Set the Container's Hostname
    When you run a container, it gets automatically the /etc/hostname file initialized with its ID:
    $ docker container run --rm ubuntu:latest /bin/bash -c "cat /etc/hostname"
    20f335d7ac41
    To assign a specific host name to the container, we can use the option -h (--hostname) with the command docker container run:
    $ docker container run --rm --hostname ubuntu-latest ubuntu:latest /bin/bash -c "cat /etc/hostname"
    ubuntu-latest
© 2025  mtitek