dockerdocker-compose

Adding files to standard images using docker-compose


I'm unsure if something obvious escapes me or if it's just not possible but I'm trying to compose an entire application stack with images from docker hub.

One of them is mysql and it supports adding custom configuration files through volumes and to run .sql-files from a mounted directory.

But, I have these files on the machine where I'm running docker-compose, not on the host. Is there no way to specify files from the local machine to copy into the container before it runs it entrypoint/cmd? Do I really have to create local images of everything just for this case?


Solution

  • If you can not use volumes (wants stateless docker-compose.yml and using remote machine), you can have config file written by command.

    Example for nginx config in official image:

    version: "3.7"
    
    services:
      nginx:
        image: nginx:alpine
        ports:
          - 80:80
        environment:
          NGINX_CONFIG: |
            server {
              server_name "~^www\.(.*)$$" ;
              return 301 $$scheme://$$1$$request_uri ;
            }
            server {
              server_name example.com
              ...
            }
        command:
          /bin/sh -c "echo \"$$NGINX_CONFIG\" > /etc/nginx/conf.d/redir.conf; nginx -g \"daemon off;\""
    

    Environment variable could also be saved in .env file, you can use Compose's extend feature or load it from shell environment (where you fetched it from enywhere else):

    https://docs.docker.com/compose/compose-file/#env_file https://docs.docker.com/compose/compose-file/#variable-substitution

    To get the original entrypoint command of a container:

    docker container inspect [container] | jq --raw-output .[0].Config.Cmd
    

    To investigate which file to modify this usually will work:

    docker exec --interactive --tty [container] sh