也许我不太了解 Docker,但我不明白制作 Dockerfile 供我使用的意义。 在 compose 中,我几乎可以指定我需要在 dockerfile 中设置的所有内容,除了两点:
1) 我没有在 docker-compose.yml 中找到 COPY/ADD 的等价物,所以我使用的是目录,但它是从主机制作图像。复制 和 添加 和有什么不一样?是否可以从 docker-compose 文件中实现它?
2) 我没有找到 RUN 的等价物。我正在使用节点脚本,所以我需要像在 dockerfile 中使用 RUN 一样进行 NPM 安装。我发现的方法是在我的 package.json 文件的启动命令中设置“npm install && node server.js”。有更好的方法吗?也许进入指挥领域?
我的英语不是很好,所以不要犹豫,要求精确。提前感谢您的回答。
最佳答案
1) Here are the official docs between COPY and ADD :
Although ADD and COPY are functionally similar, generally speaking, COPY is preferred. That’s because it’s more transparent than ADD. COPY only supports the basic copying of local files into the container, while ADD has some features (like local-only tar extraction and remote URL support) that are not immediately obvious. Consequently, the best use for ADD is local tar file auto-extraction into the image, as in ADD rootfs.tar.xz /.
您不能在 docker-compose.yml 中执行此操作,如果您想向镜像添加文件,则必须使用 Dockerfile。
2) docker-compose.yml 具有入口点和命令覆盖。与在运行时使用 docker 传递 CMD
类似,您可以使用 docker-compose run
docker run -itd mystuff/myimage:latest bash -c 'npm install && node server.js'
你可以使用 docker-compose
来完成,假设这里的服务名称是 myservice:
docker-compose run myservice bash -c 'npm install && node server.js'
如果您的用例只运行一个容器,作为初学者可能很难看到 docker-compose 的好处。如果您要将 mongodb 和 nginx 容器添加到您的开发堆栈中,您将开始看到 docker-compose 真正 的作用并为您带来好处。当用于编排需要运行和相互通信的多个容器时,它的效果最好。
关于docker - 我们可以用 docker-compose.yml 替换 dockerfile 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53085046/