mysql - Nginx Docker 卷为空

标签 mysql wordpress docker nginx volumes

使用 docker-compose 时,nginx 不会像其他图像那样显示文件。

以 Mysql 为例,下面的代码会将在 /var/lib/mysql 中创建的数据保存到本地机器的 ./volumes/db/data

./volumes/db/data:/var/lib/mysql

另一个例子,使用 Wordpress,下面的代码会将在 /var/www/html/wp-content/uploads 中创建的数据保存到本地机器的 ./volumes/uploads/数据

./volumes/uploads/data:/var/www/html/wp-content/uploads

尽管这不适用于 nginx,所以无论我将 /some/nginx/path 更改为什么,它都不会出现在 ./volumes/nginx/data 中/p>

./volumes/nginx/data:/some/nginx/path

nginx 在这方面有什么不同吗?


更新

使用具有以下配置的命名卷解决了这个问题:

  • 在 docker-compose 文件的 services 部分,我将 ./volumes/nginx/data:/some/nginx/path 更改为 nginx_data:/some/nginx/path

  • 然后我的 volumes 部分如下所示


volumes:
  nginx_data:
    driver: local
    driver_opts:
      o: bind
      device: ${PWD}/volumes/nginx/data

最佳答案

应该没有什么区别,一个volume就是将一个本地目录挂载到容器中的一个目录。要么你没有正确安装,要么你在 nginx 容器内安装了错误的路径(nginx 不使用的路径)。

基于 https://docs.docker.com/samples/library/nginx/ 上的官方 nginx docker 图像文档你应该挂载到/usr/share/nginx/html

$ docker run --name some-nginx -v /some/content:/usr/share/nginx/html:ro -d nginx

此外,我会在您的 docker-compose.yaml 中包含完整路径:

volumes:
  - /full_path/volumes/nginx/data:/usr/share/nginx/html

如果这仍然不起作用,您应该执行到容器中并确认目录已挂载:

$ docker exec -it <container_name> sh
$ df -h | grep nginx

# write data, confirm you see it on the docker host's directory
$ cd /usr/share/nginx/html
$ touch foo

# on docker host
$ ls /full_path/volumes/nginx/data/foo

如果其中任何一个失败,我会查看 docker 日志以查看挂载目录时是否存在问题,可能是路径或权限问题。

$ docker logs <container_name>

---更新---

我运行了你正在使用的一切,它运行良好:

$ cat Dockerfile
FROM nginx
RUN touch /usr/share/nginx/html/test1234 && ls /usr/share/nginx/html/


$ docker build -t nginx-image-test .; docker run -p 8889:80 --name some-nginx -v /full_path/test:/usr/share/nginx/html:rw -d nginx-image-test; ls ./test;
...

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
33ccbea6c1c1        nginx-image-test    "nginx -g 'daemon of…"   4 minutes ago       Up 4 minutes        0.0.0.0:8889->80/tcp   some-nginx

$ cat test/index.html
hello world

$ curl -i http://localhost:8889
HTTP/1.1 200 OK
Server: nginx/1.15.3
Date: Thu, 06 Sep 2018 15:35:43 GMT
Content-Type: text/html
Content-Length: 12
Last-Modified: Thu, 06 Sep 2018 15:31:11 GMT
Connection: keep-alive
ETag: "5b91483f-c"
Accept-Ranges: bytes

hello world

-- 更新 2 -- 太棒了,你想通了,这篇文章似乎解释了原因: docker data volume vs mounted host directory

The host directory is, by its nature, host-dependent. For this reason, you can’t mount a host directory from Dockerfile because built images should be portable. A host directory wouldn’t be available on all potential hosts.

If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it’s best to create a named Data Volume Container, and then to mount the data from it.

关于mysql - Nginx Docker 卷为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52196078/

相关文章:

Wordpress:如何对自定义字段上的帖子进行排序

javascript - 无法在 Windows 10 中执行脚本 docker-compose YML 文件

mysql - 根据条件将行从一个表迁移到另一个表 [MYSQL]

php - 从 php 到 mysql 的日期字段返回错误 0000-00-00

jquery - 图像库/ slider ,仅显示一个缩略图,但单击时显示一个库?

docker - 耗尽的虚拟内存使用Docker Toolbox安装SyntaxNet

docker - 在 Docker-Compose 和 GitLab-CI 之间共享环境变量

mysql - 从给定的SQL表中提供随机元素的服务是否可行?

php - 同一张表上的多个计数(Laravel)

html - 如何调用 <li> 标签 CSS