7

I am working on controlling the startup order of container as my docker-compose.yml has total 5 containers and they need to be started in an order.

I have gone through many of the suggestions on the internet regarding controlling the startup orders.

The issue is the

command: ["./wait-for-it.sh","rabbitmq:15672"]

is not getting executed inside Docker container when container starts.

Here is a detailed description:

  1. I have a Spring Boot project which has Dockerfile given below.

    FROM openjdk:8-jre-alpine
    COPY ./target/spring-cloud-config-server-0.0.1-SNAPSHOT.jar /usr/source/
    COPY wait-for-it.sh /usr/source/
    RUN chmod 777 /usr/source/wait-for-it.sh
    RUN apk add --no-cache bash
    WORKDIR /usr/source/
    EXPOSE 8080
    ENTRYPOINT ["java", "-jar", "spring-cloud-config-server-0.0.1-SNAPSHOT.jar"]
    

2.I am using wait-for-it.sh bash script which is placed in the same root directly where my Dockerfile is present. 3. In the same root directory, I have docker-compose.yml as given below

version: "3.3"
services:
 rabbitmq:
  image: rabbitmq:management
  ports:
  - "15672:15672"
  - "5672:5672"
  volumes: 
- /root/rabbitmq_data:/var/lib/rabbitmq
 spring-cloud-config-server: 
    image: rajeevshukla/spring-cloud-config-server
    container_name: spring_cloud_config_server
    depends_on: 
     - rabbitmq
    links: 
     - rabbitmq 
    command: ["./wait-for-it.sh","rabbitmq:15672"]
    ports: 
      - "8888:8080"
    environment:
      - "SPRING_PROFILES_ACTIVE=prod"

Now when I build Docker image and start it. I see wait-for-it.sh is present inside Docker container in /usr/source/ directory where my jar file is present.

When I run

docker-compose up

I am expecting that once Rabbitmq server is up then spring-cloud-config-server should start. But it is not actually happening. Even I run any command in command: it does not get executed. I have tried all the possible cases but no luck.

As per my knowledge command: ["./wait-for-it.sh","rabbitmq:15672"] gets executed when the container starts. So does it execute before ENTRYPOINT ["java", "-jar", "spring-cloud-config-server-0.0.1-SNAPSHOT.jar"] ?

halfer
  • 19,824
  • 17
  • 99
  • 186
Rajeev
  • 1,730
  • 3
  • 20
  • 33
  • 5
    https://stackoverflow.com/questions/21553353/what-is-the-difference-between-cmd-and-entrypoint-in-a-dockerfile In short, `CMD` is appended to `ENTRYPOINT` and run as one command. Not executing `CMD` first then executing `ENTRYPOINT`. – shaun shia Aug 16 '18 at 10:13

1 Answers1

12

Containers are started by executing a single binary, and when that executable exits, the container itself exits. Because of this, when you define both an entrypoint and a command on a container, the result is to pass the command as arguments to the entrypoint. In that situation, your entrypoint needs to know how to process the automatically arguments from the command. What you won't get is a container that runs two commands.

The typical usage is to make the entrypoint a script that performs any initialization steps, like the wait-for-it command. And then the last line is often an exec "$@" which replaces the entrypoint script in pid 1 with a new executable defined by the "$@", which is the command line arguments.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 2
    `docker ps --no-trunc` helps to see the full command ran, e.g. in my case it is obvious that my docker-compose command is now an arg for the original entrypoint: `"/entrypoint.sh /bin/sh -c 'echo 'foo' > /bar"`. – Sebastiaan Sep 06 '21 at 07:51
  • @BMitch do you have an example how to run `wait-for-it` as a command and then run the executable from the `image`? – Jeet Banerjee Nov 12 '21 at 12:57