How do I create a .env file in Docker

12,654

Solution 1

.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

Solution 2

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.

Solution 3

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}

Solution 4

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/

Share:
12,654

Related videos on Youtube

Adam Hey
Author by

Adam Hey

Updated on June 04, 2022

Comments

  • Adam Hey
    Adam Hey almost 2 years

    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

  • Adam Hey
    Adam Hey almost 3 years
    Thanks Justin, I tried that. I'm still getting the same issue though. I have the .env and docker-compose.yml files in the project directory and my environment variable is in the .env file. At tun time it's still null though: `Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CRED‌​ENTIALS") null'
  • Adam Hey
    Adam Hey almost 3 years
    Thanks. I downloaded the Compose installer and ran it. A console window appeared for a second and nothing else happened. In a console window in my project's directory, I ran "docker compose up" and got the following: [+] Building 0.0s (1/2) => [internal] load build definition from Local.Dockerfile 0.0s => => transferring dockerfile: 2B I restarted the container and VS and the env var is still null at runtime
  • Adam Hey
    Adam Hey almost 3 years
    Thanks Cristoph. I had already tried your w/o Docker suggestion to no avail. I ran the Compose installer and a console window appeared for a second and nothing else happened. I had already manually added the .env and yml files and the ENV VAR is still null at runtime. To confirm - the .env and yml files should be in web app's project directory, not in a docker/compose installation directory?
  • richardsefton
    richardsefton almost 3 years
    I think you might be confusing env vars and args
  • Adam Hey
    Adam Hey almost 3 years
    Not sure what you mean regarding args. I definitely need an environment variable because the Google BigQuery SDK needs the credentials to be stored as one. I have that in a .env file, but it seems docker is not seeing that file. I also created the yml file which references the env file. I tried using the console to set the variable and also to set the env file, but those commands didn't work, as per my original post
  • richardsefton
    richardsefton almost 3 years
    The ARG instruction defines a variable that users can pass at build-time to the builder - docs.docker.com/engine/reference/builder/#arg
  • Justin Lessard
    Justin Lessard almost 3 years
    @AdamHey Read the doc on how to set a environment variable in a container. Variables defined in your .env files are not automatically set inside your containers.
  • Christoph Lütjen
    Christoph Lütjen almost 3 years
    It's hard to understand how your setup looks in detail. So just some notes: If you install docker desktop, docker compose is already installed and there's no need for an additional installer. I guess, compose is not what you're looking for (for now).