我无法在带有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目录。 ./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/