In docker container are not created db migrations

Viewed 66

Can anyone say why my migrations are not created in docker container? 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

My 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

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

1 Answers

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?

Related