docker - 使用dockerfile运行mkdir命令

标签 docker docker-compose dockerfile

我无法在带有dockerfile的容器中使用mkdir命令创建目录。

我的Dockerfile文件很简单;

FROM php:fpm

WORKDIR /var/www/html

VOLUME ./code:/var/www/html

RUN mkdir -p /var/www/html/foo

通过这种方式,我创建了一个简单的php:fpm容器。
我写了一个名为foo的目录。
docker build -t phpx .

我已经用上面的代码构建了。

在我的docker-compose文件中,如下所示。
version: '3'

services:
web:
container_name: phpx
build : .
ports:
- "80:80"
volumes:
- ./code:/var/www/html

后来;运行以下代码,然后我进入了容器内核。
docker exec -it phpx /bin/bash

,但/ var / www / html中没有名为foo的目录。

我想知道我在哪里做错了。
你能帮助我吗?

最佳答案

原因是您正在将主机中的卷装载到/var/www/html
一步步:

  • RUN mkdir -p /var/www/html/foo在容器的文件系统内创建foo目录。
  • docker-compose.yml ./code:/var/www/html“隐藏”容器文件系统中/var/www/html的内容在主机文件系统上./code内容的后面。

  • 因此,实际上,当您执行到容器中时,当您查看./code时,您会在主机上看到/var/www/html目录的内容。

    修复:可以从docker-compose.yml中删除卷,或者在启动容器之前在主机上创建foo-目录。

    附加说明:在Dockerfile中,将卷声明为VOLUME ./code:/var/www/html。这不起作用,您可能应该将其删除。在Dockerfile中,您无法在主机上指定路径。

    引用docker:

    The host directory is declared at container run-time: The host directory (the mountpoint) is, by its nature, host-dependent. This is to preserve image portability. since a given host directory can’t be guaranteed to be available on all hosts. For this reason, you can’t mount a host directory from within the Dockerfile. The VOLUME instruction does not support specifying a host-dir parameter. You must specify the mountpoint when you create or run the container.

    关于docker - 使用dockerfile运行mkdir命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52754149/

    相关文章:

    docker - 将 --security-opt seccomp :filename. json 传递给 Docker 群中的容器

    php - Docker - 如何在 docker-compose.yml 中设置 Apache + PHP

    bash - docker:如何在dockerfile中设置端口

    docker - Docker “docker-entrypoint-initdb.d”脚本未执行

    reactjs - react 脚本 : not found when building docker image in CI, 但在本地工作

    docker - ENTRYPOINT之后docker交互模式退出

    docker - 将 docker 卷导出到另一台机器

    docker - docker build 会运行两次命令吗?

    docker - 如何将点火数据安装到主机系统

    Docker ADD不会提取fi