I study containers. For it, I implement it in a particular project, available on this link. When I type the command docker-compose up, an error log below pops out. I can run the command npm start without problems.
Attaching to nodejs
nodejs | [nodemon] 2.0.15
nodejs | [nodemon] to restart at any time, enter `rs`
nodejs | [nodemon] watching path(s): *.*
nodejs | [nodemon] watching extensions: js,mjs,json
nodejs | [nodemon] starting `node app.js ./src/index.js`
nodejs | /home/node/app/src/index.js:2
nodejs | import express from 'express';
nodejs | ^^^^^^^
nodejs |
nodejs | SyntaxError: Unexpected identifier
nodejs | at Module._compile (internal/modules/cjs/loader.js:723:23)
nodejs | at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
nodejs | at Module.load (internal/modules/cjs/loader.js:653:32)
nodejs | at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
nodejs | at Function.Module._load (internal/modules/cjs/loader.js:585:3)
nodejs | at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
nodejs | at startup (internal/bootstrap/node.js:283:19)
nodejs | at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
nodejs | [nodemon] app crashed - waiting for file changes before starting...
Related
I am dockerizing a system that uses a pem and key to authenticate with a remote dev server. Unfortunately, it would appear that it is unable to find the file despite it being in the docker image.
Error
server_1 | Error: ENOENT: no such file or directory, open '/app/certificates/jwt.key'
server_1 | at Object.openSync (fs.js:497:3)
server_1 | at Object.readFileSync (fs.js:393:35)
server_1 | at Object.<anonymous> (/app/controllers/user.js:7:16)
server_1 | at Module._compile (internal/modules/cjs/loader.js:1085:14)
server_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
server_1 | at Module.load (internal/modules/cjs/loader.js:950:32)
server_1 | at Function.Module._load (internal/modules/cjs/loader.js:790:12)
server_1 | at Module.require (internal/modules/cjs/loader.js:974:19)
server_1 | at require (internal/modules/cjs/helpers.js:101:18)
server_1 | at Object.<anonymous> (/app/routes/upload.js:8:24)
server_1 | at Module._compile (internal/modules/cjs/loader.js:1085:14)
server_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
server_1 | at Module.load (internal/modules/cjs/loader.js:950:32)
server_1 | at Function.Module._load (internal/modules/cjs/loader.js:790:12)
server_1 | at Module.require (internal/modules/cjs/loader.js:974:19)
server_1 | at require (internal/modules/cjs/helpers.js:101:18) {
server_1 | errno: -2,
server_1 | syscall: 'open',
server_1 | code: 'ENOENT',
server_1 | path: '/app/certificates/jwt.key'
server_1 | }
Line (server/controllers/user.js -> app/controllers/user.js)
const pem = fs.readFileSync(path.resolve(__dirname, "../certificates/jwt.key"));
Dockerfile
FROM node:14
RUN mkdir app
WORKDIR /app
COPY . .
RUN npm install
RUN npm run get_key
EXPOSE 5000
CMD [ "npm", "run", "start" ]
package.json
"scripts": {
"test": "mocha -r dotenv/config ./**/*.test.js",
"start": "node -r dotenv/config server.js",
"dev": "nodemon -r dotenv/config server.js",
"get_key": "ssh-keygen -t rsa -b 4096 -m PEM -f ./certificates/jwt.key -P ''"
},
The volume mount was overriding the key building. Need to generate the key locally first before composing.
server:
build: server
ports:
- "5000:5000"
restart: always
depends_on:
- mongodb
volumes:
- ./server:/app
I have a Docker installation that I would like to start with docker compose up (and not have to run 2 extra ttys ) so I added a Procfile.dev looking like this
web: bin/rails server -p 3000 -b '0.0.0.0'
js: yarn build_js --watch
css: yarn build_css --watch
The output is, however, less than enjoyable
√ mindling % docker compose up
[+] Running 3/0
⠿ Container mindling_redis Running 0.0s
⠿ Container mindling_db Running 0.0s
⠿ Container mindling_mindling_1 Created 0.0s
Attaching to mindling_db, mindling_1, mindling_redis
mindling_1 | 19:54:04 web.1 | started with pid 16
mindling_1 | 19:54:04 js.1 | started with pid 19
mindling_1 | 19:54:04 css.1 | started with pid 22
mindling_1 | 19:54:06 css.1 | yarn run v1.22.17
mindling_1 | 19:54:06 js.1 | yarn run v1.22.17
mindling_1 | 19:54:06 js.1 | $ esbuild app/javascript/*.* --bundle --outdir=app/assets/builds --watch
mindling_1 | 19:54:06 css.1 | $ tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --watch
mindling_1 | 19:54:08 js.1 | Done in 2.02s.
mindling_1 | 19:54:08 js.1 | exited with code 0
mindling_1 | 19:54:08 system | sending SIGTERM to all processes
mindling_1 | 19:54:08 web.1 | terminated by SIGTERM
mindling_1 | 19:54:09 css.1 | terminated by SIGTERM
mindling_1 exited with code 0
I've tried running a Bash in the application container - and calling the Procfile in a tty by itself looks more or less like this:
root#facfb249dc6b:/app# foreman start -f Procfile.dev
20:11:45 web.1 | started with pid 12
20:11:45 js.1 | started with pid 15
20:11:45 css.1 | started with pid 18
20:11:48 css.1 | yarn run v1.22.17
20:11:48 js.1 | yarn run v1.22.17
20:11:48 css.1 | $ tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --watch
20:11:49 js.1 | $ esbuild app/javascript/*.* --bundle --outdir=app/assets/builds --watch
20:11:50 js.1 | [watch] build finished, watching for changes...
20:11:53 web.1 | => Booting Puma
20:11:53 web.1 | => Rails 7.0.0 application starting in development
20:11:53 web.1 | => Run `bin/rails server --help` for more startup options
20:11:57 web.1 | Puma starting in single mode...
20:11:57 web.1 | * Puma version: 5.5.2 (ruby 3.0.3-p157) ("Zawgyi")
20:11:57 web.1 | * Min threads: 5
20:11:57 web.1 | * Max threads: 5
20:11:57 web.1 | * Environment: development
20:11:57 web.1 | * PID: 22
20:11:57 web.1 | * Listening on http://0.0.0.0:3000
20:11:57 web.1 | Use Ctrl-C to stop
20:11:58 css.1 |
20:11:58 css.1 | Rebuilding...
20:11:59 css.1 | Done in 1066ms.
^C20:13:23 system | SIGINT received, starting shutdown
20:13:23 web.1 | - Gracefully stopping, waiting for requests to finish
20:13:23 web.1 | === puma shutdown: 2021-12-22 20:13:23 +0000 ===
20:13:23 web.1 | - Goodbye!
20:13:23 web.1 | Exiting
20:13:24 system | sending SIGTERM to all processes
20:13:25 web.1 | terminated by SIGINT
20:13:25 js.1 | terminated by SIGINT
20:13:25 css.1 | terminated by SIGINT
root#facfb249dc6b:/app#
What is going on? It works when doing it 'by hand' but if I let docker-compose rip the processes somehow terminates!?!
I have isolated the issue to the build_css script in package.json (or at least it does keep going if I comment that line in the Procfile.dev)
All the 'dirty linen'
My package.json looks like this
{
...8<...
"scripts": {
"build_js": "esbuild app/javascript/*.* --bundle --outdir=app/assets/builds",
"build_css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css"
},
...8<...
}
My containers are exceptionally boring, looking like almost everybody else's:
FROM ruby:3.0.3
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y nodejs yarn
WORKDIR /app
COPY src/Gemfile /app/Gemfile
COPY src/Gemfile.lock /app/Gemfile.lock
RUN gem install bundler foreman && bundle install
EXPOSE 3000
ENTRYPOINT [ "entrypoint.sh" ]
version: "3.9"
db:
build: mysql
image: mindling_db
container_name: mindling_db
command: [ "--default-authentication-plugin=mysql_native_password" ]
ports:
- "3306:3306"
volumes:
- ~/src/mysql_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: mindling_development
mindling:
platform: linux/x86_64
build: .
volumes:
- ./src:/app
ports:
- "3000:3000"
depends_on:
- db
and finally my entrypoint.sh
#!/usr/bin/env bash
rm -rf /app/tmp/pids/server.pid
foreman start -f Procfile.dev
Allow me to give credit to they who deserve it!! The correct answer was provided by earlopain in this issue on rails/rails
It's actually an almost embarrassingly easy fix - once you know it :)
Add tty: true to your docker-compose.yml - like this
mindling:
platform: linux/x86_64
build: .
tty: true
volumes:
- ./src:/app
ports:
- "3000:3000"
depends_on:
- db
Thanks Earlopain & #walt_die, you saved my day. Writing this answer because I had a bit of explanation which didn't fit in the comment.
Just like yours, when trying to run rails in docker using docker-compose the problem I was facing was that CMD bin/dev in dockerfile was constantly crashing, although it worked when ran manually via bash.
The issue was not with tailwindcss but esbuild instead. This line js: yarn build --watch in Procfile.dev was failing because it runs esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=assets under the hood, and as mentioned by evanw in esbuild issue esbuild exits when stdin is closed.
So, the solution of adding tty: true to docker-compose.yml as above works.
Alternatively, one can also remove/comment out this line js: yarn build --watch from Procfile.dev works. But this won't compile the JS changes. So, one can jump into bash of the running container and manually run yarn build --watch
I am trying to run my parse server app in docker, but when running the command 'docker-compose up' it runs fine for some seconds and then throws this error:
Error parsing command line: unrecognised option '--break-mongo'.
and after this it also throws an npm related error --
my-parse-app |
my-parse-app | > app#1.0.0 start /parse-server
my-parse-app | > parse-server "/parse-server/config/config.json"
my-parse-app |
my-parse-app |
/parse-server/node_modules/parse/lib/node/ParseObject.js:1301
my-parse-app | async exists(options
my-parse-app | ^^^^^^
my-parse-app |
my-parse-app | SyntaxError: Unexpected identifier
my-parse-app | at createScript (vm.js:56:10)
my-parse-app | at Object.runInThisContext (vm.js:97:10)
my-parse-app | at Module._compile (module.js:549:28)
my-parse-app | at Object.Module._extensions..js (module.js:586:10)
my-parse-app | at Module.load (module.js:494:32)
my-parse-app | at tryModuleLoad (module.js:453:12)
my-parse-app | at Function.Module._load (module.js:445:3)
my-parse-app | at Module.require (module.js:504:17)
my-parse-app | at require (internal/module.js:20:19)
my-parse-app | at Object.
(/parse-server/node_modules/parse/lib/node/ParseRole.js:12:43)
my-parse-app |
my-parse-app | npm ERR! Linux 4.19.128-microsoft-standard
my-parse-app | npm ERR! argv "/usr/local/bin/node"
"/usr/local/bin/npm" "start" "--" "/parse-server/config/config.json"
my-parse-app | npm ERR! node v6.17.1
my-parse-app | npm ERR! npm v3.10.10
my-parse-app | npm ERR! code ELIFECYCLE
my-parse-app | npm ERR! app#1.0.0 start: parse-server "/parse-server/config/config.json"
my-parse-app | npm ERR! Exit status 1
my-parse-app | npm ERR!
my-parse-app | npm ERR! Failed at the app#1.0.0 start script
'parse-server "/parse-server/config/config.json"'.
my-parse-app | npm ERR! Make sure you have the latest version of
node.js and npm installed.
my-parse-app | npm ERR! If you do, this is most likely a problem with
the app package,
my-parse-app | npm ERR! not with npm itself.
my-parse-app | npm ERR! Tell the author that this fails on your
system:
my-parse-app | npm ERR! parse-server
"/parse-server/config/config.json"
my-parse-app | npm ERR! You can get information on how to open an
issue for this project with:
my-parse-app | npm ERR! npm bugs app
my-parse-app | npm ERR! Or if that isn't available, you can get their
info via:
my-parse-app | npm ERR! npm owner ls app
my-parse-app | npm ERR! There is likely additional logging output
above.
my-parse-app |
my-parse-app | npm ERR! Please include the following file with any
support request:
my-parse-app | npm ERR! /parse-server/npm-debug.log
my-parse-app exited with code 1
My docker-compose:
version: "2"
services:
# Node.js parse-server application image
app:
build: ./app
command: npm start -- /parse-server/config/config.json
container_name: my-parse-app
volumes:
- ./app:/parse-server/
- /parse-server/node_modules
ports:
- "1337:1337"
links:
- mongo
# MongoDB image
mongo:
image: mongo:latest
container_name: mongo-database
ports:
- "27017:27017"
volumes_from:
- mongodata
# MongoDB image volume for persistence
mongodata:
image: mongo
volumes:
- ./data/db:/data/db
command:
- --break-mongo
My Dockerfile
FROM node:boron
RUN mkdir -p /parse-server
WORKDIR /parse-server
COPY . /parse-server
RUN npm install
EXPOSE 1337
CMD ["npm","start"]
The break-mongo error
The npm error
It looks you are using an old version of node and it is probably the problem. Switch the first line of your docker file to:
FROM node:erbium
I had an existing Ionic app which I have dockerized. The build and up commands are successful and I can access the app at http://localhost:8100/ionic-lab. However, hot reload doesn't work. Whenever I edit an HTML or CSS, those changes are nor reflected.
My dockerfile:
FROM node:8
COPY package.json /opt/library/
WORKDIR /opt/library
RUN npm install -g cordova ionic && cordova telemetry off
# && echo n | ionic start dockerized-ionic-app --skip-npm --v2 --ts
RUN npm install && npm cache verify
COPY . /opt/library
#CMD ["ionic", "serve", "--all"]
And docker-compose.yml:
app:
build: .
ports:
- '8100:8100'
- '35729:35729'
volumes:
- .:/opt/library
- /opt/library/node_modules
command: ionic serve --lab
Why is it happening? What is missing?
UPDATE:
Output of docker-compose build --no-cache
D:\Development\personal_projects\library>docker-compose build --no-cache
Building app
Step 1/6 : FROM node:8
---> b87c2ad8344d Step 2/6 : COPY package.json /opt/library/
---> 4422d0333b92
Step 3/6 : WORKDIR /opt/library
Removing intermediate container 1cfdd60477f9 ---> 1ca3dc5f5bd6 Step 4/6 : RUN npm install -g cordova ionic && cordova telemetry off
---> Running in d7e9bf4e6d7b
/usr/local/bin/cordova -> /usr/local/lib/node_modules/cordova/bin/cordova
/usr/local/bin/ionic -> /usr/local/lib/node_modules/ionic/bin/ionic
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents#1.1.3 (node_modules/ionic/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents#1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
+ cordova#8.0.0
+ ionic#3.19.1
added 660 packages in 29.173s
You have been opted out of telemetry. To change this, run: cordova telemetry on.
Removing intermediate container d7e9bf4e6d7b
---> 3fedee0878af
Step 5/6 : RUN npm install && npm cache verify
---> Running in 8d482b23f6bb
> node-sass#4.5.3 install /opt/library/node_modules/node-sass
> node scripts/install.js
Downloading binary from https://github.com/sass/node-sass/releases/download/v4.5.3/linux-x64-57_binding.node
Download complete
Binary saved to /opt/library/node_modules/node-sass/vendor/linux-x64-57/binding.node
Caching binary to /root/.npm/node-sass/4.5.3/linux-x64-57_binding.node
> uglifyjs-webpack-plugin#0.4.6 postinstall /opt/library/node_modules/uglifyjs-webpack-plugin
> node lib/post_install.js
> node-sass#4.5.3 postinstall /opt/library/node_modules/node-sass
> node scripts/build.js
Binary found at /opt/library/node_modules/node-sass/vendor/linux-x64-57/binding.node
Testing binary
Binary is fine
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents#1.1.3 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents#1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
added 548 packages in 30.281s
Cache verified and compressed (~/.npm/_cacache):
Content verified: 1476 (55779072 bytes)
Index entries: 2306
Finished in 9.736s
Removing intermediate container 8d482b23f6bb
---> 5815e391f2c6
Step 6/6 : COPY . /opt/library
---> 5cc9637a678c
Successfully built 5cc9637a678c
Successfully tagged library_app:latest
D:\Development\personal_projects\library>
And output of docker-compose up:
D:\Development\personal_projects\library>docker-compose up
Recreating library_app_1 ... done
Attaching to library_app_1
Starting app-scripts server: --address 0.0.0.0 --port 8100 --livereload-port 35729 --dev-logger-port 53703 --nobrowser --lab - Ctrl+C to cancel
app_1 | [14:45:19] watch started ...
app_1 | [14:45:19] build dev started ...
app_1 | [14:45:19] clean started ...
app_1 | [14:45:19] clean finished in 78 ms
app_1 | [14:45:19] copy started ...
app_1 | [14:45:19] deeplinks started ...
app_1 | [14:45:20] deeplinks finished in 60 ms
app_1 | [14:45:20] transpile started ...
app_1 | [14:45:24] transpile finished in 4.54 s
app_1 | [14:45:24] preprocess started ...
app_1 | [14:45:24] preprocess finished in 1 ms
app_1 | [14:45:24] webpack started ...
app_1 | [14:45:24] copy finished in 5.33 s
app_1 | [14:45:31] webpack finished in 6.73 s
app_1 | [14:45:31] sass started ...
app_1 | [14:45:32] sass finished in 1.46 s
app_1 | [14:45:32] postprocess started ...
app_1 | [14:45:32] postprocess finished in 40 ms
app_1 | [14:45:32] lint started ...
app_1 | [14:45:32] build dev finished in 13.64 s
app_1 | [14:45:32] watch ready in 13.73 s
app_1 | [14:45:32] dev server running: http://localhost:8100/
app_1 |
[OK] Development server running!
app_1 | Local: http://localhost:8100
app_1 | External: http://172.17.0.2:8100
app_1 | DevApp: library#8100 on 1643dcb6c0d7
app_1 |
app_1 | [14:45:35] lint finished in 2.51 s
Your Dockerfile and Docker-Compose does exactly what is needed.
With the - .:/opt/library line the volume gets mounted correctly and your local changes will take effect in the container as well.
If you are on Windows the problem is that Hyper-V is not capable of propagating local file changes correctly into the container. Therefore the serve program is not able to catch file changes.
The solution is to use ng serve directly and enable polling by running ng serve with the poll flag: ng serve --poll 200 --host=0.0.0.0 --port=8100.
--poll 200 is looking actively for file changes every 200ms
--host=0.0.0.0 set the host. 0.0.0.0 is used to be reachable from other containers
--port=8100 is used to get the same port as ionic serve uses (just for convinience)
You said "hot reload doesn't work", this is correct.
if you re-build docker container then only you will see code changes, because your source code needs to get copy inside your docker-container.
just run docker-compose up -d or rebuild docker container then you should see your code changes.
You are mapping local 8100 port with cointainer 8100 port, this is ok. You are running ionic from a container, in an External way.
Try with “ionic serve --external”
So, I updated npm inside docker container and since I have node version 6.9.4, npm is broken. Any npm commands will throw this error.
client | module.js:471
client | throw err;
client | ^
client |
client | Error: Cannot find module 'process-nextick-args'
client | at Function.Module._resolveFilename (module.js:469:15)
client | at Function.Module._load (module.js:417:25)
client | at Module.require (module.js:497:17)
client | at require (internal/module.js:20:19)
client | at Object.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/readable-stream/lib/_stream_readable.js:26:23)
client | at Module._compile (module.js:570:32)
client | at Object.Module._extensions..js (module.js:579:10)
client | at Module.load (module.js:487:32)
client | at tryModuleLoad (module.js:446:12)
client | at Function.Module._load (module.js:438:3)
and I would like to downgrade npm to 3.10.10.
Is there any way to do this without recreating the container?
I'm using docker-compose.