Node.js environment construction with Docker Compose

Motivation

I wanted to practice Create React App a little detour without polluting the local environment. Therefore, we aimed to build an environment like the title.

Final directory structure

Final directory structure


node-docker/
    |--docker-compose.yml
    |--node/
        |--app/

docker-compose.yml

Docker Compose Overview Compose is a tool for Docker applications that define and run multiple containers.

First, create the project folder node-docker and create the docker compose.yml file there.

Current directory structure


node-docker/
    |--docker-compose.yml

docker-compose.yml


version: '3'
services:
  node:
    image: node:14.9.0-alpine3.10
    container_name: node
    volumes:
      - ./node/app:/app
    tty: true
    ports:
      - 3000:3000

version File format version of docker-compose.yml services You can define each container as a service. node Service name image Specifies the original image when running the container. If the image doesn't exist, Compose will try to pull it from Docker Hub. To create an image from Dockerfile, for example, create Dockerfile under the node directory and specify as follows. build: ./node container_name Specify a custom container name instead of the default generated name. By default, it will be named node-docker_node_1. volumes Share the local path (left) and the container path (right). tty Specifies whether to start the terminal. (Maybe)

ports Publish the port. Specify the host and port (host: container), or specify only the port of the container (the port on the host side is randomly selected). expose exposes the port and is only accessible between linked services.

Create and start a container

Now that you have the necessary files, let's create and start the container. If you want to create an image from Dockerfile, you need to do Docker-compose build, but this time you don't need it because you will get the already built image remotely. Work is done on the directory where docker-compose.yml is located.

terminal


$ docker-compose up -d

The d option allows it to start in the background.

Enter the node container

Now that the node environment is in place, the node environment is on the container, so you need to work on it. For that purpose, start the shell and terminal on the container by using the exec command that can execute the specified command in the running container.

terminal


$ docker-compose exec node sh

The directory structure in the container is as follows, and since it was specified in docker-compose.yml, the app directory in the container and the local node / app directory correspond.

Directory structure in the container


/
    |--app
    |--bin
    |--dev
    |--home
    |--lib
    |--media
    |--mnt
    |--opt
    |--proc
    |--root
    |--run
    |--sbin
    |--srv
    |--sys
    |--tmp
    |--usr
    |--var

I think it's better to work in the app directory that can be shared locally.

reference

Compose file version 3 reference Compose File Reference Docker Compose --docker-compose.yml reference [For beginners] Easy Node.js development environment construction with Docker (2)

Recommended Posts

Node.js environment construction with Docker Compose
[Docker] Rails 5.2 environment construction with docker
React environment construction with Docker
Rails + MySQL environment construction with Docker
Build a Node.js environment with Docker
Environment construction with Docker for beginners
Build Rails environment with Docker Compose
[Environment construction with Docker] Rails 6 & MySQL 8
Docker environment construction
GPU environment construction with Docker [October 2020 version]
Rails environment construction with Docker (personal apocalypse)
Laravel development environment construction with Docker (Mac)
Create Rails 6 + MySQL environment with Docker compose
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
[Docker] Create Node.js + express + webpack environment with Docker
Laravel + MySQL + phpMyadmin environment construction with Docker
Rails Docker environment construction
Environment construction command memo with Docker on AWS
Rails6 [API mode] + MySQL5.7 environment construction with Docker
React + Django + Nginx + MySQL environment construction with Docker
Wordpress local environment construction & development procedure with Docker
Pytorch execution environment with Docker
MySQL 5.7 (Docker) environment construction memo
Redmine (Docker) environment construction memo
Build docker environment with WSL
Docker × Spring Boot environment construction
[Docker] postgres, pgadmin4 environment construction
Database environment construction with Docker in Spring boot (IntellJ)
Create a Vue3 environment with Docker!
Build Couchbase local environment with Docker
Laravel + Docker Laradock usage environment construction
Build PlantUML environment with VSCode + Docker
Build environment with vue.js + rails + docker
Easy environment construction of MySQL and Redis with Docker and Alfred
[Portfolio] Manage site with laravel APP implementation (Docker environment construction)
Create SolrCloud verification environment with Docker
WordPress with Docker Compose on CentOS 8
Rails on Docker environment construction procedure
Create Laravel environment with Docker (docker-compose)
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Web application creation with Nodejs with Docker
Build docker + laravel environment with laradock
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
PostgreSQL environment construction with Docker (from setup to just before development)
Build a development environment for Django + MySQL + nginx with Docker Compose
"Rails 6 x MySQL 8" Docker environment construction procedure for sharing with teams
Alert slack with alert manager in Docker environment
SQL statement learning ~ Environment construction ~ Docker + MySQL
Building Rails 6 and PostgreSQL environment with Docker
Sapper × Go (echo) × Docker development environment construction
Build a PureScript development environment with Docker
[Now] Let's Redmine with Docker Compose with Let's Encrypt
Edit Mysql with commands in Docker environment
Create a MySQL environment with Docker from 0-> 1
Create Spring Boot-gradle-mysql development environment with Docker
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Spring Boot + Docker Java development environment construction
Build a Wordpress development environment with Docker
[Docker] Build Jupyter Lab execution environment with Docker
Try using Kong + Konga with Docker Compose.
Build an environment with Docker on AWS