gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 02:09:45 25 4
gpt4 key购买 nike

我有一个奇怪的情况,当我使用 dockerize 等待数据库等准备就绪,并使用 dep 加载时,我的 docker-compose 构建将无法完成我的 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

如果我取消注释 docker-compose.ymlcommand 行,我的 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 - 无法让 dep 和 dockerize 在 docker-compose 中一起工作(但它们是分开工作的)。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50680615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com