-
Notifications
You must be signed in to change notification settings - Fork 6
Docker
In the context of docker compose and within a docker network services talk to each other on their internal port number, and not the port that is exposed to the host. For example, given a stack with the following config
name: api
services:
api:
environment:
NB_API_ALLOWED_ORIGINS: '*'
NB_API_PORT: "8000"
NB_GRAPH_ADDRESS: graph
NB_GRAPH_DB: test_data/query
NB_GRAPH_PASSWORD: admin
NB_GRAPH_PORT: "5820"
NB_GRAPH_USERNAME: admin
NB_RETURN_AGG: "false"
image: neurobagel/api:test
networks:
default: null
ports:
- mode: ingress
target: 8000
published: "8888"
protocol: tcp
graph:
image: stardog/stardog:8.2.2-java11-preview
networks:
default: null
ports:
- mode: ingress
target: 5820
published: "5821"
protocol: tcp
volumes:
- type: bind
source: /home/ubuntu/stardog_root_test
target: /var/opt/stardog
bind:
create_host_path: true
query:
environment:
API_QUERY_URL: http://localhost:8000/
image: neurobagel/query_tool:latest
networks:
default: null
ports:
- mode: ingress
target: 3000
published: "3000"
protocol: tcp
networks:
default:
name: api_default
The api will be sending its requests to the port 5820
and not 5821
.
This may introduce issues for the API -> graph connection when the running API is not in the same Docker network as the graph, e.g. if you have locally spun up an API using Python but want to communicate with a Dockerized graph endpoint. In this scenario, you would likely need to manually specify the host port for the graph container in the query URL, rather than using the container port.
-
environment
instruction for a specific serve (in docker-compose.yml
)- Defines variables to be set inside the container (compose config file itself cannot see the variables)
-
env_file
instruction for a specific service (in docker-compose.yml
)- Only sets variables inside the container (compose config file itself cannot see the variables)
- Can replace / works without
environment
instruction to set variables in the container - Values inside specified env file can be quoted or unquoted, with same result in
docker compose config
-
--env-file
CLI argument (e.g.,docker compose --env-file custom.env ...
)- Only seen by the compose config itself, UNLESS the
environment
instruction is also used (in which case, variables that are defined underenvironment
which also have a value inside the specified env file are passed to and can be used inside the container) - Essentially just for specifying a custom path for the
.env
file, and works the same as having a file named.env
(in which case the--env-file
argument is not needed) - Does not care about quotes around variable values in the file
- Only seen by the compose config itself, UNLESS the
Some important references:
- https://docs.docker.com/compose/environment-variables/set-environment-variables/
-
https://docs.docker.com/compose/compose-file/05-services/ (see sections on
env_file
andenvironment
) - https://docs.docker.com/compose/environment-variables/env-file/
- https://docs.docker.com/compose/environment-variables/envvars-precedence/
All three options below can set variables inside the container.
-
ENV
instruction in dockerfile- Should work the same as
--env
/-e
command-line argument (e.g.,docker run -e MYVAR=MYVALUE
) - Quotes are needed for variable values with spaces
- Not required for an environment variable to be available to code in the container, such as if one of the below two options are used
- Should work the same as
-
--env-file
CLI argument- Interprets value exactly as is inside specified file (including any quotes), does not do any parameter expansion for values inside the file (i.e., for
VAR=VALUE
, the VALUE is passed in exactly as is, as a literal) -
IMPORTANT: THIS IS (FOR SOME REASON) NOT THE SAME FUNCTIONALLY AS THE
--env-file
CLI ARGUMENT FOR DOCKER COMPOSE!- See this commment https://github.com/docker/cli/issues/4347#issuecomment-1590214094 from a Docker maintainer
- Unfortunately these caveats are not very well documented 😢
- Interprets value exactly as is inside specified file (including any quotes), does not do any parameter expansion for values inside the file (i.e., for
-
--env
/-e
CLI argument- Quotes are needed for variable values with spaces, will override the same variable set via
ENV
instruction in the dockerfile
- Quotes are needed for variable values with spaces, will override the same variable set via
Some important references:
- https://docs.docker.com/engine/reference/commandline/run/#env
- https://docs.docker.com/engine/reference/builder/#env
-
https://stackoverflow.com/a/63640896 (this answer mostly explains
ARG
/ENV
but also covers some info on bothdocker run
anddocker compose
)- a summary of https://vsupalov.com/docker-arg-env-variable-guide/