Home > database >  Docker how to run node js
Docker how to run node js

Time:06-06

This is my docker-compose.yml

version: '3.9'

networks:
  bedrock:

services:
  web:
    container_name: kawa-web
    image: nginx:stable-alpine
    volumes:
      - ./:/var/www/html:delegated
      - ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 8080:80
    depends_on:
      - php
      - mysql
    networks:
      - bedrock

  php:
    container_name: kawa-php
    image: nanoninja/php-fpm:8.0
    volumes:
      - ./:/var/www/html
      - ./docker/config/php.ini:/usr/local/etc/php/conf.d/php.ini
    ports:
      - 9000:9000
    networks:
      - bedrock

  mysql:
    container_name: kawa-db
    image: mysql:8
    volumes:
      - ./docker/db:/var/lib/mysql:delegated
    ports:
      - 3306:3306
    command: --default-authentication-plugin=mysql_native_password
    environment:
      - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
    networks:
      - bedrock

  node:
    container_name: kawa-node
    build:
      context: .
      dockerfile: node.dockerfile
    volumes:
      - ./:/var/www/html
    networks:
      - bedrock

Content of node.dockerfile

FROM node:18-alpine

WORKDIR /var/www/html

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080

When I run docker compose up -d it shows

failed to solve: error from sender: open /path/to/project/docker/db/#innodb_temp: permission denied

How can I fix this? Or any another way to run nodejs inside PHP container maybe?

CodePudding user response:

When your Compose setup has:

services:
  mysql:
    volumes:
      - ./docker/db:/var/lib/mysql:delegated
  node:
    build:
      context: .

The MySQL data directory is in the ./docker/db directory. That's inside the . build-context directory of the Node application, so docker-compose build among other things sends the entire MySQL data to itself, and if the database is currently running, you could get lock or permission problems like this.

The best approach to work around this is to split your application into separate directories, and have each language component only build its own subdirectory.

$ ls -1F
data/
docker-compose.yml
js/
php/
static/

$ ls -1F js
Dockerfile
index.js
node_modules/
package.json
package-lock.json
# docker-compose.yml
version: '3.8'
services:
  ...
  mysql:
    image: mysql:8
    volumes:
      - ./data/db:/var/lib/mysql:delegated
    ports:
      - 3306:3306
    command: --default-authentication-plugin=mysql_native_password
    environment:
      - MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
  node:
    build: ./js

(Note that I've used the short syntax for build: to use the default Dockerfile in the context directory; I've removed unnecessary container_name: and networks: options, using the Compose-provided default network; and I've removed the volumes: that overwrite the image's content. Make sure ./js/Dockerfile has a CMD instruction that says how to start the container.)

  • Related