简体   繁体   中英

Docker compose : how to define env_file pointing to some file inside the container and not the physical server

I ve some services running within docker-compose file :

myService:  
          image: 127.0.0.1:myimage
          stdin_open: true
          tty: true
          ports:
            - target: 8800
              published: 8800
              protocol: tcp
              mode: host
          deploy:
            mode: global
            resources:
              limits:
                memory: 1024M
            placement:
              constraints:
                - node.labels.myLabel== one
          env_file:
            - /opt/app/myFile.list # I WANT TO REUSE SOME FILE INSIDE THE CONTAINER
          healthcheck:
            disable: true

As you can see i need to declare an env-file :

env_file:
     - /opt/app/myFile.list # I WANT TO REUSE SOME FILE INSIDE THE CONTAINER

My purpose is how to reuse some file inside the container and not pointing to the physical machine.

Suggestions ?

Docker (Compose) doesn't quite directly support this on its own, but it's fairly easy to add to your image.

Remember that there are two mechanisms to pass command lines to Docker. If you use both an entrypoint and a command, then the entrypoint is launched as the main container process, and passed the command as arguments. This lets you do first-time setup (like set environment variables) and then exec the command.

A typical entrypoint script for this sort of application could look like

#!/bin/sh
if [ -n "$ENV_FILE" ]; then
  . "$ENV_FILE"
fi
exec "$@"

You'd add it to your Docker image

...
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["same", "as", "before"]

(There are several variants that use ENTRYPOINT to name the main application or just a language interpreter. For this pattern you need to move that to the CMD .)

Then when you launch the container set the environment variable the entrypoint script is looking for.

services:
  myservice:
    environment:
      ENV_FILE: /opt/app/myFile.list # inside the container

If you launch a debug shell with docker run --rm myimage sh , that goes through the entrypoint script and you will get to see these environment variables. docker exec bypasses the entrypoint and you do not get the same environment. Low-level debugging tools like docker inspect won't show the environment variables either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM