Skip to content

Instantly share code, notes, and snippets.

@airtonix
Last active August 11, 2018 05:16
Show Gist options
  • Select an option

  • Save airtonix/55e1d67ef803a887b411caed0f88e689 to your computer and use it in GitHub Desktop.

Select an option

Save airtonix/55e1d67ef803a887b411caed0f88e689 to your computer and use it in GitHub Desktop.
Random ports used by webpack-serve despite ports configured.
repo: https://github.com/airtonix/netlify-cms-widget-relations
It's also caused by the fact that you might have `onchange` wrapping your callto `webpack-serve`, which
leads to `onchange` sending SIGTERM instead of SIGINT to the childprocess it wraps.
The result is that `webpack-serve` remains running, thus keeps the intially configured port and the
next process uses a random port.
In a bare metal naive scenario this might not be a problem. But in docker, we expect that the process
will only use the port we configure.
Basically my scenario is that I use docker to make the development environment portable.
1. I wrap `webpack-serve` with `onchange`.
2. when certain files that are only read at bootup by webpack change, `onchange` runs it again
```
$ npm run dev
> [email protected] dev C:\Users\zenobius\Projects\Mine\netlify-cms-widget-relations
> npm run docker:serve -- as_server npm run container:dev:watch
> [email protected] docker:serve C:\Users\zenobius\Projects\Mine\netlify-cms-widget-relations
> npm run docker:run -- --service-ports "as_server" "npm" "run" "container:dev:watch"
> [email protected] docker:run C:\Users\zenobius\Projects\Mine\netlify-cms-widget-relations
> docker-compose run --rm "--service-ports" "as_server" "npm" "run" "container:dev:watch"
.start
.exec npm run container:dev:watch
> [email protected] container:dev:watch /app
> npx onchange "./package.json" "./webpack_config/**/*" -v -p -i -- npm run container:dev
onchange: watching ./package.json, ./webpack_config/**/*
onchange: executing command "npm run container:dev"
> [email protected] container:dev /app
> NODE_ENV=develop webpack-serve --config=./webpack_config --port=$PORT --host=$HOST
ℹ 「hot」: webpack: Compiling...
ℹ 「hot」: WebSocket Server Listening at 0.0.0.0:8081
[04:48:37] ℹ 「serve」: Project is running at http://0.0.0.0:3000
ℹ 「hot」: webpack: Compiling Done
ℹ 「wdm」: Hash: 9923769365c6eb286c9a
Version: webpack 4.6.0
Time: 13147ms
Built at: 2018-08-11 04:48:39
Asset Size Chunks Chunk Names
main.js 5.08 MiB main [emitted] main
index.html 180 bytes [emitted]
// snip lots of build info
onchange: change to package.json
onchange: killing command 31 and restarting
onchange: command completed with code 0
onchange: executing command "npm run container:dev"
```
Regardless of what I tell `onchange` to send as the kill signal `webpack-serve` continues to use a random port next time around.
```
//package.json
...
"serve": {
"hmr": true,
"hotClient": {
"host": "localhost"
},
"dev": {
"watchOptions": {
"poll": 300
}
},
"clipboard": false,
"logTime": true,
"logLevel": "info",
"openPath": "/#/collections/test/entries/test"
},
"scripts": {
"[ workstation/container: commands ]": "",
"container:dev:watch": "npx onchange \"./package.json\" \"./webpack_config/**/*\" -v -p -i -- npm run container:dev",
"container:dev": "NODE_ENV=develop webpack-serve --config=./webpack_config --port=$PORT --host=$HOST",
"container:demo": "NODE_ENV=production webpack --config=./webpack_config --display errors-only --devtool source-map",
"container:prod": "NODE_ENV=production webpack",
"[ workstation: docker commands ]": "",
"docker:serve": "npm run docker:run -- --service-ports",
"docker:run": "docker-compose run --rm",
"docker:build": "docker-compose -f ./docker-compose--build.yml build image",
"docker:shell": "npm run docker:run -- as_builder /bin/sh",
"[ workstation: dev commands ]": "",
"dev": "npm run docker:serve -- as_server npm run container:dev:watch",
"prod": "npm run docker:run -- as_builder npm run container:prod",
"prepublishOnly": "npm run docker:run -- as_builder npm run container:prod"
}
...
```
```
# Dockerfile
FROM node:8.9.0-alpine as builder
WORKDIR /build
RUN apk add --no-cache \
autoconf \
automake \
ca-certificates \
curl \
g++ \
gcc \
git \
jpeg-dev \
jq \
make \
nasm \
openssh \
openssl \
python \
run-parts \
zlib-dev \
&& update-ca-certificates
RUN npm install -g node-gyp
COPY ./package.json .
COPY ./yarn.lock .
RUN yarn install
FROM node:8.9.0-alpine
WORKDIR /app
COPY --from=builder /build/node_modules /app/node_modules
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD [ "npm", "run", "container:start" ]
```
```
# docker-compose.yml
version: "2.0"
services:
image:
image: airtonix/dev-${npm_package_name}:${npm_package_version}
as_builder:
extends: {service: "image"}
volumes:
- "./:/app/"
- "/app/node_modules"
as_server:
extends: {service: "as_builder"}
environment:
- HOST=0.0.0.0
- PORT=3000
ports:
- "3000:3000"
- "8081:8081"
```
```
//docker-compose--build.yml
version: "2.0"
services:
image:
build: .
image: airtonix/dev-${npm_package_name}:${npm_package_version}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment