- learning docker with node.js via https://www.youtube.com/watch?v=9zUHg7xjIqQ
A basic docker script for a node.js app:
# Mandatory, every dockerfile must be based on another image
FROM node:16
# This sets the current working dir, all further action will be made relative to this path
WORKDIR /app
# We copy the package.json and install before deps first for performance reasons.
# This is the longest step so dependencies are cache for subsequent steps
COPY package.json.
RUN npm install
# We can now copy the rest of the source code across, we don't need re-run npm install as dependencies are installed already
# '. .' looks kinda confusing. This just means copy everything from dev working dir to the docker working dir
COPY . .
# This doesnt actually do anything and acts more as documentation that port 3000 is being exposed and can be port forwarded for external access
EXPOSE 3000
# run the bash commands that will start the app
CMD ['node', "index.js"]
-
docker caches these steps so a second run will generally be faster
-
to build an image run
docker build . -t node-app-image
.
is the path of where the Dockerfile is
-t
the name of of the image
to run a built image
docker run -p 3000:3000 -d --name node-app node-app-image
p 3000:3000
is how we forward ports from the host machines to correct exposed port on the docker container.- port on left === port being accessed on host machine
- port on right === port to direct to on docker container