Đi đến nội dung chính

Docker trên Windows

Trong hướng dẫn này, bạn sẽ triển khai MyEMS với Docker trên Windows.

Điều kiện tiên quyết

  • Đã cài đặt docker, npm trên máy chủ.
  • Đã cài đặt máy chủ MySQL.
  • Cơ sở dữ liệu MySQL có thể kết nối từ máy chủ chạy Docker Engine.
  • Yêu cầu phần cứng: Tối thiểu 4GB RAM, 20GB dung lượng lưu trữ (cơ sở dữ liệu và container Docker).

Sao chép mã nguồn:

cd ~
git clone https://github.com/myems/myems

Bước 1: Cơ sở dữ liệu

Xem Cơ sở dữ liệu

Bước 2: myems-api

Trong phần này, bạn sẽ cài đặt myems-api trên Docker.

  • Sao chép mã nguồn vào thư mục gốc
cp -r myems/myems-api c:\
cd c:\myems-api
  • Tạo tệp .env dựa trên tệp example.env
Cảnh báo

Thay thủ công 127.0.0.1 bằng địa chỉ IP HOST thực tế.

cp example.env .env
  • Build Image từ mã nguồn
docker build -t myems/myems-api .

Để build cho nhiều nền tảng, không chỉ cho kiến trúc và hệ điều hành mà người dùng đang sử dụng. Bạn có thể sử dụng buildx và đặt cờ --platform để chỉ định nền tảng mục tiêu cho kết quả build (ví dụ: linux/amd64, linux/arm64 hoặc darwin/amd64).

docker buildx build --platform=linux/amd64 -t myems/myems-api .
  • Chạy một container Docker

On host, create a folder at c:\myems-upload, and bind-mount it to the container, and also bind-mount the .env to the container:

docker run -d -p 8000:8000 -v c:\myems-upload:/var/www/myems-admin/upload -v c:\myems-api\.env:/app/.env:ro --log-opt max-size=1m --log-opt max-file=2 --restart always --name myems-api myems/myems-api
  • -d Run container in background and print container ID

  • -p Publish a container's port(s) to the host, 8000:8000 (Host:Container) binds port 8000 (right) of the container to TCP port 8000 (left) of the host machine.

  • -v If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v creates the endpoint for you. It is always created as a directory. The ro option, if present, causes the bind mount to be mounted into the container as read-only. For v4.7.0 or earlier versions, use '/code/.env' instead of '/app/.env'.

  • --log-opt max-size=2m The maximum size of the log before it is rolled. A positive integer plus a modifier representing the unit of measure (k, m, or g).

  • --log-opt max-file=2 The maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. A positive integer.

  • --restart Restart policy to apply when a container exits

  • --name Assign a name to the container

Đường dẫn tuyệt đối trước dấu hai chấm là đường dẫn trên máy chủ và có thể khác nhau trên hệ thống của bạn. Đường dẫn tuyệt đối sau dấu hai chấm là đường dẫn trên container và KHÔNG thể thay đổi. Bằng cách truyền .env làm tham số bind-mount, bạn có thể thay đổi giá trị cấu hình sau này. Nếu bạn thay đổi tệp .env, hãy khởi động lại container để áp dụng thay đổi.

Nếu bạn muốn di chuyển image sang máy tính khác,

  • Xuất image ra tệp tarball
docker save --output myems-api.tar myems/myems-api
  • Sao chép tệp tarball sang máy tính khác, sau đó nạp image từ tệp tarball
docker load --input .\myems-api.tar

Bước 3: myems-admin

Trong phần này, bạn sẽ cài đặt myems-admin trên Docker.

  • Sao chép mã nguồn vào thư mục gốc
cp -r myems/myems-admin c:\
cd c:\myems-admin
Cảnh báo

Thay thủ công 127.0.0.1:8000 trong nginx.conf bằng địa chỉ IP HOST và cổng thực tế của myems-api

notepad nginx.conf
      proxy_pass http://127.0.0.1:8000/;
  • Build Image từ mã nguồn
