


How to Run Golang-Migrate Database Migrations with Docker Compose Without `--network host`?
Dec 08, 2024 pm 08:38 PMMigrating Database Using Golang-Migrate in Docker-Compose
Docker-compose has introduced a new syntax that discourages the use of the "--network" option, which raises questions about running migrations using golang-migrate in this environment. This article explores how to set up golang-migrate with docker-compose and connect to a database in another container.
Running Migrations in a Single Folder
The following command, as stated in the golang-migrate documentation, can be used to run all migrations in one folder:
docker run -v migration-dir:/migrations --network host migrate/migrate -path=/migrations/ -database postgres://localhost:5432/database up 2
To adapt this syntax to docker-compose, we can use the following approach:
Adding Docker-Compose Configuration
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:
This configuration sets up a new network called "new" and adds both the "db" and "migrate" services to it. The "db" service serves as the database, while the "migrate" service is used to run migrations.
Connecting to a Database in Another Container
Instead of using the "--network host" option, we establish a network and connect to the database via its alias, "database," within that network. This enables the "migrate" service to interact with the "db" service as if it were running on localhost.
The connection string used in the "command" section reflects this connection method:
"postgres://mydbuser:mydbpwd@database:5432/mydbname?sslmode=disable"
Now, running docker-compose up should successfully execute migrations and connect to the database container.
The above is the detailed content of How to Run Golang-Migrate Database Migrations with Docker Compose Without `--network host`?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language pack import: What is the difference between underscore and without underscore?

How to implement short-term information transfer between pages in the Beego framework?

How do I write mock objects and stubs for testing in Go?

How can I use tracing tools to understand the execution flow of my Go applications?

How to convert MySQL query result List into a custom structure slice in Go language?

How can I define custom type constraints for generics in Go?

How to write files in Go language conveniently?

How do I write benchmarks that accurately reflect real-world performance in Go?
