[2021] Build a Docker + Vagrant environment for using React / TypeScript

Purpose

We have built an environment for creating front components using the following technologies.

The purpose is to use the above, but since I will not touch on React/TypeScript in this article, I think that it can be used to build an environment that uses Node.js.

The reason why I use Vagrant as well as Docker is that my development environment is mac. This is to eliminate the slow operation for the reasons described in the reference article.

Reference article

Abandon Docker for Mac, which significantly reduces DX, and get the fastest Docker environment for Mac

Preparation/Writer's environment

You need to install Vagrant and Mutagen described in the reference article. The environment of the author is as follows.

What to create

Create the following four files. Each file can be placed in the same directory.

When you start it, start it from vagrant, ssh it in the virtual machine, and start docker.

Vagrantfile This file describes the configuration of the virtual machine to be created.

Vagrant.configure('2') do |config|
  config.vm.box = 'ubuntu/focal64'

  config.vm.hostname = 'frontcomponent'

  config.vm.network :private_network, ip: '192.168.60.10'
  config.vm.network 'forwarded_port', guest: 8000, host: 8000

  config.vm.provider :virtualbox do |vb|
    vb.gui = false
    vb.cpus = 2
    vb.memory = 4096
    vb.customize ['modifyvm', :id, '--natdnsproxy1', 'off']
    vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'off']
  end

  config.disksize.size = '15GB'
  config.mutagen.orchestrate = true

  config.vm.synced_folder './', '/home/vagrant/front_component', type: "rsync",
    rsync_auto: true,
    rsync__exclude: ['.git/', 'log/', 'tmp/']

  config.vm.provision 'shell', inline: <<-SHELL
    echo "fs.inotify.max_user_watches = 65536" >> /etc/sysctl.conf && sysctl -p

    curl -fsSL https://get.docker.com -o get-docker.sh
    sh get-docker.sh
    usermod -aG docker vagrant

    #Adjust ver when updating docker
    curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
  SHELL
end

The port settings are set according to the environment that you will use in the future. Also, regarding the setting of specifications such as vm.provider, I think that it is better to tune with the PC you are using.

Other points I've been addicted to and added after reading the reference articles are as follows. Please be careful when using it.

hostname It seems that "_" cannot be used. Please note that it is easy to make a mistake if you enter the service name.

fs.inotify.max_user_watches If there is a directory that pulls in external files such as node_modules, the number of files may become too large and an error may occur. In that case, I think it is correct to exclude it from the mount of the file, but if it is not mounted locally such as the typescript definition file, the type definition file in node_modules could not be found and added when editing the code. If you have good knowledge, I would like to hear your opinion.

mutagen.yml

sync:
  app:
    mode: "two-way-resolved"
    alpha: "./"
    beta: "frontcomponent:/home/vagrant/front_component"
    ignore:
      vcs: true
      paths:
        - "/log"
        - "/tmp"

Use mutagen for file synchronization with vagrant. As for the details, I have not caught up with the understanding enough to summarize in the article, so I think that you should refer to the reference article.

Dockerfile Based on the Docker image file located in Docker Hub Create a base image of the container to be used this time.

FROM node:15.5.1

ENV LANG C.UTF-8
ENV WORKSPACE=/var/www/front_component/

RUN curl -sL https://deb.nodesource.com/setup_15.x | bash - 
RUN apt install -y less build-essential nodejs
RUN apt install -y vim less
RUN npm install n -g
RUN n 14.15.4

WORKDIR $WORKSPACE

Based on the image placed on Docker Hub, create a base image for this environment construction. This time, I'm assuming that react/typescript will be installed in the future, so I'm installing npm with the image of node.

docker-compose.yml


version: '3.7'

services:
  front:
    build: .
    image: front:1.0
    container_name: front
    tty: true
    stdin_open: true
    ports:
      - "8000:8000"
    environment: {}
    volumes:
      - .:/var/www/front_component
    # command: "bash -c 'npm i && npm run watch'"

