简体   繁体   中英

How to import configurations to dockerfile.

I have a docker-compose.yaml file and then dockerfiles. Now, I don't want to write all the host, port, password in this docker-compose/dockerfile. Rather, I would like to keep all these configurations in a different file. I know, half of the requirement could be achieved by using .env file, where I can keep my environment variables, but then it will be good to keep all configurations in a single file (main.cfg), which could used by other components.

You can use volumes to share a file or a entire folder to a docker service. Here is an example of a mysql which shares a database to docker:

version: '3'
services:
  mysql-database:
    image: mysql
    container_name: mysql-database
    volumes:
      - ./mysql/docker-entrypoint-initdb.d/:/docker-entrypoint-initdb.d
    env_file:
      - .env
    ports:
      - 3306:3306

The directory structure is as follows:

BaseDirectory
|____docker-compose.yml
|    mysql
|    |____docker-entrypoint-initdb.d
|         |____sqldb1.gz
|         |____sqldb2.gz

In above example, all files inside docker-entrypoint-initdb.d from host, are shared to the docker container, and can be accessed from inside the container also at /docker-entrypoint-initdb.d . You can mount individual files also by giving exact filename. For example:

volumes:
  - ./mysql/docker-entrypoint-initdb.d/sqldb1.gz:/docker-entrypoint-initdb.d/sqldb1.gz

As I have shared a database, similarly you can share a config file and place it at the exact location you want inside the docker.

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