7

I am using dockerfiles to build a simple container. Here is the Dockerfile:

FROM XXXXXXX:5003/base-java

MAINTAINER XXXXX

ADD pubsub/ /opt/pubsub/

CMD ["/opt/pubsub/run.sh"]

Content of run.sh is as follows:

#!/bin/bash
nohup java -jar /opt/pubsub/publish.jar &
nohup java -jar /opt/pubsub/subscribe.jar &

This is simple java application for pub/sub.

Now I have got another container running rabbitmq and I am linking the 2 containers however each of my attempt has just failed and My pub/sub container does not start. Can someone advice how to go about debuggin this issue? Somehow docker logs does not have anything.

Here is how I am linking the containers: sudo docker run -d -P --name pub_sub --link rabbitmq:rabbitmq1 image_pub_sub

And here is how I am using the alias name in my pub/sub code

factory = new ConnectionFactory(); 
factory.setHost("rabbitmq1"); 
try { connection = factory.newConnection(); 
channel = connection.createChannel(); 
channel.queueDeclare("pub", true, false, false, null); 
} 
catch (IOException e) { // TODO Auto-generated catch block 
e.printStackTrace(); }

I was expecting that my publish code will create a queue in the rabbitmq container and start pushing messages. My subscriber code will basically connect to the same rabbitmq and start reading the messages.

When I run the command nothing happens it just prints a long id of the new container and exits..When I run sudo docker ps -a, I can see the following:

e8a50d5aefa5     image_pub_sub:latest     "/opt/pubsub/run.sh"     32 minutes ago     Exited (0) 32 minutes ago     pub_sub

So this means my container is not running.

Just now I tested by updating the /etc/hosts by launching a new container using the following command: sudo docker run -i -t image_pub_sub /bin/bash. Modified the /etc/hosts of this new container and added the following entry <IP_ADDRESS> rabbitmq1 and ran my script /opt/pubsub/run.sh and it appends the nohup file with the following messages:

Message Sent
 [x] Received 'Hello'
Message Sent
Message Sent
 [x] Received 'Hello'
4
  • You need to provide more information; what commands did you use to link the containers, what was the result and what did you expect? Commented Feb 19, 2015 at 14:08
  • Please add this to the question, rather than as a comment. And let us know what does happens when you run the commands (is the container still running? How do you know it isn't working?). Also, get a shell in the container and test connecting to the rabbitmq container. Commented Feb 19, 2015 at 14:12
  • Thanks. At the moment it's not clear if you have a networking problem or code/rabbitmq problem. What happens if you run docker exec -it pub_sub ping rabbitmq1? Commented Feb 19, 2015 at 14:21
  • Added all the information you asked, please let me know what to do to nail down this issue. Commented Feb 19, 2015 at 14:27

2 Answers 2

7

A Docker container will stop when its main process completes. In your case, this means the two Java applications will be forked to the background (because of the nohup call) then the script will immediately complete and the container will exit.

There are a few solutions:

  • The quickest and easiest solution is to just remove nohup call from the second java call. That way the script won't exit until the second Java application exits.
  • Use a process manager such as runit or supervisord to manage the processes.
  • Put the jars in separate containers and call Java directly (this would seem to be the best solution to me).
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Adrian, Thanks a lot for helping me with first ettiquetes while making posts in this forum. Secondly a super thanks for providing so many ways of solving this issue. Here is my feedback on all of these. 1) First one was a quick one, I just rebuilt my container with the necessary changes and it worked :)
2) Not tried yet but here is my understanding please clarify if this is correct. Use the supervisor.conf and append it with both the java processes. Now just start supervisord process using the dockerfile CMD ["/usr/bin/supervisord"]. Since this is a never ending process that is what I suppose. 3) Adding jars in 2 separate containers wouldnt it require to build 2 separate containers. Each of my custom build containers with java on it is taking 730 MB of space. Just wanted to know why you consider this as the best option.
@user1507003 2) Yes, I think that's about it 3) If you use the same base image for both of the containers, then there won't be any extra space used. It's more idiomatic to have 1 process per container & it would also seem to fit well with your distributed code.
Adrian - Somehow I do not get it why extra space will not be used. Here is why I think the space would be doubled. I build a container-1 with publish code and than build container-2 with subscribe code. Container-1 and 2 are using the same base-image. Now I deploy container-1 and 2 on my host machine. U see there are 2 different containers. These will twice the space isnt??
No. Same image, same files. Two JVMs though, which may or not be an issue.
|
0

can use tail -f /dev/null or tail your log

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.