Create Dockerfile, docker-compose.yml and run nodejs on windows10, Linux, Raspberry Pi

In the continuation of the following article I wrote earlier, I will create a Dockerfile and docker-compose.yml and describe the procedure to run it this time. https://qiita.com/MCYamamoto/items/4d424af9573fa90edf61

Final folder structure

First, the final folder structure. It will be as follows.

base folder
+docker-compose.yml
+cont
 +env
  +Dockerfile
   +deploy //Source code to be deployed(This time nodejs)
    +package.json
    +package-lock.json
    +build
     +js files
  +share //Shared folder for host and container

Create a Dockerfile

[Reference URL] https://qiita.com/pottava/items/452bf80e334bc1fee69a

For the overall flow, the above was referred to. Installation Make a note of the command while installing what you need to create the final Dockerfile.

1. Decide on a Docker image.

Since Raspberry Pi is ARM, I made it Debian as an image corresponding to ARM. [Reference URL] https://qiita.com/koduki/items/0ed303dac5d32646194f

2. Start the bash container.

$ sudo docker run -it -v $(pwd):/tmp/share debian:buster /bin/bash
 -it: Can be operated in the terminal
 -v: Folder sharing with containers

3. Install what you need.

Install the required packages depending on the program you want to run.

apt-get install package name

It's like that.

Please note that the "-y" option is required when writing to the Dockerfile because the installation will not be stopped.

In the end, I only needed node and sqlite, so it's better to make the container image a node, By leaving the process, the final result is as follows.

Here's what we're doing: --Copy the code to deploy --Install what you need on the OS --Install the required packages on the node under the deploy folder with "npm install"

#Dockerfile

FROM debian:buster

COPY deploy/ /home/deploy
WORKDIR /home/deploy

RUN apt-get update
RUN apt-get install -y cmake
RUN apt-get install -y libssl-dev
RUN apt install curl
RUN apt-get install -y sqlite3 libsqlite3-dev
RUN apt-get install -y nodejs npm && npm install n -g && n 12.18.4
RUN npm install

Create docker-compose.yml

Using the Dockerfile created earlier as a container image, the node under the deploy folder will be automatically started in the end.

version: "3.8"
services:
  cont:
    build:
      context: ./cont/env
      dockerfile: Dockerfile
    container_name: CONT-1
    tty: true
    volumes:
      - "./cont/env/share:/tmp/share"
      - ".:/workspace"
      - "./cont/env/deploy/build:/home/deploy/build"
    working_dir: /home/deploy/build
    command: npm run start
The following is described in package.json so that the programs under the build folder can run.
  "scripts": {
    "start": "node ./build/main.js",
  },

launch docker-compose

If you place the created folder environment set in any folder of each OS (Windows 10, Linux, Raspi) and execute the following, all will work in the same way.

sudo docker-compose up --build

(Bonus) Create a debug environment with Vscode

[Reference URL] https://tech-lab.sios.jp/archives/21675 https://qiita.com/Yuki_Oshima/items/d3b52c553387685460b0

In Visual Studio Code, click the f1 menu or the green icon that appears at the bottom left, select "Remote-Containers: Open Folder in Container ...", and select the folder containing docker-compose.yml. After a while, the container will run and you can debug it in the console.

vscode, docker convenient ,,

Recommended Posts

Create Dockerfile, docker-compose.yml and run nodejs on windows10, Linux, Raspberry Pi
Install Docker and docker-compose on Raspberry Pi 4, Linux (Debian) and Windows 10, respectively
Install Docker on Raspberry Pi 4 and Raspberry Pi OS 64bit
How to run javafx with Raspberry Pi Posted on 2020/07/12
Eh !? Run Linux on Windows? I can do it! !! (I set WSL2 and put Ubuntu)
Install Docker on Raspberry Pi
Put Ubuntu 20.04.1 on Raspberry Pi 4
Run openvpn on Docker (windows)
Minecraft server on Raspberry Pi 4
Install Cloud9 on Raspberry pi 4 and set up Rails development environment
Until you put Ubuntu 20 on Windows 10 Home and WSL2 and run Docker
Install MariaDB on Raspberry Pi OS
Try putting CentOS 8 on Raspberry Pi 3
Install Java 9 on windows 10 and CentOS 7
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!