2

I noticed whenever I build the docker image it builds, but fails to run npx and node commands in the dockerfile for the image. When the container for the image is up, I would not see a generate-typings.js file at all. But when I enter the container to run the script it works.

DockerFile

FROM node:14.17.6 as base

WORKDIR /usr/src/app

#copy package.json to docker container
COPY package.json ./

#For schema back end
COPY prisma/. /usr/src/app/prisma/.
COPY ts*.json ./
#For the database connection
COPY .env ./
#install package.json dependency
RUN npm i
#transfer code files
COPY ./src /usr/src/app/src/
#generate typings for graphql -- line not running in image
RUN npx - tsc --skipLibCheck /usr/src/app/src/gql/generate-typings.ts
#update the scheme 
RUN node /usr/src/app/src/gql/generate-typings.js

#include nestjscli globally
RUN npm install -g @nestjs/cli
#generate
RUN npx prisma generate

docker-compose file

version: "3.9"

services: 
  api:
    container_name: back_end_api
    build: 
      context: .
      dockerfile: Dockerfile
      target: base
    tty: true
    expose:
      - '4000'
    ports: 
      - "4000:4000"
    volumes:
      - ./src:/usr/src/app/src  #persist src folder to host machine
    networks:
      - emNetwork
    command: npm run dev
networks:
  emNetwork:  
adeniran827
  • 57
  • 1
  • 7
  • The first thing that jumps out is that you're overwriting the container's `/usr/src/app/src` directory with a bind mount, so running things in the container sees a fundamentally different source tree than during the build stage. Does removing the `volumes:` block make a difference, or at least get you consistent behavior? – David Maze Oct 01 '21 at 16:39
  • @DavidMaze it does make a difference but at the same time, I also want to persist the data for code editing. How can I do that? especially if i was to persist data for the new node_modules folder from container to host etc, without overwriting the content in the container. – adeniran827 Oct 01 '21 at 16:46
  • I tend to recommend using Node, without Docker, for ordinary development, and then making sure your image is self-contained (without a volume mount) for integration testing and deployment. – David Maze Oct 01 '21 at 17:13
  • thanks I found a way around it, using the command in docker-compose with bash -c – adeniran827 Oct 01 '21 at 19:52
  • @adeniran827 whats the work around for it? – Bibek Sep 20 '22 at 10:53
  • Does this answer your question? [Why \`~/.bashrc\` is not executed when run docker container?](https://stackoverflow.com/questions/55206227/why-bashrc-is-not-executed-when-run-docker-container) – Nick Roz Jun 14 '23 at 23:42
  • It is very likely due to the same reason by which .bashrc is not loaded – Nick Roz Jun 14 '23 at 23:43

0 Answers0