2017-11-28 04:38



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?


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"]
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongruolin5324 dongruolin5324 4年前

    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.

    点赞 评论 复制链接分享