donglang5157 2019-02-17 23:32
浏览 212
已采纳

Golang Docker容器未在Docker-Compose中重新启动

I want to be able to restart a golang docker file on failure to connect to rabbitmq as outined here: (Docker Compose wait for container X before starting Y see answer by: svenhornberg). Unfortunately my golang container will exit but never restart and I don't know why.

Docker-compose:

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 file:

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" ]

Here is my golang code:

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)
  <...>

And here is my terminal output (golang never restarts after rabbit called):

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...>

I'm very confused on how to get this to work. What am I doing wrong?

EDIT:

The exec.Command was incorrectly implemented, however os.Exit(1), log.Fatal, and log.Panic exit the container, but the container does not restart. Still confused.

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 android object box 一个实体多个表怎么写
      • ¥15 temux 启用docker 服务失败
      • ¥15 Flask 使用celery发送邮件出现‘目标计算机积极拒绝‘
      • ¥60 老人用的sd卡在手机里面不知道操作了什么,导致图片和视频变成了文件,取下sd卡连接电脑就是图中的样子,后缀改为.jpg才可以,需要用系统的画图软件才能打开,文件属性还是文件,有没有批量操作的解决办法
      • ¥15 超时跳出方法代码的返回值问题
      • ¥15 汇编语言程序设计设计,ascii码求数,再求数的BCD码
      • ¥30 Mask rcnn训练自己的数据集出现问题!
      • ¥20 研究人工智能时的几个问题
      • ¥15 mysql的sql查询写法问题
      • ¥15 Python中导入模块中的函数运行出错