无法在docker-compose中使dep和dockerize一起工作(但它们是分开工作的)。 为什么?

I have a curious situation where my docker-compose build won't complete when I use dockerize to wait for databases etc to be ready, and use dep to load my Go dependencies.

Here's an extract from docker-compose.yml (there are mosquitto, postgres, and python containers in addition to the golang container shown below)

version '3.3'
services:

   foobar_container:
     image: foobar_image
     container_name: foobar
     build:
      context: ./build_foobar
      dockerfile: Dockerfile.foobar
     #command: dockerize -wait tcp://mosquitto:1883 -wait tcp://postgres:5432 -timeout 200s /go/src/foobar/main
     volumes:
       - ./foobar:/go
     stdin_open: true
     tty: true
     external_links:
       - mosquitto
       - postgres
     ports:
       - 1833
       - 8001
     depends_on:
       - mosquitto
       - postgres

Here's my Dockerfile.foobar

FROM golang:latest
 WORKDIR /go
 RUN apt-get update && apt-get install -y wget mosquitto-clients net-tools
 ENV DOCKERIZE_VERSION v0.6.0
 RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
   && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
   && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
 ADD foobar.sh /foobar.sh
 #RUN go build main.go
 RUN chmod +x /foobar.sh

Here's my build script foobar.sh:

#!/bin/bash

mkdir -p /go/bin # required directory that may have been overwriten by docker-compose `volumes` param
echo "++++++++ Downloading Golang dependencies ... ++++++++"
cd /go/src/foobar
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
echo "++++++++ Installing Golang dependencies ... ++++++++"
dep ensure
echo "++++++++ Testing MQTT message broker ... ++++++++"
until [[ $(mosquitto_sub -h "mosquitto" -t '$SYS/#' -C 1 | cut -c 1-9) = "mosquitto" ]]; do
    echo "++++++++ Message broker is not ready. Waiting one second... ++++++++"
    sleep 1
done
echo "++++++++ Building application... ++++++++" 
go build main.go

If I uncomment the command line of docker-compose.yml my foobar.sh won't run past the curl line. No error is outputted, the execution just stops. If I comment from curl onwards, and uncomment the command line, I can setup to completion (however the foobar container needs to me started manually). My python container (which depends on all postgres, go, and mosquitto containers) sets up fine.

What's going wrong?

There are a couple of things I found, first the execution order, you must ensure the foobar.sh gets executed first. As another recommendation, I wouldn't override the entire /go folder inside the container using volumes, instead use another subfolder, e.g /go/github.com/my-project. I got an app running using this configuration, based on yours:

build_foobar/Dockerfile.foobar:

FROM golang:latest
WORKDIR /go
RUN apt-get update && apt-get install -y wget mosquitto-clients net-tools
ENV DOCKERIZE_VERSION v0.6.0
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
        && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
        && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
ADD foobar.sh /foobar.sh
# RUN go build main.go
RUN chmod +x /foobar.sh

build_foobar/foobar.sh:

#!/bin/bash

# mkdir -p /go/bin # required directory that may have been overwriten by docker-compose `volumes` param
echo "++++++++ Downloading Golang dependencies ... ++++++++"
cd /go/src/foobar
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
echo "++++++++ Installing Golang dependencies ... ++++++++"
dep ensure
echo "++++++++ Testing MQTT message broker ... ++++++++"
until [[ $(mosquitto_sub -h "mosquitto" -t '$SYS/#' -C 1 | cut -c 1-9) = "mosquitto" ]]; do
    echo "++++++++ Message broker is not ready. Waiting one second... ++++++++"
    sleep 1
done
echo "++++++++ Building application... ++++++++" 
go build main.go

dockerize -wait tcp://mosquitto:1883 -wait tcp://postgres:5432 -timeout 200s /go/src/foobar/main

foobar/main.go: place your app main file

docker-compose.yml:

version: '3.3'
services:
  foobar_container:
    image: foobar_image
    container_name: foobar
    build:
      context: ./build_foobar
      dockerfile: Dockerfile.foobar
    # command: dockerize -wait tcp://mosquitto:1883 -wait tcp://postgres:5432 -timeout 200s /go/src/foobar/main
    # command: /bin/bash
    command: /foobar.sh
    volumes:
      - ./foobar:/go/src/foobar
    stdin_open: true
    tty: true
    external_links:
      - mosquitto
      - postgres
    depends_on:
      - mosquitto
      - postgres
    ports:
      - 1833
      - 8001
  mosquitto:
    image: eclipse-mosquitto
  postgres:
    image: postgres