It is a setting for creating a container. The comment out part is supposed to be used after inserting react/typescript in the future, Since it is not used in the environment construction so far, it is commented out.

Launch vagrant and docker

How to start

#Use the cd command to move to the directory where the file created above is located.

vagrant up  #Launch of vagrant
vagrant ssh #Access vagrant with ssh
cd front_component   #Move to the directory that mounts the files local to vagrant
docker-compose up -d #Launch docker

This completes the environment startup.

In addition to the above commands, I will list the commands that I often use. It is only a brief explanation, so please check it if you want to use it properly.

vagrant system

vagrant up      #Launch of vagrant
vagrant ssh     #Ssh access to the launched vagrant
vagrant halt    #Exit vagrant
vagrant destroy #Delete the entire disguise image created with vagrant

docker system

docker ps #Check the image of running docker
docker exec -ti <Container name> /bin/bash #Access inside the container like ssh
docker attach <Container name> #View processes running in a container
docker logs <Container name>   #Used to see the log etc. at the end of the forcibly terminated container

First post ε ('∞' *)

Recommended Posts

[2021] Build a Docker + Vagrant environment for using React / TypeScript
Build a development environment for Docker + Rails6 + Postgresql
Build a development environment for Docker, java, vscode
How to build a Ruby on Rails environment using Docker (for Docker beginners)
Try to build a Java development environment using Docker
Build a Node.js environment with Docker
Build a local development environment for Open Distro for Elasticsearch with multiple nodes using Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
Create a web environment quickly using Docker
[App development 0.5] [Node.js express Docker] Build an environment for Node.js Express MongoDB using Docker
Build a PureScript development environment with Docker
Build a browser test environment using Capybara in the Docker development environment
Build a Wordpress development environment with Docker
Build a simple Docker + Django development environment
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Create a Privoxy + Tor environment instantly using Docker
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
Build a Kotlin app using OpenJDK's Docker container
Build a simple Docker Compose + Django development environment
Build a container for Docker x Laravel phpMyAdmin
[App development 1] [Node.js express Docker] Build an environment for Node.js Express MongoDB (mongoose) using Docker [December 2020]
Build a development environment to create Ruby on Jets + React apps with Docker
[Road _node.js_1-1] Road to build Node.js Express MySQL environment using Docker
Build a docker container for a python simple web server
How to build docker environment with Gradle for intelliJ
Easily build a Vue.js environment with Docker + Vue CLI
[Note] Build a Python3 environment with Docker in EC2
Build debug environment for Phalcon + Vagrant + centos + xdebug + phpstorm
[Docker] Build a Wordpress environment locally (Win, Mac) quickly
I tried to build an environment using Docker (beginner)
Install Ubuntu 20.04 in virtual box on windows10 and build a development environment using docker
Build docker environment with WSL
Build Spring for Android 2.0.0 environment
Ruby ① Build a Windows environment
React environment construction with Docker
multi-project docker build using jib
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
I tried to build the environment little by little using docker
Build a Node-RED environment with Docker to move and understand
Configuration script for using docker in proxy environment on ubuntu 20.04.1
Create a Vue3 environment with Docker!
Use MailHog for checking emails in the development environment (using Docker)
Build a SPA for Laravel 6.2 / Vue.js / Nginx / Mysql / Redis with Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Environment construction with Docker for beginners
Build PlantUML environment with VSCode + Docker
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Run React on a Docker container
Build environment with vue.js + rails + docker
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ MySQL edition ~
Build Rails environment with Docker Compose
(For myself) Try creating a C # environment with docker + code-server, cloud9
Build a XAMPP environment on Ubuntu
Jupyter's Docker environment for running TensorFlow
[Introduction] Build a virtual environment of Vagrant + VirtualBox on Window10 [Environment construction]
Set up a Wordpress Docker environment without using the Worpdress image
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Sequelize ~
I tried to build a laravel operating environment while remembering Docker
Build docker + laravel environment with laradock
How to build CloudStack using Docker