docker build -t myems/myems-admin .

Để build cho nhiều nền tảng, không chỉ cho kiến trúc và hệ điều hành mà người dùng đang sử dụng. Bạn có thể sử dụng buildx và đặt cờ --platform để chỉ định nền tảng mục tiêu cho kết quả build (ví dụ: linux/amd64, linux/arm64 hoặc darwin/amd64).

docker buildx build --platform=linux/amd64 -t myems/myems-admin .
  • Chạy một container Docker

On host, create a folder at c:\myems-upload, and bind-mount it to the container, and also bind-mount the nginx.conf to the container: -v parameter for upload folder must be same with that in myems-api

docker run -d -p 8001:8001 -v c:\myems-upload:/var/www/myems-admin/upload -v c:\myems-admin/nginx.conf:/etc/nginx/nginx.conf:ro --log-opt max-size=1m --log-opt max-file=2 --restart always --name myems-admin myems/myems-admin
  • -d Run container in background and print container ID

  • -p Publish a container's port(s) to the host, 8001:8001 (Host:Container) binds port 8001 (right) of the container to TCP port 8001 (left) of the host machine.

  • -v If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v creates the endpoint for you. It is always created as a directory. The ro option, if present, causes the bind mount to be mounted into the container as read-only. For v4.7.0 or earlier versions, use '/code/.env' instead of '/app/.env'.

  • --log-opt max-size=2m The maximum size of the log before it is rolled. A positive integer plus a modifier representing the unit of measure (k, m, or g).

  • --log-opt max-file=2 The maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. A positive integer.

  • --restart Restart policy to apply when a container exits

  • --name Assign a name to the container

Nếu bạn muốn di chuyển image sang máy tính khác,

  • Xuất image ra tệp tarball
docker save --output myems-admin.tar myems/myems-admin
  • Sao chép tệp tarball sang máy tính khác, sau đó nạp image từ tệp tarball
docker load --input .\myems-admin.tar

Bước 4: myems-modbus-tcp

Trong phần này, bạn sẽ cài đặt myems-modbus-tcp trên Docker.

  • Sao chép mã nguồn vào thư mục gốc
cp -r myems/myems-modbus-tcp c:\
cd c:\myems-modbus-tcp
  • Tạo tệp .env dựa trên tệp example.env
Cảnh báo

Thay thủ công 127.0.0.1 bằng địa chỉ IP HOST thực tế.

cp example.env .env
  • Build Image từ mã nguồn
docker build -t myems/myems-modbus-tcp .

Để build cho nhiều nền tảng, không chỉ cho kiến trúc và hệ điều hành mà người dùng đang sử dụng. Bạn có thể sử dụng buildx và đặt cờ --platform để chỉ định nền tảng mục tiêu cho kết quả build (ví dụ: linux/amd64, linux/arm64 hoặc darwin/amd64).

docker buildx build --platform=linux/amd64 -t myems/myems-modbus-tcp .
  • Chạy một container Docker (Chạy với quyền Quản trị viên)
docker run -d -v c:\myems-modbus-tcp\.env:/app/.env:ro --log-opt max-size=1m --log-opt max-file=2 --restart always --name myems-modbus-tcp myems/myems-modbus-tcp
  • -d Run container in background and print container ID

  • -v If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v creates the endpoint for you. It is always created as a directory. The ro option, if present, causes the bind mount to be mounted into the container as read-only. For v4.7.0 or earlier versions, use '/code/.env' instead of '/app/.env'.

  • --log-opt max-size=2m The maximum size of the log before it is rolled. A positive integer plus a modifier representing the unit of measure (k, m, or g).

  • --log-opt max-file=2 The maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. A positive integer.

  • --restart Restart policy to apply when a container exits

  • --name Assign a name to the container

