Home > Backend Development > Golang > How to Run Database Migrations with Go and Docker Compose After the `--network host` Deprecation?

How to Run Database Migrations with Go and Docker Compose After the `--network host` Deprecation?

DDD
Release: 2024-12-16 11:20:10
Original
117 people have browsed it

How to Run Database Migrations with Go and Docker Compose After the `--network host` Deprecation?

Migrating Databases with Go and Docker-Compose

Running migrations using the golang-migrate tool can help ensure database schema consistency. However, with the deprecation of --network host in Docker-Compose, you may face challenges when executing migrations. This article explores how to adapt to the updated syntax and how to connect to a database residing in a separate container.

Adapt to New Docker-Compose Syntax

To run migrations without --network host, you can add the following configuration to your docker-compose.yml file:

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:
Copy after login

In this configuration, a network called new is created, and the migrate service is linked to the db service through the database alias.

Connecting to a Remote Database

To connect to a database in a separate container, use the following format in the connection string:

"postgres://mydbuser:mydbpwd@database:5432/mydbname?sslmode=disable"
Copy after login

Here, database is the alias defined in the network configuration. This allows you to connect to the remote database as if it were running locally.

The above is the detailed content of How to Run Database Migrations with Go and Docker Compose After the `--network host` Deprecation?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template