在Mac上用Docker建一個Ubuntu環境來玩玩Webinoly吧!

文章最後更新於 2020 年 11 月 6 日

Image for post

大家好,我是卡哥,Webinoly Study Group in Taiwan 社團召集人,這篇文章會介紹用Docker啟動一個Ubuntu的Container,然後安裝Webinoly。

我的筆電是MacBook,文中的示範都是在Mackbook的環境下完成的。

因為Webinoly目前只Support Linux Ubuntu,在過去如果不想花錢錢去租一台Linux VPS的話,就必須自己裝一台Linux或是用VMware或VirtualBox之類的虛擬主機。

有了Docker這種Container的概念後,啟動一個Linux模擬環境變的非常簡單,接下來就一步一步來示範如何實作。

首先要先安裝Docker Desktop https://www.docker.com/get-started

裝好後啟動,確定Docker Desktop is running.

Image for post

接下來3個步驟:

啟一個Ubuntu的Container

(目前Webinoly支援的版本為Ubuntu 20.04 與 18.04,現在latest是20.04):

第一個指令啟動一個名為ubuntu-webinoly的Container

docker run -itd --privileged --name ubuntu-webinoly -p 8080:80 -p 22223:22222 -v /sys/fs/cgroup:/sys/fs/cgroup:ro jrei/systemd-ubuntu

指令參數說明如下

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

run : Run a command in a new container

-i, --interactive : Keep STDIN open even if not attached-t, --tty : Allocate a pseudo-TTY-d, --detach : Run container in background and print container ID--privileged : Give extended privileges to this container--name : Assign a name to the container-p, --publish list :Publish a container's port(s) to the host-v, --volume list :Bind mount a volume

使用的image為jrei/systemd-ubuntu https://hub.docker.com/r/jrei/systemd-ubuntu

上面的docker run會把機器的Port 80 導到Container裡的Port 8080;Port 22223 Port導到Port 22222,也就是說到時候我們的

Mac的 localhost:8080 會對應到Container的 localhost:80

Mac的 localhost:22223 會對應到Container的 localhost:22222

因為80 Port將會是Nginx的預設Port,而22222 Port是預設Webinoly Tools的位置(phpMyAdmin)

第二個指令進入docker:

docker exec -it ubuntu-webinoly /bin/bash

說明如下:

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG…]Run a command in a running container-i, --interactive : Keep STDIN open even if not attached-t, --tty : Allocate a pseudo-TTY

以下是執行後的參考結果:

> docker run -itd --privileged --name ubuntu-webinoly -p 8080:80 -p 22223:22222 -v /sys/fs/cgroup:/sys/fs/cgroup:ro jrei/systemd-ubuntu                                                                                                   
Unable to find image 'jrei/systemd-ubuntu:latest' locally
latest: Pulling from jrei/systemd-ubuntu
3ff22d22a855: Pull complete
e7cb79d19722: Pull complete
323d0d660b6a: Pull complete
b7f616834fd0: Pull complete
253a33dbd35c: Pull complete
67fd89d007d5: Pull complete
6371579ddac6: Pull complete
980e40c4e38a: Pull complete
Digest: sha256:1398553ead86d54f3375424ba85c11dad53530b6e9b974efe7705f9d50474f5b
Status: Downloaded newer image for jrei/systemd-ubuntu:latest
f7839f1d813403bba1117d7d530ad59ce079d9806085fbee97448116850aa319> docker exec -it ubuntu-webinoly /bin/bash
root@f7839f1d8134:/#
root@f7839f1d8134:/# uname -a
Linux f7839f1d8134 4.19.76-linuxkit #1 SMP Tue May 26 11:42:35 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

有看到類似 root@f7839f1d8134 應該就是安裝成功並已經在Ubuntu的環境了

安裝必要的Packages (在之後安裝Webinoly時,script會用到)

安裝之前,因為docker安裝的ubuntu預設會有一個檔案/usr/sbin/policy-rc.d ,內容是exit 101; 會阻止套件安裝完後的自動啟動,影響mariadb的設定,所以要先刪掉它!

rm /usr/sbin/policy-rc.d

然後安裝Webinoly會用到的Packages

apt update && apt upgrade -y && apt autoremove && apt install lsb wget sudo curl git -y

安裝Webinoly

執行Webinoly安裝LEMP環境的指令

wget -qO weby qrok.es/wy && sudo bash weby 3

最後那個3指的就是LEMP,可以參考官網:https://webinoly.com/en/install/

wget -qO weby qrok.es/wy && sudo bash weby <option> <version>

option有下面幾種選項:

  • HTML (1) – Only install Nginx, which will only support HTML sites.
  • PHP (2) – Nginx and PHP will be installed.
  • LEMP (3) – A complete LEMP installation will be done, Nginx + PHP + MySQL (MariaDB). This is the most complete and necessary configuration for WordPress sites.
  • Maybe later (0) – Webinoly is installed without installing any package, it allows you to do a manual and personalized installation with the command stack.

裝好後,會看到安裝成功的畫面,可以把下面的DB Password記起來

Image for post

沒記也沒關系,之後用下面指令查詢即可

webinoly -dbpass

測試一下安裝

webinoly -verify

會發現

[INFO] HTTP Authentication Credentials not found!

只要用下面指令加入一組HTTP Authentication的帳密就行了

httpauth -add

做完可以試試看連 http://localhost:8080/ 應該可以看到下面內容

Image for post

試試看架一個local的網站,其中 my.local 是一個假 domain,你必需在你的Macbook (非container裡) 的 /etc/hosts加入

127.0.0.1 my.local

才能讓你的機機把 my.local認得為IP 127.0.0.1

> site my.local -wp

Downloading WordPress...Site my.local has been successfully created!

之後用瀏覽器打開 http://my.local:8080 就會看到畫面

Image for post

這時候輸入稍早用httpauth建立的帳密即可

Image for post

Tools的會在當初建Container時用的Port 22223

http://my.local:22223/
Image for post

PhpMyAdmin會在

http://my.local:22223/pma/

登入的帳密可以用下面指令查 root 那組

webinoly --dbpass

Docker Desktop可以做一些操作比方說Delete,可以刪掉整個Container

Image for post

要跳出Container可以直接 exit 或按 Ctrl+p然按按q

補充Docker常用指令

# 查詢所有的Containers
docker ps -a

# 停止 Docker Container
docker stop DOCKER_ID

# 強制停止 Docker Container
docker kill DOCKER_ID

# 重新啟動 Docker Container
docker restart DOCKER_ID

# 暫停 Docker Container
docker pause DOCKER_ID

# 恢復 Docker Container
docker unpause DOCKER_ID

# 複製本機檔案到 Container
docker cp /path/to/file1 DOCKER_ID:/path/to/file2

官方網站有詳細的教學文件可以照著試試看,這篇主要示範Docker的部分

以上謝謝您的閱讀,文章就到此結束!

對Webinoly有興趣學習分享交流的朋友,歡迎加入Webinoly Study Group in Taiwan社團:

https://www.facebook.com/groups/webinolytaiwan

關於作者

卡哥
卡哥
我是Oscar (卡哥),前Yahoo Lead Engineer、高智商同好組織Mensa會員,超過十年的工作經驗,服務過Yahoo關鍵字廣告業務部門、電子商務及搜尋部門,喜歡彈吉他玩音樂,也喜歡投資美股、虛擬貨幣,樂於與人分享交流!

如果對文章內容有任何問題,歡迎在底下留言讓我知道。
如果你喜歡我的文章,可以按分享按鈕,讓更多的人看見我的文章。

網友留言