0

I'd like to do create a sym link and source an environment file from my docker-compose file when I launch using Docker Stack. Like this:

version: '3'

services:
  hello_world:
    image: nginx:latest
    ports:
     - "80:80"
    volumes:
      - ./config:/config:ro   
      - ./data:/data          
    command:
      - /bin/bash
      - -c
      - |
        ln -s /config/lala /etc/nginx/lala
        source /config/env
        nginx -g 'daemon off;'

You can see that my command first creates a symbolic link, then sources an environment file found within the /config directory that is volume mounted, then launches nginx. I know that there are other ways to put environment variables in the docker-compose file, but I'm trying to decouple environment aspects from the docker-compose file itself.

I'm launching this using Docker Stack, like this:

docker stack deploy -c docker-compose-local.yml nginx-test

but when I shell into the container with:

docker exec -it 5c /bin/bash

I can see that while my symbolic link worked my environment variables are not loaded:

root@5c562e102cf4:/# env
HOSTNAME=5c562e102cf4
NJS_VERSION=1.15.9.0.2.8-1~stretch
NGINX_VERSION=1.15.9-1~stretch
PWD=/
HOME=/root
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env

root@5c562e102cf4:/# cat /etc/nginx/lala 
asdfadf

root@5c562e102cf4:/# source /config/env 
root@5c562e102cf4:/# env
TEST_NGINX_ENV_SETTING=test_setting1
HOSTNAME=5c562e102cf4
NJS_VERSION=1.15.9.0.2.8-1~stretch
TEST_POSTGRES_ENV_SETTING=test_setting2
NGINX_VERSION=1.15.9-1~stretch
PWD=/
HOME=/root
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env

Really appreciate anyone who can help me source this file from the mounted volume OR understand why what I'm trying to do may never work at all.

Bill Noto
  • 541
  • 4
  • 12

1 Answers1

0

Maybe you could try copying the content of your volume in a modified dockerfile and load the environment variables through an entrypoint.

The dockerfile would be:

FROM nginx:latest
COPY ./config /config

ENTRYPOINT ["bash", "start.sh"]

start.sh would be:

# !/bin/bash
source /config/env
nginx -g 'daemon off;'

And the stack.yml file would be:

version: '3'

services:
  nginx:
    image: nginx:latest
    ports:
     - "80:80"
    volumes:
      - ./data:/data

Note: you could also add your symbolic link in the entrypoint.

Rodrigo Loza
  • 1,200
  • 7
  • 14