-1

I'm trying to have docker compile my node project in the same folder as the source.

This is my Dockerfile:

FROM node:16.8.0-alpine

WORKDIR /app

RUN rm -rf node_modules

RUN yarn install

RUN yarn build

And this is my docker-compose.yml file:

version: "3"
services:
  vuebuild:
    build: ./frontend
    volumes:
      - ./frontend:/app

But I'm getting this error:

 > [5/5] RUN yarn dev:
#8 0.649 yarn run v1.22.5
#8 0.673 error Couldn't find a package.json file in "/app"
#8 0.673 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
------
executor failed running [/bin/sh -c yarn dev]: exit code: 1
ERROR: Service 'vuebuild' failed to build : Build failed
Marcello Romani
  • 2,967
  • 31
  • 40

2 Answers2

0

Since you're trying to start a development environment (assuming you want to develop instide alpine, but that's another story) instead of building a Docker image for your Node app, you could use this docker-compose.yaml file:

version: '3'
services:
    vuebuild:
        image: node:16.8.0-alpine
        command: sh -c "cd /app && yarn install && node index.js"
        volumes:
            - ./frontend:/app

Start the container:

$ docker-compose up

Then attach to it

$ docker ps 
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
3cd8f0844e87   d2adab47ce8f   "docker-entrypoint.s…"   7 seconds ago   Up 6 seconds             hello-node_vuebuild_1
$ docker exec -it hello-node_vuebuild_1 sh
Marcello Romani
  • 2,967
  • 31
  • 40
  • Okay, we misunderstood each other. I need that container to create the dist folder for me only on first boot when docker-compose up is run – DreamingCodes Aug 26 '21 at 21:50
  • What's the `dist` folder? – Marcello Romani Aug 26 '21 at 21:51
  • the folder with the result of yarn build – DreamingCodes Aug 26 '21 at 21:52
  • How can we know :D – Marcello Romani Aug 26 '21 at 22:02
  • well just add `yarn build` after `yarn install` If I do that I get an error because the barebone `package.json` I provided doesn't define a `build` command. – Marcello Romani Aug 26 '21 at 22:04
  • The problem is that I need dist folder to be in ./frontend and not inside the container – DreamingCodes Aug 26 '21 at 22:06
  • Dude! `command: sh -c "cd /app && yarn install && mkdir dist && node index.js"` Since `./frontend` is mapped to `/app` in the container, `cd /app && mkdir dist` will create the didrectory _inside the local `fronted` folder_ . – Marcello Romani Aug 26 '21 at 22:08
  • You need to review the Dockerfile build process and Docker volume mappings. Until you clarify the difference betewen a Docker image and a Docker container you'll get things messed up. :-) (Been there, done that ;-) ) – Marcello Romani Aug 26 '21 at 22:09
  • putting it as command does not mean that it will be executed at every boot? – DreamingCodes Aug 26 '21 at 22:13
  • What do you mean by `boot`? – Marcello Romani Aug 26 '21 at 22:19
  • A `CMD` will be executed every time a container from this image is started. Containers are inherently stateless, i.e. if we remove a container and start a new one wiith the same image, they have no knowledge of the previous container(s) (see [Cattle not pets](https://devops.stackexchange.com/questions/653/what-is-the-definition-of-cattle-not-pets)). If we want some behaviour to execute conditionally, we have to implement the condition ourselves (e.g. checking if the `dist` directory exists and only execute `yarn install` if it does not). – Turing85 Aug 26 '21 at 22:20
  • Yes, sorry boot is not the right term. I mean every time I run docker-compose up. Instead I would like it to be executed only the first time that I run the command. – DreamingCodes Aug 26 '21 at 22:22
  • 1
    Again: this is not a feature baked into `docker`, `Dockerfile` or `docker-compose`. If we want this behaviour, we have to implement the condition ourselves. There is no possibility to determine with 100% certainty if a container from an image was started in the past, so we have to find another "marker". As I said in my last comment, checking whether directory `dist` exists might work. – Turing85 Aug 26 '21 at 22:25
  • I still maintain that a `Dockerfile` is proobably not what you're after. The `Rust` example you provided doesn't use one, as a matter of fact. – Marcello Romani Aug 26 '21 at 22:26
  • Now that I think about it I will put restart: always to the containers that must always be active and I will ignore the problem. Also because I'll run docker-compose up -d I'll only once – DreamingCodes Aug 26 '21 at 22:27
0

Okay, my solution was to give up on using a Dockerfile and creating a container that build my project for me. It won't cause me any problems as I will only run docker-compose up -d once. Probably not the best solution but the only one that I have found.