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

报告相同问题?

悬赏问题

  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)