dpt1712 2018-06-04 12:26
浏览 158
已采纳

无法在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?

  • 写回答

1条回答 默认 最新

  • dongyata3336 2018-06-04 14:56
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 Python如何后台操作Vmwake虚拟机键鼠
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容