【发布时间】:2019-02-17 23:32:15
【问题描述】:
我希望能够在无法连接到 rabbitmq 时重新启动一个 golang docker 文件,如下所示:(Docker Compose wait for container X before starting Y 参见答案:svenhornberg)。 不幸的是,我的 golang 容器将退出但永远不会重新启动,我不知道为什么。
Docker 撰写:
version: '3.3'
services:
mongo:
image: 'mongo:3.4.1'
container_name: 'datastore'
ports:
- '27017:27017'
rabbitmq:
restart: always
tty: true
image: rabbitmq:3.7-management-alpine
hostname: "rabbit"
ports:
- "15672:15672"
- "5672:5672"
labels:
NAME: "rabbitmq"
volumes:
- ./rabbitmq-isolated.conf:/etc/rabbitmq/rabbitmq.config
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:15672"]
interval: 3s
timeout: 5s
retries: 20
api:
restart: always
tty: true
container_name: 'api'
build: '.'
working_dir: /go/src/github.com/patientplatypus/project
ports:
- '8000:8000'
volumes:
- './:/go/src/github.com/patientplatypus/project'
- './uploads:/uploads'
- './scripts:/scripts'
- './templates:/templates'
depends_on:
- "mongo"
- "rabbitmq"
Docker 文件:
FROM golang:latest
WORKDIR /go/src/github.com/patientplatypus/project
COPY . .
RUN go get github.com/imroc/req
<...more go gets...>
RUN go get github.com/joho/godotenv
EXPOSE 8000
ENTRYPOINT [ "fresh" ]
这是我的 golang 代码:
package main
import (
"fmt"
"log"
"os"
"os/exec"
"net/http"
)
func main() {
fmt.Println("Golang server started")
godotenv.Load()
fmt.Println("now doing healthcheck on rabbit")
exec.Command("docker-compose restart api")
os.Exit(1)
<...>
这是我的终端输出(调用rabbit后golang永远不会重新启动):
api | 23:23:00 app | Golang server started
api | 23:23:00 app | now doing healthcheck on rabbit
rabbitmq_1 |
rabbitmq_1 | ## ##
rabbitmq_1 | ## ## RabbitMQ 3.7.11. Copyright (C) 2007-2019 Pivotal Software, Inc.
rabbitmq_1 | ########## Licensed under the MPL. See http://www.rabbitmq.com/
rabbitmq_1 | ###### ##
rabbitmq_1 | ########## Logs: <stdout>
<...more rabbit logging...>
我很困惑如何让它发挥作用。我做错了什么?
编辑:
exec.Command 执行不正确,但是 os.Exit(1)、log.Fatal 和 log.Panic 退出容器,但容器没有重新启动。还是一头雾水。
【问题讨论】:
-
永远不要忽略错误。您的
exec.Command呼叫错误 -
我已经更新了帖子。
-
你是如何调用
docker-compose的? (一些调用can causerestart:to be ignored。) -
@DavidMaze 我不确定我是否理解这个问题。我使用
docker-compose up --build,并且我的每个容器都被标记为restart: always。 -
啊,我明白了
Note: This option is ignored when deploying a stack in swarm mode with a (version 3) Compose file. Use restart_policy instead.我不认为这种情况适用。
标签: docker go docker-compose rabbitmq restart