Docker build with having below content does not execute

I have dockerfile wherein I have referenced whose contents are as below. When I try to build the image it successfully references the .sh file but after running “echo Hello docker” it does nothing and image building goes on. I have to stop by CTRL-C

Kindly advise as to what I am doing wrong as my image does not get totally build

service apache2 start
curl localhost
echo Hello docker
ping -n 4
/bin/bash -c 'source $HOME/.bashrc && echo $HOME'
CMD ["/bin/bash", "-c", "echo 'Hello World!'; echo 'Goodbye World!'"]

I don’t know where to start. I fixed your post to use a code block. You tried to use quotes which is not good for codes and you have to select the text before you click on the formatting buttons. Code block (</>) is right next to the quote button.

The code you shared is not a valid Dockerfile content at all. Is this what you really used? Only the last line looks like a Dockerfile instruction which will just welcome you when you start the container.

The problem with the build could be caused by the invalid Dockerfile, but that should just fail immediately. However I experienced something similar today with a correct Dockerfile and I couldn’t explain it yet. That build failed too but when it failed it didn’t stop and CTRL+C didn’t work either. In my case I used Docker Compose to build the image automatically but I had the docker compose process running even after shutting down Docker Desktop.

Please, share how you run the build command and on which version of which platform you are working.

I have Windows Machine with VMWare Workstation 17 Pro. On that I have installed CentOS
I am just experimenting with different options to clear my doubts while learning.

Below is dockerfile

# Shell Vs Exec Form of RUN & CMD

FROM ubuntu:latest AS UBUNTU
#In the shell form you can use a \ (backslash) to continue a single RUN inst>
RUN apt-get update -y && \
apt-get upgrade -y
RUN apt install apache2 -y
RUN apt install iputils-ping -y
RUN apt install systemd -y
RUN apt install curl -y
RUN mkdir -p /Scripts
COPY /Scripts
#ADD /
WORKDIR /Scripts
RUN chmod +x
RUN /Scripts/

Since initially I was trying to use those SHELL Vs EXEC form of commands for CMD / RUN but were not working so I go reference from internet that instead of directly using those forms, one can create .sh file and put those linux commands directly in that file and run that file while dockerfile is build.
So every command is pure linux based rather then SHELL or EXEC form which I think should run.

#echo $PATH
#export PATH=$PATH:/usr/bin/systemctl
#service apache2 start
#curl localhost
echo Hello docker
#ping -n 4
#/bin/bash -c 'source $HOME/.bashrc && echo $HOME'
#CMD ["/bin/bash", "-c", "echo 'Hello World!'; echo 'Goodbye World!'"]

Below is the docker build command used

docker build -f dockerfile-2 .

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.