Passing Additional Configurations to Docker Containers

Get familiar with additional Docker configurations that help manage the app container.

We have built and run Docker images using very simple Docker build and Docker run commands. But a lot more can be done with the help of flags.

Let's have a look at a few CLI flags that can be used with Docker run commands to make our application more flexible.

Passing environment variables

Using environment variables in the code can prove to be very handy. These could be used for:

  • Setting configuration parameters with ease.

  • Passing credentials instead of hardcoding them in the code, thereby making the app more secure.

  • Having different behaviors based on environment variables, like setting different log levels and providing flexibility.

So let's try to use environment variables in our coffee shop app. One obvious use case is passing the port via an environment variable. This keeps the configuration out of the source code and makes it possible to change the port number without touching the app code.

The app code needs some changes initially so that it can read the port number from the environment variable. Our main.go function will have the changes that are highlighted below:

Get hands-on with 1200+ tech skills courses.