[Node.js] Docker-compose up makes it easy to build a development environment

Premise

--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)

Target

--People who want to quickly build an environment --People who want to run Node.js with docker

environment

bash


$ docker -v
Docker version 20.10.2, build 2291f61

$ npm -v
6.14.5

$ node -v
v13.11.0

$ pwd
~/{project_name}

file organization

project


{project_name}
 ├─ node_modules
 |   └─ ...
 ├─ src
 |   └─ index.js
 └─ docker-compose.yml
 └─ Dockerfile
 └─ package.json
 └─ package-lock.json

The contents of node_modules are omitted

Dockerfile settings

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)

docker-compose.yml settings

{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

(Index.js settings)

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}`);

Start-up

bash


$ docker-compose up

Have fun !!

References

-Dockerize Node.js Web Application

Recommended Posts

[Node.js] Docker-compose up makes it easy to build a development environment
Try to build a Java development environment using Docker
Build a Node.js environment with Docker
Steps to build a Ruby on Rails development environment with Vagrant
Build a PureScript development environment with Docker
Build a Java development environment on Mac
Build a Wordpress development environment with Docker
Build a simple Docker + Django development environment
Build a hot reload development environment with Docker-compose using Realize of Go
I tried to build a Firebase application development environment with Docker in 2020
Created a library that makes it easy to handle Android Shared Prefences
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Build a development environment for Docker + Rails6 + Postgresql
Build a WordPress development environment quickly with Docker
Build a simple Docker Compose + Django development environment
[Win10] Build a JSF development environment with NetBeans
Build a development environment for Docker, java, vscode
How to build a Pytorch environment on Ubuntu
Build a Java development environment with VS Code
Build a development environment to create Ruby on Jets + React apps with Docker
Memo to build a Servlet environment on AWS EC2
[Road _node.js_1-1] Road to build Node.js Express MySQL environment using Docker
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Build a Ruby on Rails development environment on AWS Cloud9
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
How to build Java development environment with VS Code
A solution that makes it easy to input Procon and Web tests to verify results
I got an error when trying to install sbt to build a Scala development environment
[Environment construction] Build a Java development environment with VS Code!
I tried to create a java8 development environment with Chocolatey
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
A memo to build Jitsi Meet on Azure with docker-compose
How to build Rails, Postgres, ElasticSearch development environment with Docker
Build a Node-RED environment with Docker to move and understand
I tried to create a padrino development environment with Docker
Ruby ① Build a Windows environment
I made a GitHub Action that makes it easy to understand the execution result of RSpec
[App development 0.5] [Node.js express Docker] Build an environment for Node.js Express MongoDB using Docker
I tried to build an http2 development environment with Eclipse + Tomcat
I tried to create a Spring MVC development environment on Mac
Build a development environment where Ruby on Rails breakpoints work on Windows
Build a browser test environment using Capybara in the Docker development environment
Build a development environment for Django + MySQL + nginx with Docker Compose
I tried to build a laravel operating environment while remembering Docker
We will build a Spring Framework development environment in the on-premises environment.
Build a Tomcat 8.5 environment with Pleiades 4.8
[Rails] How to speed up docker-compose
I want to docker-compose up Next.js!
Build Java development environment (for Mac)
Build a XAMPP environment on Ubuntu
Build jooby development environment with Eclipse
Build Unity development environment on docker
A record of setting up a Java development environment with Visual Studio Code
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Minimal steps to set up a Ruby environment with rbenv on Ubuntu 20.04
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
Build a development environment on AWS EC2 with CentOS7 + Nginx + pm2 + Nuxt.js