这篇文章上次修改于 2266 天前,可能其部分内容已经发生变化,如有疑问可询问作者。 随着docker-compose版本3及以上版本的出现,volumes_from指令即将被移除。对于本地开发来说挂载本地目录到容器是一个很重要的需求,那么,现在你可以使用host bind方式挂载目录或定义存储卷 ``` web_app_cli: image: code4hire/dev-images:php-7.2-cli hostname: "web_app_cli" working_dir: /application volumes: - ./web-app/application:/application ``` 主机相对路径./web-app/application安装到容器/application。看起来没什么问题,但假设同一个目录要在多个容器间共享呢? ``` web_app_cli: image: code4hire/dev-images:php-7.2-cli hostname: "web_app_cli" working_dir: /application volumes: - ./web/application:/application php-fpm: image: code4hire/dev-images:php-7.2-fpm volumes: - ./web/application:/application nginx: image: nginx:latest ports: - "8080:8080" volumes: - ./web/application:/application ``` 有洁癖的人不能忍 可以通过使用环境变量替换来改进 ``` web_app_cli: image: code4hire/dev-images:php-7.2-cli hostname: "web_app_cli" working_dir: ${WEB_DESTINATION_PATH} volumes: - ${WEB_APP_PATH}:${WEB_DESTINATION_PATH} php-fpm: image: code4hire/dev-images:php-7.2-fpm volumes: - ${WEB_APP_PATH}:${WEB_DESTINATION_PATH} nginx: image: nginx:latest ports: - "8080:8080" volumes: - ${WEB_APP_PATH}:${WEB_DESTINATION_PATH} ``` 并.env在docker撰写文件所在的同一级别创建一个文件 ``` # web specific mounts and settings # Application's path (absolute or relative) # **IMPORTANT** # If you are using docker-sync, the path is relative to the location of this file # if you are NOT using docker-sync, and just docker-compose, # the path is relative to the location of docker-compose.yml WEB_APP_PATH=./web/application # Path inside of container where the application will be mounted, # This var can also be used as workdir value for docker # MAKE SURE THE VALUE IS IN SYNC WITH YOUR NGINX OR APACHE CONFIG WEB_DESTINATION_PATH=/application ``` 处理.env和docker 时需要注意的一件重要事情是Docker处理键和值的方式,即没有特殊的引号处理。这意味着它们是VAL的一部分。 最佳实践当然是在docker-compose文件里直接定义啦: ``` ... volumes: web_app: driver: local driver_opts: type: none o: bind device: $PWD/web/application ```
没有评论