I was trying to Dockerize my meteor app and felt somebody could benefit. I am using alpine linux and volumes to share the runtime parameters for the meteor app and pm2 to run it. By using 2 stage build process the image size is reduced. I am sure the final image size can be further reduced if pm2 is not used. Please do share if you make this better.
Prereq - You have docker installed in your system
Steps
- Create a folder to store Dockerfile and build output of meter
- Use “meteor build” and copy the entire build directory to the newly created folder
- Create Dockerfile file in this folder and copy the following content to it.
# Pull base image.
FROM mhart/alpine-node:8
# Install build tools to compile native npm modules
RUN apk update \
&& apk upgrade \
&& apk add build-base python bind-tools libxml2-utils libxslt
# Create app directory
RUN mkdir -p /app
# Copy meteor build bundle
COPY bundle /app
# Build for the image
RUN cd /app/programs/server && npm install --unsafe-perm
FROM keymetrics/pm2:8-alpine
RUN mkdir -p /app
RUN mkdir -p /usr/pm2
COPY --from=0 /usr/bin/node /usr/bin/
COPY --from=0 /usr/lib/libgcc* /usr/lib/libstdc* /usr/lib/
COPY --from=0 /app /app
EXPOSE 3000
CMD ["pm2-runtime", "/usr/pm2/pm2.json"]
- Build Docker image replacing meteorapp with your appname if you want
docker build -t meteorapp .
- Now you need to create the pm2.json file for running. Make sure you add or change what is needed for running your app - sample pm2.json below
{
"apps": [
{
"name": "app1",
"cwd": "/app", // do not change as it should be same as defined on image
"script": "main.js",
"env": {
"NODE_ENV": "production",
"WORKER_ID": "0",
"PORT": "3000", // 3000 for now as that is exposed
"ROOT_URL": "https://XXX.XXX.XXX",
"MONGO_URL": "mongodb://xxx.xxx.xx.xxx:27017/meteor",
"MONGO_OPLOG_URL": "mongodb://xxx.xxx.xx.xxx:27017/local",
"HTTP_FORWARDED_COUNT": "1",
"MAIL_URL": "smtp://xxx.xxx.xxx",
"METEOR_SETTINGS": {
"public": {
},
"private": {
}
}
}
}
]
}
If your build was successful, you can now run it by issuing command (hopefully Mongo server is accessible somewhere)
docker run -p 3000:3000 -v absolutePathOfPM2File:/usr/pm2 -t meteorapp
Hopefully this is useful to somebody.