Answer a question

I am trying to up two containers with node and postgres (i need to save the content of the database). Follow the code below:

Dockerfile:

FROM node:lts-alpine

RUN mkdir -p /home/node/api/node_modules && chown -R node:node /home

WORKDIR /home/node/api

COPY package.json /

USER node

RUN npm i

COPY --chown=node:node . .

EXPOSE 3000

COPY ./docker-entrypoint.sh /docker-entrypoint.sh

RUN chmod +x /docker-entrypoint.sh

ENTRYPOINT ["/docker-entrypoint.sh"]

docker-entrypoint.sh:

#!/bin/sh

npx sequelize-cli db:migrate

npm run dev

Well, here I don't know how to run the migrates, because in my computer I only run "sequelize db:migrate", I have a script in package.json too "migrate:dev": "NODE_ENV=development sequelize db:migrate"

for example.

And the docker-compose.yml:

version: '3'

services:
    app-app:
        container_name: app-app
        build: '.'
        volumes:
            - .:/home/node/api
            - /home/node/api/node_modules
        depends_on:
            - postgres-app
        networks:
            - app-connect
        ports:
            - '3000:3000'

    postgres-app:
        container_name: postgres-app
        image: postgres:11
        restart: unless-stopped
        volumes:
            - postgres-app-data:/data
        environment:
            POSTGRES_DB: ${DATABASE}
            POSTGRES_USER: ${DB_USER}
            POSTGRES_PASSWORD: ${DB_PASS}
        networks:
            - app-connect

volumes:
    postgres-app-data:

networks:
    app-connect:
        driver: bridge

But the migration not run yet. I have an error when I try run the docker-entrypoint.sh:

chmod: /docker-entrypoint.sh: Operation not permitted
ERROR: Service 'app-app' failed to build: The command '/bin/sh -c chmod +x /docker-entrypoint.sh' returned a non-zero code: 1

If I remove all these items and only run npm start directly all works but when I try to access the app container to run the migrate show me the error:

docker exec -it 351 npm run migrate:dev                                                                 

> app@1.0.0 migrate:dev /home/node/api
> NODE_ENV=development sequelize db:migrate


Sequelize CLI [Node: 12.14.0, CLI: 5.5.1, ORM: 5.21.3]

Loaded configuration file "src/config/database.js".
Using environment "development".

ERROR: getaddrinfo ENOTFOUND postgres

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app@1.0.0 migrate:dev: `NODE_ENV=development sequelize db:migrate`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the app@1.0.0 migrate:dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/node/.npm/_logs/2020-01-09T12_43_32_377Z-debug.log

How I can do this? Thanks!

Answers

So as per the comments above, the problem was that the DB_HOST was set with the wrong value.

The DB_HOST should have the value of the service name in that case: postgres-app.

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