本文介绍了docker-compose volume_from与版本3等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用docker compose创建Nginx / PHP FPM安装程序,并且版本3卷的语法/更改有问题。

I'm trying to create an Nginx/PHP FPM setup with docker compose and am having issues with the version 3 volumes syntax/changes.

我的 Dockerfile

FROM php:7-fpm
VOLUME /var/www/html

我的 docker-compose.yml

version: "3"
services:
  php:
    build: .
    volumes:
      - ./html:/var/www/html
  web:
    image: nginx
    links:
      - php
    ports:
      - "8888:80"
    volumes:
      - php:/var/www/html
      - ./default.conf:/etc/nginx/conf.d/default.conf
volumes:
  php:

当我添加 index.php 文件放入 ./ html ,我可以通过转到,但是任何静态文件(如CSS)都返回404,因为Nginx在其容器中找不到这些文件( / var / www / html 在nginx容器上为空)。在版本3中,泊坞窗撰写文件不再具有 volumes_from ,这基本上就是我要复制的内容。

When I add an index.php file into ./html, I can view that by going to http://localhost:8888, but any static files (like CSS) return a 404 because Nginx cannot find those in its container (/var/www/html is empty on the nginx container). With version 3 docker compose files do not have volumes_from anymore, which is basically what I'm trying to replicate.

如何使它与版本3兼容?

How can I get this to work with version 3?

推荐答案

用于使用命名卷在所需的容器之间共享文件在yml文件的顶层定义

For using "Named volumes" for sharing files between containers you need to define

1) volumes:部分并定义卷名

volumes:
  php:

2)像您一样在第一个容器上定义 volume 节(共享将装入的位置)

2) define volume section on first container like you did (Where share will mount)

web:
    volumes:
      - php:/var/www/html #<container_name>:<mount_point>

3)在第二个容器上定义 volume 部分(共享将从中挂载)

3) define volume section on second container (Share will mount from)

php:
  volumes:
    - php:/var/www/html

4)(可选)如果需要将卷数据存储在您可以使用 Docker插件的主机。您可以指定Docker卷驱动程序和数据存储的路径。

4) (optionally) If you need to store volume data on the host machine you can use local-persist docker plugin. You can specify docker volume driver and path where you data will be stored.

volumes:
  php:
    driver: local-persist
    driver_opts:
      mountpoint: /path/on/host/machine/

在您的情况下,您忘记为 php 容器定义卷名。只需替换

In your case you forgot define volume name for php container. Just replace

  php:
    build: .
    volumes:
      - ./html:/var/www/html
  php:
    build: .
    volumes:
      - php:/var/www/html

并使用本地Persist Docker插件

and use Local Persist Docker Plugin

这篇关于docker-compose volume_from与版本3等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 03:31