简体   繁体   English

Docker 运行命令有效,但 dockerfile 无效

[英]Docker run commands works but dockerfile not

I have a problem with running the image of Dockerfile. CLI commands work fine but when I use the Dockerfile I get an error from the localhost:我在运行 Dockerfile 的图像时遇到问题。CLI 命令工作正常,但是当我使用 Dockerfile 时,我从本地主机收到错误消息:

localhost didn't send any data.本地主机没有发送任何数据。

What I am doing is simple.我正在做的很简单。 By CLI:通过 CLI:

docker run -d --name mytomcat -p 8080:8080 tomcat:latest

docker exec -it mytomcat /bin/bash

mv webapps webapps2

mv webapps.dist/ webapps

exit

Which works fine.哪个工作正常。

My Dockerfile:我的 Dockerfile:

FROM tomcat:latest
CMD mv webapps webapps2 && mv webapps.dist/ webapps && /bin/bash

Build and run:构建并运行:

docker build -t myrepo/tomacat:1.00  .  
docker run -d --name mytomcat -p 8080:8080 myrepo/tomacat:1.00

Doesn't work and show the above error.不起作用并显示上述错误。

Note: I am using mv command because I get 404 error!注意:我正在使用 mv 命令,因为我收到 404 错误!

Does anybody know the problem here?有人知道这里的问题吗?

When your Dockerfile has a CMD command, that runs instead of the command in the base image.当您的 Dockerfile 有一个CMD命令时,它会代替基本映像中的命令运行。 With the tomcat image, the base image would run the Tomcat server;使用tomcat镜像,基础镜像将运行 Tomcat 服务器; but with this Dockerfile, it's trying to run a bash shell instead, and without any input that just exits immediately.但是对于这个 Dockerfile,它试图运行bash shell,并且没有任何立即退出的输入。

To just moves files around, it's usually better to use COPY and RUN directives to set up the image once, rather than trying to repeat these steps every time you run the container.要只是移动文件,通常最好使用COPYRUN指令设置一次图像,而不是每次运行容器时都尝试重复这些步骤。 For this setup where the base image already has a reasonable CMD , you don't need to repeat it in your own custom Dockerfile.对于基本图像已经具有合理CMD的此设置,您无需在您自己的自定义 Dockerfile 中重复它。

FROM tomcat:latest
RUN mv webapps webapps2 && mv webapps.dist/ webapps
# no particular mention of bash; use the `CMD` from the base image

It's not uncommon for a base image to include some sort of runtime that needs to be configured, but for the base image's CMD to still be correct.基本映像包含某种需要配置的运行时并不少见,但基本映像的CMD仍然是正确的。 In addition to tomcat , nginx and php:fpm work similarly;除了tomcatnginxphp:fpm的工作方式类似; so long as their configuration files and code are in the right place, you don't need to repeat the CMD .只要他们的配置文件和代码在正确的地方,你就不需要重复CMD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM