Docker Lesson 4 — Making Real Project with Docker

Yao-Shang Tseng
Sep 2, 2018 · 2 min read

It’s the note for Docker and Kubernetes: The Complete Guide
https://www.udemy.com/docker-and-kubernetes-the-complete-guide/

Build a project in NodeJS

1st. Create Node JS web app
2nd. Create a Dockerfile
3rd. Build image from dockerfile
4th. Run image as container
5th. Connect to web app from a browser
--Node JS Apps
-> Have to install dependencies before running the app
-> Install dependencies by running 'npm install'
-> Assumes 'npm' is installed!
-> Have to run a command to start up the server
-> Start server by running 'npm start'
-> Assumes 'npm' is installed!
--NodeFROM alpine
RUN npm install
CMD ["npm", "start"]
Step 2/3 : RUN npm install
---> Running in e37132f728e8
/bin/sh: npm: not found
it means alpine has no npm command in pre-install.--FROM node:alpine
RUN npm install
CMD ["npm", "start"]
Step 2/3 : RUN npm install
---> Running in 08469d20b045
npm WARN saveError ENOENT: no such file or directory, open '/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/package.json'
npm WARN !invalid#2 No description
npm WARN !invalid#2 No repository field.
npm WARN !invalid#2 No README data
npm WARN !invalid#2 No license field.
it means it cannot access package.json--COPY
# first ./ -> Path to folder to copy from on *your machine* relative to build context
# second ./ -> Place to copy stuff to inside *the container*
COPY ./ ./--# Specify a base image
FROM node:alpine
# Install some dependencies
COPY ./ ./
RUN npm install
# Default command
CMD ["npm", "start"]
$ docker build -t yakushou730/simpleweb .無法連上這個網站
localhost 拒絕連線。
ERR_CONNECTION_REFUSED
it means the network is not connected.--Docker Run with Port Mapping
# first 8080 -> Route incoming requests to this port on local host to ...
# second 8080 -> ...this port inside the container
$ docker run -p 8080:8080 <image name>EXAMPLE: $ docker run -p 8080:8080 yakushou730/simpleweb--WORKDIR
# /usr/app -> Any following command will be executed relative to this path in the container
WORKDIR /usr/app[~] ♨ docker exec -it deb579715103 sh
/usr/app # ls
Dockerfile index.js node_modules package-lock.json package.json

index.jsconst express = require('express');const app = express();app.get('/', (req, res) => {  res.send('Hi, Carol');});app.listen(8080, () => {  console.log('Listening on port 8080');});package.json{  "dependencies": {    "express": "*"  },  "scripts": {    "start": "node index.js"  }}
Dockerfile
# Specify a base imageFROM node:alpineWORKDIR /usr/app
# Install some dependenciesCOPY ./ ./RUN npm install
# Default commandCMD ["npm", "start"]
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade