<p>Hey fellow gophers,</p>
<p>I'm building a full stack web application with the backend written in Go. I'm using docker-compose with machine for neat deployment. Currently my development dockerfile looks like this:</p>
<pre><code>version: '2'
services:
caddy:
container_name:
image: abiosoft/caddy
volumes:
- ./Caddyfile:/etc/Caddyfile
- $HOME/.caddy:/root/.caddy
- ./build:/root/build
ports:
- "2015:2015"
- "80:80"
- "443:443"
links:
- goapp
depends_on:
- goapp
db:
image: postgres:latest
container_name: postgres
ports:
- "5432"
environment:
- POSTGRES_DB=goapp
- POSTGRES_PASSWORD=docker
- POSTGRES_USER=docker
goapp:
container_name: goapp
build: .
ports:
- "3000:3000"
links:
- db
depends_on:
- db
</code></pre>
<p>and my Main.go like this:</p>
<p>package main</p>
<pre><code>import (
"flag"
"log"
"net/http"
"os"
"github.com/barthr/microservice/api/dto"
"github.com/barthr/microservice/api/routes"
"github.com/gorilla/handlers"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
var (
connName = flag.String("db", "host=db:5432 user=docker dbname=goapp sslmode=disable password=docker", "set the db connecction name")
)
func main() {
flag.Parse()
db, err := gorm.Open("postgres", *connName)
db.CreateTable("goapp").AutoMigrate(&dto.User{})
if err != nil {
log.Println(err)
}
defer db.Close()
r := routes.InitRoutes(db)
http.ListenAndServe(":3000", 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'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's "depends_on" controls the order in which containers are created but does not "wait" 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't be dialed.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传