简体   繁体   English

在 docker 容器中未创建数据库迁移

[英]In docker container are not created db migrations

Can anyone say why my migrations are not created in docker container?谁能说出为什么我的迁移不是在 docker 容器中创建的? Locally I have a working project but in container there are no tables.在本地我有一个工作项目,但在容器中没有表。 Locally my tables are created but when I run docker-compose up there are no migrations in console logs在本地创建了我的表,但是当我运行 docker-compose up 时,控制台日志中没有迁移

My Dockerfile:我的 Dockerfile:

FROM golang:1.17-alpine as build-stage

RUN mkdir -p /app

WORKDIR /app

COPY . /app
RUN go mod download

RUN go build -o crypto main.go

FROM alpine:latest

WORKDIR /

COPY --from=build-stage /app/crypto .

EXPOSE 9999

ENTRYPOINT [ "/crypto" ]

docker-compose.yml码头工人-compose.yml

version: "3"

volumes:
  crypto_postgres_data: {}

services:
  crypto:
    build:
      context: .
      dockerfile: ./Dockerfile
    image: crypto_app
    platform: linux/amd64
    env_file:
      - ./.env
    depends_on:
      - postgres
    ports:
      - "9999:9999"

  postgres:
    image: postgres:14.2 
    healthcheck:
      test: [ "CMD", "pg_isready", "-U", "$POSTGRES_USER", "-d", "$POSTGRES_DB" ]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    restart: unless-stopped
    env_file:
      - ./.env 
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 4G
    volumes:
      - crypto_postgres_data:/var/lib/postgresql/data:Z
  migrate:
    image: migrate/migrate
    volumes:
      - .:/migrations
My output in container:
[gomigrate] 2022/06/22 14:02:11 Migrations path: migrations/
[gomigrate] 2022/06/22 14:02:11 Migrations table not found
[gomigrate] 2022/06/22 14:02:11 Created migrations table: gomigrate
[gomigrate] 2022/06/22 14:02:11 Migrations file pairs found: 0

Thanks in advance提前致谢

Your migrate service does not have any command so it cannot migrate data in the migrations folder.您的迁移服务没有任何命令,因此它无法迁移迁移文件夹中的数据。 You should change it like this:你应该像这样改变它:

version: "3"

volumes:
  crypto_postgres_data: {}

services:
  crypto:
    build:
      context: .
      dockerfile: ./Dockerfile
    image: crypto_app
    platform: linux/amd64
    env_file:
      - ./.env
    depends_on:
      - postgres
    ports:
      - "9999:9999"
    networks:
      - crypto

  postgres:
    image: postgres:14.2 
    healthcheck:
      test: [ "CMD", "pg_isready", "-U", "$POSTGRES_USER", "-d", "$POSTGRES_DB" ]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    restart: unless-stopped
    env_file:
      - ./.env 
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 4G
    volumes:
      - crypto_postgres_data:/var/lib/postgresql/data:Z
    networks:
      - crypto

  migrate:
    image: migrate/migrate
    restart: on-failure:5 
    env_file:
      - ./.env 
    volumes:
      - ./migrations:/migrations
    command: ["-path", "/migrations", "-database",  "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?sslmode=disable", "up"]
    depends_on:
      - db
    networks:
      - crypto

networks:
  crypto:
    driver: bridge

Reference: How to run golang-migrate with docker-compose?参考: 如何使用 docker-compose 运行 golang-migrate?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM