dongzai3917
dongzai3917
2017-11-28 04:38
浏览 174

为什么Docker容器中的应用无法重启?

I've deployed some docker containers with golang apps. One of them I need to start by this command:

docker run --restart unless-stopped -it myapp /bin/bash

The next step I enter the container and edit some config files, then I run

go build main.go

and ./main After that I press ctrl+q and leave it out. Everything works perfectly and all my containers restart perfectly after restarting server. But there is one issue, when myapp container restarts, the golang application doesn't run while container still works. I have to enter this again and run ./main. How can I fixed it?

Dockerfile

FROM golang:1.8 
WORKDIR /go/src/app 
COPY . . 
RUN go-wrapper download # "go get -d -v ./..." 
RUN go-wrapper install # "go install -v ./..." RUN ["apt-get","update"] 
RUN ["apt-get","install","-y","vim"] 
EXPOSE 3000 
CMD ["app"]

图片转代码服务由CSDN问答提供 功能建议

我已经使用golang应用程序部署了一些Docker容器。 我需要通过此命令来启动其中之一:

  docker run --restart除非-stopped -it myapp / bin / bash 
    
 
 

下一步,我进入容器并编辑一些配置文件,然后运行

  go build main.go 
   
 
 

./ main 之后,我按ctrl + q并把它留在外面。 一切正常,重启服务器后,我的所有容器都能完美地重启。 但是有一个问题,当myapp容器重新启动时,当容器仍在工作时,golang应用程序无法运行。 我必须再次输入并运行 ./ main 。 我该如何解决?

Dockerfile

  FROM golang:1.8 
WORKDIR / go / src / app 
COPY。  。  
RUN go-wrapper下载#“ go get -d -v。/ ...” 
RUN go-wrapper install#“ go install -v。/ ...” RUN [“ apt-get”,“ update”]  
RUN [“ apt-get”,“ install”,“-y”,“ vim”] 
EXPOSE 3000 
CMD [“ app”] 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongruolin5324
    dongruolin5324 2017-11-28 18:05
    已采纳

    When you create a container and pass in /bin/bash as the command, that's as far as Docker cares. When the container restarts, it will start up another instance of /bin/bash.

    Docker doesn't watch your shell session and see what things you do after it starts the command. If you want to actually run ./main as the command of the container, then you'll need to pass in /go/src/app/main as the command instead of /bin/bash.

    Additionally, compiling code is something better done during the image build phase instead of at container runtime.

    点赞 评论

相关推荐