18

I have built my docker image using openjdk.

# config Dockerfile
FROM openjdk:8
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp

# build image
docker build -t shantanuo/dbt .

It is working as expected using this command...

docker run -p 8081:8080  -it shantanuo/dbt

Once I log-in, I have to run this command...

sh bin/startup.sh

My Question: Is it possible to add the startup command to dockerfile? I tried adding this line in my dockerfile.

CMD ["sh", "bin/startup.sh"]

But after building the image, I can not use -d parameter to start the container.

6
  • "I can not use -d parameter to start the container": why? Maybe your startup script exits too early. The container stops when the command exits. Commented Jul 28, 2017 at 10:53
  • How do I keep the command running? In other words, I do not want the container to exit after starting tomcat service. The shell script works correctly without docker. Commented Jul 28, 2017 at 11:02
  • 1
    If it's tomcat you are starting use CMD ["bin/catalina.sh", "run"] Commented Jul 28, 2017 at 11:22
  • When you run the script manually, does it run the tomcat in background and come back to the bash shell? Commented Jul 28, 2017 at 11:22
  • 1
    TarunLalwani yes, it does. @Henry yes, adding your CMD to dockerfile worked. I have not modified startup.sh file. So I guess it is OK to skip it. Right? Commented Jul 29, 2017 at 5:51

3 Answers 3

16

You can use the entrypoint to run the startup script. In the entrypoint you can specify your custom script, and then run catlina.sh.

Example:

ENTRYPOINT "bin/startup.sh && catalina.sh run"

This will run your startup script and then start your tomcat server, and it won't exit the container.

Sign up to request clarification or add additional context in comments.

2 Comments

@Henry I have added an example to demonstrate usage of entrypoint. This uses exec form of entrypoint. There is an alternate thing which he can do, which is to append the catlina.sh run command towards the end of startup script.
I have used similar thing in docker-compose.yml and I think I have correctly written equivalent syntax for dockerfile.
5

This is addressed in the documentation here: https://docs.docker.com/config/containers/multi-service_container/

If one of your processes depends on the main process, then start your helper process FIRST with a script like wait-for-it, then start the main process SECOND and remove the fg %1 line.

#!/bin/bash
  
# turn on bash's job control
set -m
  
# Start the primary process and put it in the background
./my_main_process &
  
# Start the helper process
./my_helper_process
  
# the my_helper_process might need to know how to wait on the
# primary process to start before it does its work and returns
  
  
# now we bring the primary process back into the foreground
# and leave it there
fg %1

Comments

1

A docker container must have a dedicated task. It is important that this task/startup script does not terminate. When it does, the task is done and everything for docker is done right.

It makes no sense to start a container only with the JDK. You have to put your application in it.

I think it would help when you will post what you exactly want to do.

The Docker reference is always a good place to look at: https://docs.docker.com/engine/reference/builder/#entrypoint

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.