Đường dẫn tuyệt đối trước dấu hai chấm là đường dẫn trên máy chủ và có thể khác nhau trên hệ thống của bạn. Đường dẫn tuyệt đối sau dấu hai chấm là đường dẫn trên container và KHÔNG thể thay đổi. Bằng cách truyền .env làm tham số bind-mount, bạn có thể thay đổi giá trị cấu hình sau này. Nếu bạn thay đổi tệp .env, hãy khởi động lại container để áp dụng thay đổi.

  • Di chuyển container Docker

Để di chuyển container sang máy tính khác,

  • Xuất image ra tệp tarball
docker save --output myems-modbus-tcp.tar myems/myems-modbus-tcp
  • Sao chép tệp tarball sang máy tính khác, sau đó nạp image từ tệp tarball
docker load --input .\myems-modbus-tcp.tar

Bước 5: myems-cleaning

Trong phần này, bạn sẽ cài đặt myems-cleaning trên Docker.

  • Sao chép mã nguồn vào thư mục gốc
cp -r myems/myems-cleaning c:\
cd c:\myems-cleaning
  • Tạo tệp .env dựa trên tệp example.env
Cảnh báo

Thay thủ công 127.0.0.1 bằng địa chỉ IP HOST thực tế.

cp example.env .env
  • Build Image từ mã nguồn
docker build -t myems/myems-cleaning .

Để build cho nhiều nền tảng, không chỉ cho kiến trúc và hệ điều hành mà người dùng đang sử dụng. Bạn có thể sử dụng buildx và đặt cờ --platform để chỉ định nền tảng mục tiêu cho kết quả build (ví dụ: linux/amd64, linux/arm64 hoặc darwin/amd64).

docker buildx build --platform=linux/amd64 -t myems/myems-cleaning .
  • Chạy một container Docker (Chạy với quyền Quản trị viên)
docker run -d -v c:\myems-cleaning\.env:/app/.env:ro --log-opt max-size=1m --log-opt max-file=2 --restart always --name myems-cleaning myems/myems-cleaning
  • -d Run container in background and print container ID

  • -v If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v creates the endpoint for you. It is always created as a directory. The ro option, if present, causes the bind mount to be mounted into the container as read-only. For v4.7.0 or earlier versions, use '/code/.env' instead of '/app/.env'.

  • --log-opt max-size=2m The maximum size of the log before it is rolled. A positive integer plus a modifier representing the unit of measure (k, m, or g).

  • --log-opt max-file=2 The maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. A positive integer.

  • --restart Restart policy to apply when a container exits

  • --name Assign a name to the container

Đường dẫn tuyệt đối trước dấu hai chấm là đường dẫn trên máy chủ và có thể khác nhau trên hệ thống của bạn. Đường dẫn tuyệt đối sau dấu hai chấm là đường dẫn trên container và KHÔNG thể thay đổi. Bằng cách truyền .env làm tham số bind-mount, bạn có thể thay đổi giá trị cấu hình sau này. Nếu bạn thay đổi tệp .env, hãy khởi động lại container để áp dụng thay đổi.

  • Di chuyển container Docker

Để di chuyển container sang máy tính khác,

  • Xuất image ra tệp tarball
docker save --output myems-cleaning.tar myems/myems-cleaning
  • Sao chép tệp tarball sang máy tính khác, sau đó nạp image từ tệp tarball
docker load --input .\myems-cleaning.tar

Bước 6: myems-normalization

Trong phần này, bạn sẽ cài đặt myems-normalization trên Docker.

  • Sao chép mã nguồn vào thư mục gốc
cp -r myems/myems-normalization c:\
cd c:\myems-normalization
  • Tạo tệp .env dựa trên tệp example.env
Cảnh báo

Thay thủ công 127.0.0.1 bằng địa chỉ IP HOST thực tế.

cp example.env .env
  • Build Image từ mã nguồn
docker build -t myems/myems-normalization .

Để build cho nhiều nền tảng, không chỉ cho kiến trúc và hệ điều hành mà người dùng đang sử dụng. Bạn có thể sử dụng buildx và đặt cờ --platform để chỉ định nền tảng mục tiêu cho kết quả build (ví dụ: linux/amd64, linux/arm64 hoặc darwin/amd64).

