RUN, ENTRYPOINT, SHELL, CMD have their own purpose. The fact that you can use a script instead of directly write the commands in the Dockerfile will not change that. I would not start to explain each of those, because it is in the documentation and I also have a tutorial, although I really feel I should finally make an English video as well, because it is just a sourcecode until that:
Your first code, the Dockerfile is using only RUN instructions so nothing will actually run in the container. Your start script for example will run as the last step of the build process and stop. You need CMD to tell Docker what you want to start when the container starts.
The other problem is that you try to use Docker containers as virtual machines. It could work in an LXC container but not really in a Docker container or not as easily. In a Docker container you shouldn’t install Apache HTTPD from the APT repository and start as a systemd service. Systemd requires special configuration to work in a container anyway. There are base images containing HTTPD already or you can build it from sourcecode.
Running each install command in a new RUN instruction is not a really good idea either and I would definitely not run apt-eget upgrade during Docker build. I would not start to explain (sorry) in details, but you can probably find debates about this online or even here on the forum. Even if you use apt-get upgradethe way you do will result in a a never changing layer so it will upgrade only once until you clear the build cache. So if I really have to use upgrade, I would create my own base image which has a Dockerfile with only the upgrade command and release the result with new tags. This way I wouldn’t have to use the build cache at all to build that image, but I could for the images that uses it. Also because you have the apt-get update command only before the upgrade, that layer, if you add a new install line to the Dockerfile after that, you will install an older version synce apt cache will never be updated. Which can also cause that you can’t even install anything because the old packages are not available anymore or not compatible with something.
You should also use apt-get
for installing as you did for updating and upgrading. I don’t understand it either, but even though apt has been there for a long time now, it still thorws a warning when you are not running it interactively, telling you that it can change and apt-get
is more stable.
Your second shared code in your last post looks like comments and only one actually running command, which is just an echo. Am I correct? That would of course run but do nothing and you won’t see it when you run docker build because buildkit is the default builder now and you get only error messages. Normal outputs will appear for a fraction of a second and disappear.
The last line is a CMD after the hashmark (comment character). That would not work of course because CMD is not a shell command. Somehow I feel I have seen the same mistake somewhere in another question.
So again, if you want to create images, you need to learn each instruction step by step. If you understand what those are doing exactly, you don’t need to experiment with longer scripts, because you know what you need to do. Unfortunately you will need to learn how to build softwares from source code so you can install a specific version of that software and when you change the version number in the Dockerfile, that invalidates the cache and Docker rebuilds that layer.