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

时间:2018-06-04 12:26:41

标签: docker go docker-compose

我有一个好奇的情况,当我使用dockerize等待数据库等准备好时,我的docker-compose构建不会完成,并使用dep来加载我的Go依赖项。

这里是docker-compose.yml的摘录(除了下面显示的golang容器外,还有mosquitto,postgres和python容器)

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

这是我的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

这是我的构建脚本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

如果我取消注释command docker-compose.yml行,foobar.sh我的curl赢了curl行。没有错误输出,执行就停止了。 如果我从command开始评论,并取消注释foobar行,我可以设置完成(但gem install bundler容器需要我手动启动)。我的python容器(取决于所有的postgres,go和mosquitto容器)设置得很好。

出了什么问题?

1 个答案:

答案 0 :(得分:1)

我发现了一些事情,首先是执行顺序,你必须确保首先执行foobar.sh。作为另一个建议,我不会使用卷覆盖容器内的整个/go文件夹,而是使用另一个子文件夹,例如/go/github.com/my-project。 我使用此配置运行的应用程序基于您的:

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:放置您的应用主文件

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