This is a tutorial for using Docker to containerize your web application. It is intended for students in CMSC389T at the University of Maryland.
A Dockerfile is a text document containing all the commands a user could call to assemble an image. Here's a breakdown of each part:
FROM node:10-alpine- Specifies the base image for your Docker image. In this case,
node:10-alpineis a lightweight version of the Node.js 10 image.
RUN mkdir -p /home/node/app && chown -R node:node /home/node/app- Executes shell commands in the container. Here, it creates a directory
/home/node/appand sets its owner to thenodeuser.
WORKDIR /home/node/app- Sets the working directory for the subsequent Dockerfile commands.
COPY package*.json ./- Copies
package.jsonandpackage-lock.json(if available) from your project directory into the container.
USER node- Switches to the
nodeuser for security purposes.
RUN npm install- Installs the dependencies defined in
package.json.
EXPOSE 8080- Informs Docker that the container listens on port 8080 at runtime.
COPY --chown=node:node . .- Copies your app's source code into the container, setting the
nodeuser as the owner.
CMD node app.js- Specifies the command to run the app. In this case, it's starting a Node.js app.
The docker-compose.yml file defines the services, networks, and volumes for your Docker application.
- Builds and configures the Node.js service. The
buildsection specifies the Dockerfile and context for building the image. Theimageandcontainer_nameprovide names for the image and the container, respectively.
- Configures the Nginx web server. It uses the
nginx:mainline-alpineimage, sets container parameters, and defines volume mappings and dependencies.
- Persistent data storage configurations. The
web-rootvolume is configured with local driver options, including the absolute path to your web template folder.
- Defines the network configuration. In this case, a bridge network is used for inter-container communication.
You can use a diagramming tool like Mermaid to illustrate the architecture. For example:
graph LR
A[Browser] -- HTTP Requests --> B{Nginx Container}
B --> C[Node.js Container]
C --> D{Node App}
This shows the flow from the user's browser through Nginx to the Node.js app.
If you have gotten to the bottom of this README, thank you for reading, and as a reward, note that the docker-compose.yml and Dockerfile files that are within this repo are the ones that I used when I created this project (so they are tested and work). Meaning that you could just copy and paste them into your own project and they should work with the exception of the absolute path to the web template folder in the docker-compose.yml file. You will need to change that to the absolute path to your web template folder.