dro59505
2019-04-21 04:50
浏览 123

如何使用docker-compose运行golang-migrate?

In golang-migrate's documentation, it is stated that you can run this command to run all the migrations in one folder.

docker run -v {{ migration dir }}:/migrations --network host migrate/migrate
    -path=/migrations/ -database postgres://localhost:5432/database up 2

How would you do this to fit the syntax of the new docker-compose, which discourages the use of --network?

And more importantly: How would you connect to a database in another container instead to one running in your localhost?

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

在golang-migrate的文档中,您可以运行此命令来在一个文件夹中运行所有迁移。

  docker run -v {{迁移目录}}:/ migrations-网络主机migration / migrate 
 -path = / migrations /-数据库postgres:// localhost:  5432 /数据库启动2 
   
 
 

您如何执行此操作以适应新的docker-compose的语法,从而不鼓励使用-network < / code>?

更重要的是:如何连接到另一个容器中的数据库而不是本地主机中运行的数据库?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutuan4361 2019-04-21 04:50
    已采纳

    Adding this to your docker-compose.yml will do the trick:

        db:
            image: postgres
            networks:
                new:
                    aliases:
                        - database
            environment:
                POSTGRES_DB: mydbname
                POSTGRES_USER: mydbuser
                POSTGRES_PASSWORD: mydbpwd
            ports:
                - "5432"
        migrate:
            image: migrate/migrate
            networks:
                - new
            volumes:
                - .:/migrations
            command: ["-path", "/migrations", "-database",  "postgres://mydbuser:mydbpwd@database:5432/mydbname?sslmode=disable", "up", "3"]
            links: 
                - db
    networks:
          new:
    

    Instead of using the --network host option of docker run you set up a network called new. All the services inside that network gain access to each other through a defined alias (in the above example, you can access the db service through the database alias). Then, you can use that alias just like you would use localhost, that is, in place of an IP address. That explains this connection string:

    "postgres://mydbuser:mydbpwd@database:5432/mydbname?sslmode=disable"
    
    点赞 评论

相关推荐 更多相似问题