I have a setup like this:
ARG TAG=latest
FROM node:${TAG} as typescript
ARG PORT=80
WORKDIR /app
COPY package.json .
COPY package-lock.json .
COPY .env .
COPY .swcrc.json .
COPY src .
RUN npm install --ignore-scripts
EXPOSE $PORT
CMD npm run dev
Along with:
version: '3.8'
services:
typescript:
build:
context: .
args:
TAG: ${TAG}
PORT: ${PORT}
image: typescript
container_name: typescript
restart: on-failure:10
ports:
- ${PORT}:${PORT}
volumes:
- ./src:/usr/app/src
And:
"dev": "swc src -d lib --config-file .swcrc.json -w"
And on my other project with webpack it does the exact same thing with:
"dev": "webpack serve"
Which usually just works in docker.
When I then build and run it using any combination of docker build or compose it always exits with code 0 and never stays open. I've found 10 stackoverflow questions and blog posts about this and everyone does weird hacks with tail and /dev/null oor tty: true or stdin, but I've never had to do anything like that to keep it open, usually it just works. The same thing is happening for webpack-dev-server as well, which also normally works perfect. I feel like I've been doing it this way for the longest time and suddenly it's not working any more?
What am I doing wrong?
Edit: I've also tried using yarn, same thing. And tried node:latest, node:16-alpine and node:18-alpine. All of them also just exists.
Edit: Image below shows what I see in docker desktop. I can see it compiling fine, logging fine like it normally does, running on the corect port, it seems as if everything is working correctly without errors, the only thing is that it doesn't stay open. The same for webpack-dev-server, also shows all of it correctly compiling and building and watching for file changes, then exits.
Edit: Here is the one for react, same thing, everything runs normal, compiles, starts up dev-server and waits for file changes, then exits with code 0.
CodePudding user response:
It looks like your container is initializing and immediately finishes his job.
Try to remove the CMD run dev
, manually exec the container and manually type CMD run dev
to debug this issue.
Also and even first of all, write this command at locally, maybe some code broke in your application.
Is your application PORT number declaration is hard coded for 80, or it is an environment variable?
If it's environment variable, you did not provide it as a variable.
CodePudding user response:
I finally managed to figure it out... Which now seems really stupid.
The important lines are WORKDIR /app
, COPY src .
and the fix:
volumes:
volumes:
- .:/app
- node_modules:/app/node_modules
volumes:
node_modules:
I must have been high when I made that first volume mount. So using the correct volume finally let it stay open the way it's supposed to.
I don't really know the exact detail of that, whether it's actually docker breaking, but a totally random guess would be that since both swc and webpack uses chokidar that when you mount the incorrect volume it probably can't find any files to watch and ends up exiting since chokidar won't have any watch files. But that's just a guess.