docker buildx build --platform=linux/amd64 -t myems/myems-normalization .
  • Chạy một container Docker (Chạy với quyền Quản trị viên)
docker run -d -v c:\myems-normalization\.env:/app/.env:ro --log-opt max-size=1m --log-opt max-file=2 --restart always --name myems-normalization myems/myems-normalization
  • -d Run container in background and print container ID

  • -v If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v creates the endpoint for you. It is always created as a directory. The ro option, if present, causes the bind mount to be mounted into the container as read-only. For v4.7.0 or earlier versions, use '/code/.env' instead of '/app/.env'.

  • --log-opt max-size=2m The maximum size of the log before it is rolled. A positive integer plus a modifier representing the unit of measure (k, m, or g).

  • --log-opt max-file=2 The maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. A positive integer.

  • --restart Restart policy to apply when a container exits

  • --name Assign a name to the container

Đường dẫn tuyệt đối trước dấu hai chấm là đường dẫn trên máy chủ và có thể khác nhau trên hệ thống của bạn. Đường dẫn tuyệt đối sau dấu hai chấm là đường dẫn trên container và KHÔNG thể thay đổi. Bằng cách truyền .env làm tham số bind-mount, bạn có thể thay đổi giá trị cấu hình sau này. Nếu bạn thay đổi tệp .env, hãy khởi động lại container để áp dụng thay đổi.

  • Di chuyển container Docker

Để di chuyển container sang máy tính khác,

  • Xuất image ra tệp tarball
docker save --output myems-normalization.tar myems/myems-normalization
  • Sao chép tệp tarball sang máy tính khác, sau đó nạp image từ tệp tarball
docker load --input .\myems-normalization.tar

Bước 7: myems-aggregation

Trong phần này, bạn sẽ cài đặt myems-aggregation trên Docker.

  • Sao chép mã nguồn vào thư mục gốc
cp -r myems/myems-aggregation c:\
cd c:\myems-aggregation
  • Tạo tệp .env dựa trên tệp example.env
Cảnh báo

Thay thủ công 127.0.0.1 bằng địa chỉ IP HOST thực tế.

cp example.env .env
  • Build Image từ mã nguồn
docker build -t myems/myems-aggregation .

Để build cho nhiều nền tảng, không chỉ cho kiến trúc và hệ điều hành mà người dùng đang sử dụng. Bạn có thể sử dụng buildx và đặt cờ --platform để chỉ định nền tảng mục tiêu cho kết quả build (ví dụ: linux/amd64, linux/arm64 hoặc darwin/amd64).

docker buildx build --platform=linux/amd64 -t myems/myems-aggregation .
  • Chạy một container Docker (chạy với quyền Quản trị viên)
docker run -d -v c:\myems-aggregation\.env:/app/.env:ro --log-opt max-size=1m --log-opt max-file=2 --restart always --name myems-aggregation myems/myems-aggregation
  • -d Run container in background and print container ID

  • -v If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v creates the endpoint for you. It is always created as a directory. The ro option, if present, causes the bind mount to be mounted into the container as read-only. For v4.7.0 or earlier versions, use '/code/.env' instead of '/app/.env'.

  • --log-opt max-size=2m The maximum size of the log before it is rolled. A positive integer plus a modifier representing the unit of measure (k, m, or g).

  • --log-opt max-file=2 The maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. A positive integer.

  • --restart Restart policy to apply when a container exits

  • --name Assign a name to the container

Đường dẫn tuyệt đối trước dấu hai chấm là đường dẫn trên máy chủ và có thể khác nhau trên hệ thống của bạn. Đường dẫn tuyệt đối sau dấu hai chấm là đường dẫn trên container và KHÔNG thể thay đổi. Bằng cách truyền .env làm tham số bind-mount, bạn có thể thay đổi giá trị cấu hình sau này. Nếu bạn thay đổi tệp .env, hãy khởi động lại container để áp dụng thay đổi.

  • Di chuyển container Docker

  • Xuất image ra tệp tarball

