--I have a Mac environment (I haven't tried it, but I think it works on Windows) --The docker command can be used --The npm command can be used --The node command can be used --Has basic knowledge of Docker (concept of images and containers)
--People who want to quickly build an environment --People who want to run Node.js with docker
bash
$ docker -v
Docker version 20.10.2, build 2291f61
$ npm -v
6.14.5
$ node -v
v13.11.0
$ pwd
~/{project_name}
project
{project_name}
├─ node_modules
| └─ ...
├─ src
| └─ index.js
└─ docker-compose.yml
└─ Dockerfile
└─ package.json
└─ package-lock.json
The contents of node_modules
are omitted
Basically, you can do it according to this official document, but I modified it because it was easy to develop.
{project_name}/Dockerfile
FROM node:12
#Create an application directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
#Copy under the directory where Dockerfile is located
ADD . /usr/src/app
#Install application dependencies
COPY package*.json /usr/src/app
RUN npm install
#If you are writing code for production
# RUN npm install --only=production
#Bundle application sources
COPY . /usr/src/app
--FROM node: 12
: For the part of 12
, refer to here and use your favorite version!
--WORKDIR/usr/src/app
: Specify the working directory of the docker container
(If you want to change it, you need to change all / usr/src/app
written in Dockerfile and docker-compose.yml which will be introduced later)
{project_name}/docker-compose.yml
version: '3'
services:
app:
build: .
command: bash -c 'node src/index.js'
image: node_test
volumes: .:/usr/src/app
ports: "8080:8080"
tty: true
--command: bash -c'node src/index.js'
: docker-compose up
command called when starting a container
--image: node_test
: Change the image name arbitrarily (such as an easy-to-understand project name)
--volumes:.:/usr/src/app
: Synchronize local files with files in docker container
--ports: "8080: 8080"
: Both local and container use port 8080
--tty: true
: docker-compose up
prevents the container from quitting
Basically it depends on your environment The following is a template of here
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello World');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
bash
$ docker-compose up
Have fun !!
Recommended Posts