2

New to dockers, so please bear with me.

My Dockerfile contains an ENTRYPOINT:

ENV MONGOD_START "mongod --fork --logpath /var/log/mongodb.log --logappend --smallfiles"
ENTRYPOINT ["/bin/sh", "-c", "$MONGOD_START"]

I have a shell script add an entry to database through python script, and starts the server.

The script startApp.sh

chmod +x /addAddress.py
python /addAddress.py $1
cd /myapp/webapp 
grunt serve --force

Now, all the below RUN commands are unsuccessful in executing this script.

sudo docker run -it --privileged myApp -C /bin/bash && /myApp/webapp/startApp.sh loc

sudo docker run -it --privileged myApp /myApp/webapp/startApp.sh loc

The docker log of container is

"about to fork child process, waiting until server is ready for connections. forked process: 7 child process started successfully, parent exiting "

Also, the startApp.sh executes fine when I open a bash prompt in docker and run it.

I am unable to figure out what wrong I am doing, help please.

5
  • What's the output of docker logs <container> ? Have you tried launching the container with just bash instead of startApp.sh and then executing startApp.sh from your bash prompt? Commented Mar 8, 2016 at 20:14
  • sudo docker ps doesn't show this as a running container. So can't fetch the logs. When trying to launch a bash and executing startApp.sh it runs fine and gives me the expected output. Commented Mar 8, 2016 at 20:15
  • Okay, the container which exited without any further response has the following log - "about to fork child process, waiting until server is ready for connections. forked process: 7 child process started successfully, parent exiting " Commented Mar 8, 2016 at 20:17
  • Please don't post additional info in comments; add this info to the question by edit ing it instead. Commented Mar 8, 2016 at 20:24
  • Thank you for pointing that out Frank, I have updated the question. Commented Mar 8, 2016 at 20:27

2 Answers 2

2

I would suggest you to create an entrypoint.sh file:

#!/bin/sh

# Initialize start DB command
# Pick from env variable MONGOD_START if it exists
# else use the default value provided in quotes
START_DB=${MONGOD_START:-"mongod --fork --logpath /var/log/mongodb.log --logappend --smallfiles"}

# This will start your DB in background
${START_DB} &

# Go to startApp directory and execute commands
`chmod +x /addAddress.py;python /addAddress.py $1; \
               cd /myapp/webapp ;grunt serve --force`

Then modify your Dockerfile by removing the last line and replacing it with following 3 lines:

COPY entrypoint.sh /

RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

Then rebuild your container image using

docker build -t NAME:TAG .

Now you run following command to verify if ENTRYPOINT is /entrypoint.sh

docker inspect NAME:TAG | less
Sign up to request clarification or add additional context in comments.

Comments

0

I guess (and I might be wrong, since I'm neither a MongoDB nor a Docker expert) that your combination of mongod --fork and /bin/sh -c is the culprit.

What you're essentially executing is this:

/bin/sh -c mongod --fork ...

which

  • executes a shell
  • this shell executes a single command and waits for it to finish
  • this command launches MongoDB in daemon mode
  • MongoDB forks and immediately exits

The easiest fix is probably to just use

 CMD ["mongod"]

like the official MongoDB Docker does.

2 Comments

Frank, I need the --fork because while building the docker. There are certain initial data that I need to populate into MongoDB. It has to run in a separate shell.
I tried your approach too, without using fork. It doesn't even complete execution of the mongod and thus never reaches the script.

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.