docker save --output myems-aggregation.tar myems/myems-aggregation
  • Sao chép tệp tarball sang máy tính khác, sau đó nạp image từ tệp tarball
docker load --input .\myems-aggregation.tar

Bước 8: myems-web

Trong phần này, bạn sẽ cài đặt myems-web trên Docker.

  • Chỉnh sửa tệp config.js:
Ghi chú

Lấy mapboxToken tại https://mapbox.com và sau đó đặt showOnlineMap thành true. Nếu bạn muốn tắt tính năng bản đồ trực tuyến, hãy đặt showOnlineMap thành false

cd myems/myems-web
notepad src/config.js
Cảnh báo

Thay 127.0.0.1:8000 trong nginx.conf bằng ip HOST và cổng thực tế của myems-api

cd myems/myems-web
notepad nginx.conf
  • Sao chép mã nguồn vào thư mục gốc
cp -r myems/myems-web c:\
cd c:\myems-web
Thông tin

Bạn có thể bỏ qua lệnh 'npm run build' trong phần này, vì nó đã được tích hợp trong Dockerfile

  • Build Image từ mã nguồn
docker build -t myems/myems-web .
docker image prune -f

Để build cho nhiều nền tảng, không chỉ cho kiến trúc và hệ điều hành mà người dùng đang sử dụng. Bạn có thể sử dụng buildx và đặt cờ --platform để chỉ định nền tảng mục tiêu cho kết quả build (ví dụ: linux/amd64, linux/arm64 hoặc darwin/amd64).

docker buildx build --platform=linux/amd64 -t myems/myems-web .
docker image prune -f
  • Chạy một container Docker

On host, bind-mount nginx.conf

docker run -d -p 80:80 -v c:\myems-web/nginx.conf:/etc/nginx/nginx.conf:ro --log-opt max-size=1m --log-opt max-file=2 --restart always --name myems-web myems/myems-web
  • -d Run container in background and print container ID

  • -p Publish a container's port(s) to the host, 80:80 (Host:Container) binds port 80 (right) of the container to TCP port 80 (left) of the host machine.

  • -v If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v creates the endpoint for you. It is always created as a directory. The ro option, if present, causes the bind mount to be mounted into the container as read-only. For v4.7.0 or earlier versions, use '/code/.env' instead of '/app/.env'.

  • --log-opt max-size=2m The maximum size of the log before it is rolled. A positive integer plus a modifier representing the unit of measure (k, m, or g).

  • --log-opt max-file=2 The maximum number of log files that can be present. If rolling the logs creates excess files, the oldest file is removed. A positive integer.

  • --restart Restart policy to apply when a container exits

  • --name Assign a name to the container

If you want to immigrate the image to another computer,

  • Export image to tarball file
docker save --output myems-web.tar myems/myems-web
  • Copy the tarball file to another computer, and then load image from tarball file
docker load --input .\myems-web.tar

Sau khi cài đặt

Chúc mừng! Bạn đã có thể đăng nhập vào giao diện quản trị MyEMS và giao diện web MyEMS.

Cổng mặc định

MyEMS Web UI: 80

MyEMS API: 8000

MyEMS Admin UI: 8001

Giả sử địa chỉ máy chủ là 192.168.1.8 (thay bằng địa chỉ thực tế) Truy cập MyEMS Web UI tại http://192.168.1.8 (có thể bỏ 80) Truy cập MyEMS Admin UI tại http://192.168.1.8:8001

Mật khẩu mặc định

MyEMS Admin UI

Tên đăng nhập:

administrator

Mật khẩu:

!MyEMS1

MyEMS Web UI

Tên đăng nhập:

administrator@myems.io

Mật khẩu:

!MyEMS1

Khắc phục sự cố

Cách khắc phục sự cố timeout khi kéo image

Do vấn đề mạng ISP, bạn có thể gặp tình trạng kéo image chậm. Bạn có thể sử dụng các dịch vụ tăng tốc của bên thứ ba.