我有一个奇怪的情况,当我使用dockerize
等待数据库等就绪并使用dep
加载Go依赖项时,我的docker-compose构建将无法完成。
这是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
容器需要我手动启动)。我的python容器(取决于所有postgres,go和mosquitto容器)设置良好。怎么了
最佳答案
我发现了几件事情,首先是执行顺序,您必须确保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
关于docker - 无法在docker-compose中使dep和dockerize一起工作(但它们是分开工作的)。为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50680615/