Home > other >  Docker run commands works but dockerfile not
Docker run commands works but dockerfile not

Time:01-20

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:

localhost didn’t send any data.

What I am doing is simple. By 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:

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!

Does anybody know the problem here?

CodePudding user response:

When your Dockerfile has a CMD command, that runs instead of the command in the base image. With the tomcat image, the base image would run the Tomcat server; but with this Dockerfile, it's trying to run a bash shell instead, and without any input that just exits immediately.

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. For this setup where the base image already has a reasonable CMD, you don't need to repeat it in your own custom 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. In addition to tomcat, nginx and php:fpm work similarly; so long as their configuration files and code are in the right place, you don't need to repeat the CMD.

  • Related