Home > Backend Development > Golang > How to Run Golang-Migrate with Docker Compose and a Separate Database Container?

How to Run Golang-Migrate with Docker Compose and a Separate Database Container?

Linda Hamilton
Release: 2024-12-06 13:06:14
Original
576 people have browsed it

How to Run Golang-Migrate with Docker Compose and a Separate Database Container?

Running Golang-Migrate with Docker Compose

Golang-migrate's documentation provides a command for executing all migrations in a given directory:

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

However, this command is not compatible with the syntax of docker-compose. This article will demonstrate how to modify the command to fit the new syntax and connect to a database running in another container.

Docker-Compose Integration

To integrate golang-migrate with docker-compose, add the following 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

This configuration creates a network named "new" and includes the "db" and "migrate" services. The "db" service is defined with the required environment variables for a PostgreSQL database.

Connecting to a Database in Another Container

To connect to a database running in another container, modify the connection string in the "command" attribute of the "migrate" service:

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

In this connection string:

  • mydbuser is the username for the PostgreSQL database
  • mydbpwd is the password for the PostgreSQL database
  • database is the alias defined in the "db" service
  • 5432 is the port number for the PostgreSQL database
  • mydbname is the name of the PostgreSQL database

By using the alias "database," you can connect to the "db" service as if it were running on localhost.

With these modifications, you can successfully run golang-migrate with docker-compose and connect to a database in another container.

The above is the detailed content of How to Run Golang-Migrate with Docker Compose and a Separate Database Container?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template