5

I have a spring-boot app which talks to couchbase. I build the spring app as a docker image. There are some preconditions that need to be fulfilled in couchbase set up in order for the app to run. When I run my couchbase image first and then run my spring-boot app image everything runs fine. However, I need this to be automated and run from a docker-compose file meaning by a single docker-compose up command I should be able to run the couchbase image first, configure it with all presettings and then start to run the spring-boot app. I ran into quite a few discussion threads, but unfortunately I am not able to make it work somehow. I tried using cmd and entrypoint, but without success. Here is my docker-compose file

version: "2"
services:
  expensetracker-cb:
    image: chakrar27/expensetracker-cb
    command: sh test_hello.sh
    ports:
      - 8080:8080
    depends_on:
      - mycouchbase

  mycouchbase:
    image: chakrar27/couchbase_new_10_08_2016
    ports:
      - 8091:8091
      - 8092:8092 
      - 8093:8093 
      - 8094:8094
      - 11210:11210

In fact it doesn't trigger the test_hello.sh at all. Here's the dockerfile for the spring-boot expensetracker app

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD expensetracker-cb-0.1.0.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Can someone please help?

stmoebius
  • 662
  • 11
  • 30
chakrar
  • 189
  • 2
  • 10
  • 1
    This may help https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y – Windsooon Oct 11 '16 at 02:20

2 Answers2

1

Ok...I could get it working by including the script in the Dockerfile of the app container. Not the best solution because I feel waiting code should not be part of the container itself. Also, I need to find a way to wait for the couchbase cluster to be up and running with sample buckets and include that in the script or the couchbase container itself. For now though this work-around works for me. Here's the dockerfile contents

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD expensetracker-cb-0.1.0.jar app.jar
RUN sh -c 'touch /app.jar'
ADD test_hello.sh .
RUN chmod +x test_hello.sh
CMD sh test_hello.sh
chakrar
  • 189
  • 2
  • 10
0

Yes.

First in your docker-compose.yml use entrypoint instead of command. Due to your entry point calls java.

Second include your script in the container:

version: "2"
services:
  expensetracker-cb:
    image: chakrar27/expensetracker-cb
    entrypoint: sh /mnt/test_hello.sh
    ports:
      - 8080:8080
    depends_on:
      - mycouchbase
    volumes:
      - ./test_hello.sh:/mnt/test_hello.sh


  mycouchbase:
    image: chakrar27/couchbase_new_10_08_2016
    ports:
      - 8091:8091
      - 8092:8092 
      - 8093:8093 
      - 8094:8094
      - 11210:11210

Example test_hello.sh

#! /bin/bash

echo "Put your waiting code here, I will wait for 1 min"
sleep 60


java -Djava.security.egd=file:/dev/./urandom -jar /app.jar

I had the same problem with Oracle and my last resort was trying to execute a SQL until it succeed. I think with couch base you can do something similar.

Carlos Rafael Ramirez
  • 5,984
  • 1
  • 29
  • 36
  • I get this error when trying to run thru entrypoint ERROR: for expensetracker-cb Cannot start service expensetracker-cb: oci runtime error: exec: "./test_hello.sh": stat ./test_hello.sh: no such file or directory. The script file exists in the same directory as the compose.yml. I changed the docker-compose to this entrypoint: ./test_hello.sh – chakrar Oct 11 '16 at 11:55
  • Ok the first thing you need to do is to include your script in your container. Via modifying the dockerfile or mounting a volume. I've updated the answer – Carlos Rafael Ramirez Oct 11 '16 at 12:18
  • Ok...with your changes the script executed. However I have a new issue, looks like my app container did not run. I get this error: "expensetrackercb_expensetracker-cb_1 exited with code 0". I found a similar issue here https://github.com/docker/compose/issues/3140. When I do docker inspect I see this: – chakrar Oct 11 '16 at 14:00
  • I saw your container running and I don't know how you are specifying the IP address of the database. As you are using docker-compose you can use the service name, just check you /etc/hosts in your container to see the name. – Carlos Rafael Ramirez Oct 11 '16 at 14:03
  • And if my answer solved your issue, please drop the negative point and check it as the right answer. – Carlos Rafael Ramirez Oct 11 '16 at 14:03
  • with your changes the script got executed. However, my app container did not run, it exited with error "expensetrackercb_expensetracker-cb_1 exited with code 0". I found a similar issue mentioned here -> https://github.com/docker/compose/issues/3140 Not sure if docker-compose supports something like "wait" by default. I am using docker 1.12. – chakrar Oct 11 '16 at 14:10
  • Yes in fact it wipes the entrypoint and the command. Just put in the script the complete command you need to be run. Like I did in the example. No but if you put the database first in the file it will try to run it first, but you still need to wait for the database – Carlos Rafael Ramirez Oct 11 '16 at 14:18
  • I tried putting the command to run the app in the script. It didn't work. I also tried by putting both the command and entrypoint in the compose. Got the same error. – chakrar Oct 11 '16 at 14:50
  • Paste the script to see what you are doing. Do everything in the script and leave the docker-compose.yml with only the entrypoint. Remember in the script you are inside the container – Carlos Rafael Ramirez Oct 11 '16 at 14:54
  • Here's the script: pretty much the same as your example. !/bin/bash echo "Starting script ..........." echo "Put your waiting code here, I will wait for 1 min" sleep 60 java -Djava.security.egd=file:/dev/./urandom -jar /app.jar – chakrar Oct 11 '16 at 15:14
  • Try to pass the database ip address to your application, I don't know how, using the script – Carlos Rafael Ramirez Oct 11 '16 at 15:17