How do I create a .env file in Docker

Viewed 21541

I'm quite new to Docker and .Net Core (been using .Net Framework for ages).

I've created a new .Net Core 3 app and set it to use Docker and the app builds and runs. So far so good. This app needs to connect to GoogleBigQuery and, as such, needs the Google Cloud credentials to be stored in an environment variable (took a frustrating hour to realise that Docker doesn't use the Windows environment variables).

From what I've been reading, I would just add them to the .env file, but I don't have one in my project directory.

I've tried using the command line to set them like this docker run containername:dev --env VAR1=value1 but I get the following error:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "-e": executable file not found in $PATH: unknown.

I've also seen references to the docker-compose.yml file.

I've tried creating the .env file and tried (unsuccessfully) using the CLI to associate it with the container as follows:

docker run -i containername:dev --env-file .env
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "--env-file": executable file not found in $PATH: unknown.

So I'm not sure where to go from here. Everything I've read seems to assume that you already have a .env and I can't find anything around creating a .env manually. I did create a docker-compose.yml file which I was hoping Docker would recognise and subsequently find the .env:

version: '3.5'

services:
  fms-techmobile-utilities:
    build:
      context: .
      dockerfile: Local.Dockerfile
    image: foo
    container_name: foo
    restart: unless-stopped
    env_file:
      - .env

The above didn't work, so I really don't know what else to try. I did try stopping and restarting the Docker container in the hopes that it would find the yml and env files, but nope

5 Answers

docker run containername:dev --env VAR1=value1
is passing the command into containername:dev and asking it to run it.

Changing the order of the command to:
docker run -e VAR1-value1 containername:dev
will ask Docker to run the command and pass the value into the container.

To use an environment file replace the -e with --env-file ./filename. For example:
docker run --env-file ./my_env containername:dev

This assumes the file my_env is in the directory you are running the command from.

.env files are for docker-compose. You need to use the docker-compose cli instead of just docker.

Then you just need to do this:

docker-compose up -d 

and everything should work

W/o Docker Compose

Docker containers do not know about environment variables on the host system. To set an environment variable in a container, you have to specify it:

docker run -i containername:dev -e VARNAMEINCONTAINER='value'

To simplify this, you can create an "env file", which is a simple text file that looks like this

myenvfile

VARNAME=value
FOO=bar

And tell docker to use it

docker run -i containername:dev --env-file=myenvfile

This will set VARNAME and FOO in your container.

Docs: https://docs.docker.com/engine/reference/commandline/run/

Docker Compose

Imagine you need to start multiple containers. This would be a pain if we had to manually call docker run... for each container with all parameters. That's why we can use docker compose.

Docker compose has the docker-compose.yml file that describes what to start and replaces the docker run commands. Again, we have to provide the environment variables:

version: '3.5'

services:
  fms-techmobile-utilities:
    build:
      context: .
      dockerfile: Local.Dockerfile
    container_name: foo
    environment:
      - FOO=bar

Again, we often need environment variables multiple times in a docker-compose.yml file, so we can use a text file called .env to store them:

.env

FOO=bar

and use them in docker-compose.yml

web:
  environment:
    -FOO

.env is the standard name for this file and docker compose will automatically look for this file. Note, that you still have to list the variable names for each service. (= variable name + value in .env and variable name only in docker-compose.yml)

Or you apply all variables from such a text file like so:

web:
  env_file:
    - web-variables.env

(= make all variables from file "web-variables.env" available in this container)

Docs: https://docs.docker.com/compose/environment-variables/

you literally create a file called .env in the same location as your compose file.

you can then add env variables to the .env file such as IMAGE=foo then in the compose file use the line image: ${foo}

Well, after lots of banging my head I found the solution and it's so simple. In the properties page of my web app, under the Debug tab there is an Environment Variables section where you can add the key/value pairs you like. done

enter image description here

Thank you to everyone who took the time to reply.

I'm still left with some uncertainties though:

  • Why don't the .env file and .yml files appear to be used when Visual Studio builds the app?
  • When I had the docker-compose.yml file configured, why does VS seem to insist on creating a NEW container with a different name to the one in the yml file? e.g. in the yml file the container name is Foo. VS created a container called Foo_1, so the env file referenced in the yml file seems to be ignored
Related