How to build a Vapor Docker image with an environment variable?

I use Docker images when developing apps with Vapor. It’s a great way to run the code in the final deployment environment right from the start of the project. I use environment variables to direct the code execution and Docker provides a nice way to set them when building an image.

My usual build command looks something like this:

docker build -f Dockerfile-dev --build-arg env=development -t yetanotherapp:latest .

The command builds an image defined in Dockerfile-dev file and sets an environment variable env. The command also sets a tag for the image.

After the build I can spin up a container from the image and get the app running with a command:

docker run -p 8080:8080 yetanotherapp:latest