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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能