简体   繁体   中英

when running docker-compose it doesn't do as docker-entrypoint.sh instructs

dockerfile

FROM node:10

RUN mkdir /app
WORKDIR /app

COPY . /app
RUN npm install

EXPOSE 3000

CMD ["npm","start"]


docker-compose.yaml

version: "3.9"

networks:
  app-tier:
    driver: bridge
services:
  db:
    platform: linux/x86_64
    image: mysql
    networks:
      - app-tier
    container_name: mysql_container
    restart: always
    ports:
      - "3306:3306"
    # environment:
    #   - MYSQL_ROOT_PASSWORD: abc
    #   - MYSQL_DATABASE: mydb
    volumes:
      - ./:/usr/src/app
     
  web:
    depends_on:
     - db
    environment:
      - MYSQL_HOST=mysql
      - MYSQL_USER=me
      - MYSQL_PASSWORD=1234
      - MYSQL_DATABASE=mydb
 
    build: . 

docker-entrypoint.sh

dockerize -wait tcp://mysql:3306 -timeout 20s

echo "Start server"

npm run start

result: app and mysql starts up but I am assuming db should start up first in order app to connect to the database

web_1  | 
web_1  | > xxxx@0.0.0 start /app
web_1  | > export NODE_ENV=production || set NODE_ENV=production && node ./bin/www
web_1  | 
web_1  | production
web_1  | process.env.NODE_ENV production
web_1  | { SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
web_1  |     at ConnectionManager.connect (/app/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:116:17)
web_1  |     at process._tickCallback (internal/process/next_tick.js:68:7)
web_1  |   name: 'SequelizeConnectionRefusedError',
web_1  |   parent:
web_1  |    { Error: connect ECONNREFUSED 127.0.0.1:3306
web_1  |        at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
web_1  |      errno: 'ECONNREFUSED',
web_1  |      code: 'ECONNREFUSED',
web_1  |      syscall: 'connect',
web_1  |      address: '127.0.0.1',
......

I don't know if docker-compose file is not reading .sh file or not nor do I know somethings wrong on docker-compose file. can anybody help me with the configuration.

You have a connection issue as your web service cannot connect with your database.

Keep in mind that the hostname of your database equals the service name. So instead of connecting with 127.0.0.1:3306 connect with http://db:3306 instead.

I figured it out

  1. I didn't define RUN ["chmod", "+x", "docker-entrypoint.sh"] ENTRYPOINT ["sh", "docker-entrypoint.sh"] in my dockerfile
FROM node:10

ENV DOCKERIZE_VERSION v0.2.0
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \  
    && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install dependencies
COPY package.json .
RUN npm install

# Bundle app source
COPY . .

RUN ["chmod", "+x", "docker-entrypoint.sh"]
ENTRYPOINT ["sh", "docker-entrypoint.sh"]


# Exports
EXPOSE 3000
  1. I didn't configure db related configutation as same as my docker-compose file in my .env file
DB_USERNAME=user1
DB_PASSWORD=password1
DB_NAME=user1
DB_HOSTNAME=mysql
JWT_SECRETKEY=123

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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