skip to Main Content

I have this docker-compose.yml file:

version: "3"

services:
  db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ROOT_USER: root
      MYSQL_DATABASE: bl
    ports:
      - "3306:3306"
    volumes:
      - ./data:/var/lib/mysql

  server:
    env_file: "./server/.env"
    build:
      context: ./server
      dockerfile: ./Dockerfile
    image: "server"
    ports:
      - "5000:5000"
    depends_on:
      - db

and Dockerfile:

FROM node:lts-alpine

WORKDIR /server

COPY . .
COPY package*.json ./
COPY .env ./ 

RUN yarn install

EXPOSE 5000
EXPOSE 3306

ENV NODE_ENV=docker

RUN yarn typeorm:migrate
RUN yarn typeorm:run-migrations

CMD ["yarn", "start"]

When trying to build both of them, I am getting an error on this line: RUN yarn typeorm:migrate.

This is the error that I am getting:

 > [7/8] RUN yarn typeorm:migrate:                                                                                             
#10 0.857 yarn run v1.22.19                                                                                                    
#10 0.943 $ yarn create:migrate-repo && yarn run typeorm -- -d ./src/systems/typeOrm.config.ts migration:generate ./src/migrations/migration
#10 1.344 $ mkdir -p ./src/migrations
#10 1.757 warning From Yarn 1.0 onwards, scripts don't require "--" for options to be forwarded. In a future version, any explicit "--" will be forwarded as-is to the scripts.
#10 1.769 $ ts-node ./node_modules/typeorm/cli -d ./src/systems/typeOrm.config.ts migration:generate ./src/migrations/migration
#10 7.524 Error during migration generation:
#10 7.527 Error: connect ECONNREFUSED 127.0.0.1:3306
#10 7.527     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
#10 7.527   errno: -111,
#10 7.527   code: 'ECONNREFUSED',
#10 7.527   syscall: 'connect',
#10 7.527   address: '127.0.0.1',
#10 7.527   port: 3306,
#10 7.527   fatal: true
#10 7.527 }
#10 7.565 error Command failed with exit code 1.
#10 7.565 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
#10 7.594 error Command failed with exit code 1.
#10 7.594 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
------

The error is occurring because the server cannot establish a connection with the db image.

This is my DataSource:

export const appDataSource = new DataSource({
  type: "mysql",
  host,
  port,
  username: rootUser,
  password,
  database,
  entities: [Company, Order],
  migrationsTableName: "migrations",
  migrations: [`${__dirname}/../migrations/*.ts`],
});

Here is a list of things that I was tring to do. in order to solve the issue:

  1. Create a network bridge between the two images. It did not work.
version: "3"

services:
  db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ROOT_USER: root
      MYSQL_DATABASE: bl
    ports:
      - "3306:3306"
    volumes:
      - ./data:/var/lib/mysql
    networks:
      - my-network

  server:
    env_file: "./server/.env"
    build:
      context: ./server
      dockerfile: ./Dockerfile
    image: "server"
    ports:
      - "5000:5000"
    depends_on:
      - db
    networks:
      - my-network

networks:
  my-network:
    driver: bridge

  1. Change the name of the host to the name of my db image with and without my network bridge.

  2. I was trying even to get the direct IP of the db image and it did not work.

docker inspect db | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "172.23.0.2",

2

Answers


  1. Chosen as BEST ANSWER

    OK, so the way I've fixed it was:

    I've added a command line in my docker-compose.yml file as Andromeda suggested.

    My docker-compose file looks like this:

    version: "3"
    
    services:
      db:
        image: mysql
        restart: always
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_ROOT_USER: root
          MYSQL_DATABASE: bl
        ports:
          - "3306:3306"
        volumes:
          - ./data:/var/lib/mysql
    
      server:
        env_file: "./server/.env"
        build:
          context: ./server
          dockerfile: ./Dockerfile
        image: "server"
        ports:
          - "5000:5000"
        depends_on:
          - db
        command: sh -c "yarn typeorm:migrate && yarn typeorm:run-migrations && yarn start"
    
    

    And my Dockerfile looks like this:

    FROM node:lts-alpine
    
    WORKDIR /server
    
    COPY . .
    COPY package*.json ./
    COPY .env ./ 
    
    RUN yarn install
    
    EXPOSE 5000
    EXPOSE 3306
    
    ENV NODE_ENV=docker
    
    CMD ["yarn", "start"]
    

    The scripts that I am running from package.json are:

        "typeorm": "ts-node ./node_modules/typeorm/cli",
        "create:migrate-repo": "mkdir -p ./src/migrations",
        "typeorm:migrate": "yarn create:migrate-repo && yarn run typeorm -- -d ./src/systems/typeOrm.config.ts migration:generate ./src/migrations/migration",
        "typeorm:run-migrations": "yarn typeorm migration:run -- -d ./src/systems/typeOrm.config.ts",
    

    In order to use the db image in my service, I needed to use "db" as a host:

    export const appDataSource = new DataSource({
      type: "mysql",
      host: "db", // <--- use "db" instead of "localhost".
      port,
      username: rootUser,
      password,
      database,
      entities: [Company, Order],
      migrationsTableName: "migrations",
      migrations: [`${__dirname}/../migrations/*.ts`],
    });
    

  2. The problem is the db address is not available in the build time.

    RUN yarn typeorm:migrate 
    RUN yarn typeorm:run-migrations
    

    Remove these line from the Dockerfile and add to a command directive in the server service:

      server:
        env_file: "./server/.env"
        build:
          context: ./server
          dockerfile: ./Dockerfile
        image: "server"
        ports:
          - "5000:5000"
        depends_on:
          - db
        networks:
          - my-network
        command:
          - yarn typeorm:migrate && yarn typeorm:run-migrations && yarn start
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search