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
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 -e VARNAMEINCONTAINER='value' -i containername:dev
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 --env-file=myenvfile -i containername:dev
This will set VARNAME and FOO in your container.
Docs: https://docs.docker.com/engine/reference/commandline/run/
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With