Docker Community Forums

Share and learn in the Docker community.

How to Start a Process within a Container via Dockerfile?

Greetings!

[ My Desire ]

Have a process start running when a Container is built/started.

[ Troubles ]

The command to start the process, when added into the Dockerfile, causes the Container to not fail after the process initiates.

[ Attempts/Process ]

I have built a successfully running Dockerfile/Container, up to the point of executing the process that I want to start, and have tried a variety of approaches to get this to function properly, such as:

  • Using CMD
  • Using ENTRYPOINT
  • Using RUN
  • Creating a script, and using the previous to execute the script when starting
  • Adding in Cron to execute the command and/or a script when starting
  • Adding in Cron to execute the command and/or a script some time after the Container has been built and running

None of those have been successful, and to help round this out, after getting the Container running, I can enter the CLI and execute the command and it the process runs without any issue (also, I haven’t been successful in getting Cron to function either).

I can also call the script that contains the command, and runs with no issues from the CLI.

It seems to have an issue running the command/process during the Container startup if the command is within the Dockerfile as a line to execute.

[ Sample Dockerfile for Reference ]

Within this file, there are multiple attempts listed, and many that have been removed, and various combinations. The “/etc/init.d/process start” is how I normally start the process, and that command works just fine within the CLI of the container, but, as I mentioned earlier, it causes the Container to fail when trying to start it from the Dockerfile.

FROM ubuntu:20.10

RUN apt update && apt install openssh-server sudo openvpn ser2net network-manager cron nano -y
RUN mkdir /etc/new-dir
RUN mkdir /etc/new-dir/sub-dir

COPY local-dir/certs/* /etc/new-dir/sub-dir
COPY local-dir/config_dir/* /etc/new-dir
COPY local-dir/directory /usr/bin/directory

ADD local-dir/crontab.txt /var/spool/cron/crontabs/root

RUN chmod 0644 /var/spool/cron/crontabs/root
RUN touch /var/log/cron.log

CMD cron && tail -f /var/log/cron.log

#COPY docker-config/processStartup.sh /etc/new-dir

#ENTRYPOINT ["/etc/init.d/S01process start"]
#CMD “/etc/new-dir/processStartup.sh”

#RUN crontab /crontab.txt

#RUN /processStartup.sh

#CMD ["/etc/init.d/process", “start”]

#CMD ["/etc/init.d/process start"]

1 Like

By default,main running process for Docker is the ENTRYPOINT and/or CMD at the end of the Dockerfile . Per Docker guidelines, it is recommended that you separate areas of concern by using one service per container but there could be scenarios where you would have to group services and run it in single container.

I read over Docker guidelines (though I am sure I could reread it again) that, and have tried ENTRYPOINT (it is in the Dockerfile), and it causes the container to fail from starting up.

Clearly you need to debug the reason for the failure. Assuming there is nothing in the container logs, you could just run up a container from your image but override the ENTRYPOINT and/or CMD to start a simple shell. Then inside the container manually run the commands you are trying to use so that you can see what the behaviour is and take it from there. Something like …

docker run -it —rm your-image:tag /bin/sh

Or

docker run -d —name test —entrypoint sleep your-image:tag infinity
docker exec -it test /bin/sh

I realize now that some of my wording in the initial post is confusing.

I need to build a testing environment to test/experiment with a program that has been developed. This program runs just fine in every linux environment it has been placed in.

I created a container to run said program.

I loaded everything into the container that the program needs to run.

I can start the container without any issues.

I can open up a CLI within the container and start the program via the following command:

“/etc/init.d/S01process start”

The program and container both run fine.

If fact, I can even get it to call out to the internet and whatnot. There doesn’t appear to be any issues.

I want the program to start when the container is spin up, so I add that command into the Dockerfile.

When I spin up the container, the program begins to initialize, because the program outputs some text telling use when it starts up and shuts down, and the start up text appears.

Right after the startup text appears, the container fails/crashes/etc, without any evidence as to why.

This pattern repeats every time I attempt to spin up the contain while that command is in the Dockerfile.

@goffinf

I followed your directions, and nothing different happened, other than the Container started as if I didn’t have

ENTRYPOINT /etc/init.d/S01gateway start

in the Dockerfile.

If this helps at all, this is the only error/log output that I have been able to find/capture with regards to the Container failing:

(HTTP code 500) server error - OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "/etc/init.d/S01gateway start": stat /etc/init.d/S01gateway start: no such file or directory: unknown

My only thought to this, is that the Container is attempting to run this before it initializes whatever is needed within the Container to start the program.