Skip to main content

Development

Docker Bootcamp – Environment Variables

Open Container

Welcome back to Docker Bootcamp. In this post, we’ll cover the use of environment variables. These special variables let you modify the execution of the application inside the container without modifying the application’s files.  Each time you create a container from the same image, you can pass different values for the environment variables.

Terminology

  • Environment Variable – A key-value pair used to change the settings of a program without modifying the program’s files.

Commands

  • create – Create a new container in a stopped state
  • run – Create and start a new container

Command Details

You can find a full list of commands and all available flags at https://docs.docker.com/engine/reference/commandline/docker/.

create

docker create [options] image [command] [args…]
Outputs: ContainerID

–env -e O Set an environment variable
–env-file O Read a file containing one or more environment variables

 

  • If an environment variable has a default value provided by the image, passing in a value for the same key will override the default value.
  • You can inject any environment variable into a container, even if the program does not respond to that variable.

run

docker run [options] image [command] [args…]

–detach -d O Run container in detached mode (no console input/output)
  • See “create” command details for other available options

Examples

Pass in a single variable

  • Create, start and run an interactive container with an environment variable
    • docker run -it –env TestKey=TestVal –name envtest1 busybox /bin/sh
  • View environment variables
    • printenv
      • Notice the provided TestKey variable is listed
  • Stop the container
    • exit
  • Inspect the container
    • docker inspect envtest1
      • Notice the provided TestKey is listed under the Config/Env section

 

Pass in a file of variables

  • Create a text file with multiple key=value pairs named env.txt
    • TestKey1=TestValue1
    • TestKey2=TestValue2
    • TestKey3=TestValue3
  • Create, start and run an interactive container that reads environment variables from a file
    • docker run -it –env-file <path_to_file> –name envtest2 busybox /bin/sh
  • View environment variables
    • printenv
      • Notice the provided TestKey variables are listed
  • Stop the container
    • exit
  • Inspect the container
    • docker inspect envtest2
      • Notice the provided TestKey variables are listed under the Config/Env section

Real-World Environment Variables

  • View the image details page for MySql database on docker hub
  • Create, start and run a detached container
    • docker run -d –name mysqltest1 mysql
      • Notice the container exits immediately
  • View docker logs
    • docker logs mysqltest1
      • Notice the error saying we must provide at least one of the specified environment variables
  • Create, start and run a detached container with the required environment variable
    • docker run -d –env MYSQL_ROOT_PASSWORD=my-secret-pw –name mysqltest2 mysql
      • Notice the container runs as expected
  • View docker logs
    • docker logs mysqltest2
      • Notice the application is running and producing logs as expected
  • Inspect the container
    • docker inspect mysqltest2
      • Notice the MYSQL_ROOT_PASSWORD variable is displayed
      • Notice the other available environment variables from the image details page are not listed. Make sure to refer to the image documentation any time you use a new image you are not familiar with.
  • Stop the container
    • docker stop mysqltest2

Stay tuned for a future post where we will pass in sensitive data in environment variables in a secure way.  Until then, happy containers!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Eric Sanner, Solutions Architect

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram