始终在Docker容器中运行Go HTTP服务器应用程序。

huangapple go评论88阅读模式
英文:

Keep always running Go http server app inside a Docker container

问题

我有一个运行着我创建的Go二进制文件的Docker容器,它是一个使用Gin框架的HTTP服务器。我没有使用其他的Web服务器,只是使用Go内部的HTTP服务器。在我的Dockerfile文件的末尾,我有以下内容:

EXPOSE 80

CMD ["/home/project/microservices/backend/engine/cmd/main"]

我使用docker-compose来运行容器,并为每个容器设置了"restart: always"。这样就可以正常工作了!

但是我的问题是,如果我创建的HTTP服务器由于编程错误或其他原因而失败,它会重新启动吗?我该如何检查这一点?Docker有相关的工具吗?

我尝试使用Supervisord,但是遇到了一些问题,无法成功运行它。

我想找到一个解决方案,让容器内的HTTP服务器始终保持运行状态。

我该怎么做?

英文:

I have a Docker container that runs just a Go binary I created that is a http server with Gin framework. I don't use any other Web Server, just Go's internal http server. Inside my Dockerfile at the end of file I have this:

EXPOSE 80

CMD ["/home/project/microservices/backend/engine/cmd/main"]

I use docker-compose to run the container and restart: always for each container. And it works!

But my question is that, if the http server that I created fails due to programming error or something, It will restart? how can I check this? does Docker has tools for this?

I tried go with Supervisord but it has some problems and I was not successful on running it.

I want a workaround to keep the http server inside container always running.

What can I do?

答案1

得分: 0

你可以尝试从主机上终止该进程。使用类似以下命令查找进程ID:

ps -aux | grep main

然后使用以下命令终止它:

sudo kill <进程ID>

Docker 将会重新启动该进程。通过使用以下命令:

docker ps

你应该能够看到 'status' 已经变成了类似 Up 10 seconds 的状态。

英文:

You can try killing the process from the host. Find the process id using something like

ps -aux | grep main

Then kill it using

sudo kill &lt;process id&gt;

Docker will then restart it. By using

docker ps

you should see that the 'status' has changed to something like Up 10 seconds.

huangapple
  • 本文由 发表于 2022年6月21日 05:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/72693003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定