Golang postgres and docker-compose question

polaris · · 435 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey fellow gophers,</p> <p>I&#39;m building a full stack web application with the backend written in Go. I&#39;m using docker-compose with machine for neat deployment. Currently my development dockerfile looks like this:</p> <pre><code>version: &#39;2&#39; services: caddy: container_name: image: abiosoft/caddy volumes: - ./Caddyfile:/etc/Caddyfile - $HOME/.caddy:/root/.caddy - ./build:/root/build ports: - &#34;2015:2015&#34; - &#34;80:80&#34; - &#34;443:443&#34; links: - goapp depends_on: - goapp db: image: postgres:latest container_name: postgres ports: - &#34;5432&#34; environment: - POSTGRES_DB=goapp - POSTGRES_PASSWORD=docker - POSTGRES_USER=docker goapp: container_name: goapp build: . ports: - &#34;3000:3000&#34; links: - db depends_on: - db </code></pre> <p>and my Main.go like this:</p> <p>package main</p> <pre><code>import ( &#34;flag&#34; &#34;log&#34; &#34;net/http&#34; &#34;os&#34; &#34;github.com/barthr/microservice/api/dto&#34; &#34;github.com/barthr/microservice/api/routes&#34; &#34;github.com/gorilla/handlers&#34; &#34;github.com/jinzhu/gorm&#34; _ &#34;github.com/jinzhu/gorm/dialects/postgres&#34; ) var ( connName = flag.String(&#34;db&#34;, &#34;host=db:5432 user=docker dbname=goapp sslmode=disable password=docker&#34;, &#34;set the db connecction name&#34;) ) func main() { flag.Parse() db, err := gorm.Open(&#34;postgres&#34;, *connName) db.CreateTable(&#34;goapp&#34;).AutoMigrate(&amp;dto.User{}) if err != nil { log.Println(err) } defer db.Close() r := routes.InitRoutes(db) http.ListenAndServe(&#34;:3000&#34;, handlers.LoggingHandler(os.Stdout, r)) } </code></pre> <p>My problem is that I cannot access the DB from the docker-compose (tried with all different ports and usernames, I can connect with pgadmin). I don&#39;t understand how this is possible, am I missing something?</p> <p>Thanks for you time, I appreciate it a lot.</p> <p>p.s. English is not my first language</p> <hr/>**评论:**<br/><br/>bubaflub: <pre><p>docker-compose&#39;s &#34;depends_on&#34; controls the order in which containers are created but does not &#34;wait&#34; until any particular service inside of that container is ready or accepting incoming connections. From <a href="https://docs.docker.com/compose/compose-file/#/dependson:" rel="nofollow">https://docs.docker.com/compose/compose-file/#/dependson:</a></p> <blockquote> <p>Note: depends_on will not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.</p> </blockquote> <p>They link to <a href="https://docs.docker.com/compose/startup-order/" rel="nofollow">https://docs.docker.com/compose/startup-order/</a> which has an example script that waits for postgres to be ready.</p> <p>Alternatively, you could put a small retry loop in your go program to attempt to connect to the database multiple times. For example, you could have it try to connect every second for 10 seconds or 15 seconds and then fail if a connection can&#39;t be dialed.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:701969077

435 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传