The app I'm making is written in ES6 and other goodies is transpiled by webpack inside a Docker container. At the moment everything works from creating the inner directory, installing dependencies, and creating the compiled bundle file.
我正在制作的应用程序是用ES6编写的,其他好东西是由Docker容器中的webpack编写的。目前,一切都可以从创建内部目录,安装依赖项和创建编译的bundle文件开始。
When running the container instead, it says that dist/bundle.js does not exist. Except if I create the bundle file in the host directory, it will work.
相反,当运行容器时,它表示dist / bundle.js不存在。除非我在主机目录中创建捆绑文件,否则它将起作用。
I've tried creating a volume for the dist directory at it works the first time, but after making changes and rebuilding it does not pick up the new changes.
我已经尝试为dist目录创建一个卷,它第一次工作,但在进行更改和重建之后它没有获取新的更改。
What I'm trying to achieve is having the container build and run the compiled bundle. I'm not sure if the webpack part should be in the Dockerfile as a build step or at runtime since the CMD ["yarn", "start"]
crashes but RUN ["yarn", "start"]
works.
我想要实现的是让容器构建并运行已编译的bundle。我不确定webpack部分是作为构建步骤在Dockerfile中还是在运行时,因为CMD [“yarn”,“start”]崩溃但是RUN [“yarn”,“start”]起作用。
Any suggestions ands help is appreciated. Thanks in advance.
任何建议和帮助表示赞赏。提前致谢。
|_src
|_index.js
|_dist
|_bundle.js
|_Dockerfile
|_.dockerignore
|_docker-compose.yml
|_webpack.config.js
|_package.json
|_yarn.lock
docker-compose.yml
泊坞窗,compose.yml
version: "3.3"
services:
server:
build: .
image: selina-server
volumes:
- ./:/usr/app/selina-server
- /usr/app/selina-server/node_modules
# - /usr/app/selina-server/dist
ports:
- 3000:3000
Dockerfile
Dockerfile
FROM node:latest
LABEL version="1.0"
LABEL description="This is the Selina server Docker image."
LABEL maintainer="AJ alvaroo@selina.com"
WORKDIR "/tmp"
COPY ["package.json", "yarn.lock*", "./"]
RUN ["yarn"]
WORKDIR "/usr/app/selina-server"
RUN ["ln", "-s", "/tmp/node_modules"]
COPY [".", "./"]
RUN ["yarn", "run", "build"]
EXPOSE 3000
CMD ["yarn", "start"]
.dockerignore
.dockerignore
.git
.gitignore
node_modules
npm-debug.log
dist
package.json
的package.json
{
"scripts": {
"build": "webpack",
"start": "node dist/bundle.js"
}
}
2 个解决方案
#1
0
I haven't included my src tree structure but its basically identical to yours, I use the following docker setup to get it to run and its how we dev stuff every day.
我没有包含我的src树结构,但它基本上与你的相同,我使用以下docker设置来运行它以及我们每天开发的东西。
In package.json we have
在package.json中我们有
"scripts": {
"start": "npm run lint-ts && npm run lint-scss && webpack-dev-server --inline --progress --port 6868",
}
dockerfile
dockerfile
FROM node:8.11.3-alpine
WORKDIR /usr/app
COPY package.json .npmrc ./
RUN mkdir -p /home/node/.cache/yarn && \
chmod -R 0755 /home/node/.cache && \
chown -R node:node /home/node && \
apk --no-cache add \
g++ gcc libgcc libstdc++ make python
COPY . .
EXPOSE 6868
ENTRYPOINT [ "/bin/ash" ]
docker-compose.yml version: "3"
docker-compose.yml版本:“3”
volumes:
yarn:
services:
web:
user: "1000:1000"
build:
context: .
args:
- http_proxy
- https_proxy
- no_proxy
container_name: "some-app"
command: -c "npm config set proxy=$http_proxy && npm run start"
volumes:
- .:/usr/app/
ports:
- "6868:6868"
Please note this Dockerfile is not suitable for production it's for a dev environment as its running stuff as root.
请注意,这个Dockerfile不适合用于开发环境,因为它以root身份运行。
With this docker file there its a gotcha.
有了这个docker文件,就会有一个问题。
Because alpine is on musl and we are on glib if we install node modules on the host the compiled natives won't work on the docker container, Once the container is up if you get an error we run this to fix it (its a bit of a sticking plaster right now)
因为alpine是在musl上我们在glib上如果我们在主机上安装节点模块,编译的本机将无法在docker容器上工作,一旦容器启动,如果你收到错误我们运行它来修复它(它有点现在贴膏药)
docker-compose exec container_name_goes_here /bin/ash -c "npm rebuild node-sass --force"
ikky but it works.
ikky,但它的工作原理。
#2
0
Try changing your start script in the package.json
to perform the build first (doing this, you won't need the RUN
command to perform the build in your Dockerfile
:
尝试更改package.json中的启动脚本以首先执行构建(这样做,您将不需要RUN命令来执行Dockerfile中的构建:
{
"scripts": {
"build": "webpack",
"start": "webpack && node dist/bundle.js"
}
}
#1
0
I haven't included my src tree structure but its basically identical to yours, I use the following docker setup to get it to run and its how we dev stuff every day.
我没有包含我的src树结构,但它基本上与你的相同,我使用以下docker设置来运行它以及我们每天开发的东西。
In package.json we have
在package.json中我们有
"scripts": {
"start": "npm run lint-ts && npm run lint-scss && webpack-dev-server --inline --progress --port 6868",
}
dockerfile
dockerfile
FROM node:8.11.3-alpine
WORKDIR /usr/app
COPY package.json .npmrc ./
RUN mkdir -p /home/node/.cache/yarn && \
chmod -R 0755 /home/node/.cache && \
chown -R node:node /home/node && \
apk --no-cache add \
g++ gcc libgcc libstdc++ make python
COPY . .
EXPOSE 6868
ENTRYPOINT [ "/bin/ash" ]
docker-compose.yml version: "3"
docker-compose.yml版本:“3”
volumes:
yarn:
services:
web:
user: "1000:1000"
build:
context: .
args:
- http_proxy
- https_proxy
- no_proxy
container_name: "some-app"
command: -c "npm config set proxy=$http_proxy && npm run start"
volumes:
- .:/usr/app/
ports:
- "6868:6868"
Please note this Dockerfile is not suitable for production it's for a dev environment as its running stuff as root.
请注意,这个Dockerfile不适合用于开发环境,因为它以root身份运行。
With this docker file there its a gotcha.
有了这个docker文件,就会有一个问题。
Because alpine is on musl and we are on glib if we install node modules on the host the compiled natives won't work on the docker container, Once the container is up if you get an error we run this to fix it (its a bit of a sticking plaster right now)
因为alpine是在musl上我们在glib上如果我们在主机上安装节点模块,编译的本机将无法在docker容器上工作,一旦容器启动,如果你收到错误我们运行它来修复它(它有点现在贴膏药)
docker-compose exec container_name_goes_here /bin/ash -c "npm rebuild node-sass --force"
ikky but it works.
ikky,但它的工作原理。
#2
0
Try changing your start script in the package.json
to perform the build first (doing this, you won't need the RUN
command to perform the build in your Dockerfile
:
尝试更改package.json中的启动脚本以首先执行构建(这样做,您将不需要RUN命令来执行Dockerfile中的构建:
{
"scripts": {
"build": "webpack",
"start": "webpack && node dist/bundle.js"
}
}