如何用 Docker 來安裝 WordPress

WordPress 是最受歡迎的內容管理軟體 (content management software, CMS) 之一,因為它的功能多和使用上容易。但是,如果你不常架設網頁主機環境,要建立一個新的環境可能特別花時間。簡化安裝過程於一些快速指令大大減少時間和需要的氣力,這是為什麼要使用 Docker。用 docker 來安裝 WordPress 是很輕鬆地,繼續讀下去來發現更多。

Docker 是容器平台允許簡單及快速軟體安裝在任何系統及作業系統。它包裝了軟體的片段在完整的檔案系統,包含了所有它執行需要的東西,例如程式碼、執行環境、系統工具及程式庫。這讓任何人可以包裝應用程式及它的相依性軟體變成一個標準化的建造區塊。

Install Docker

安裝 Docker 本身非常容易。首先對你的系統執行常見的 update 指令以確認你有最新的來源檔清單。

# Debian and Ubuntu
sudo apt-get update
# CentOS
sudo yum update

檢查你是否有 curl 這個命令列工具程式。

curl -V

它預裝在大部數的 Linux 發行版,但如果找不到它,使用適合你系統的指令來手動安裝它。

# Debian and Ubuntu
sudo apt-get install curl
# CentOS
sudo yum install curl

使用下列的指令來下載並安裝 Docker。這個過程需要 root 權限,所以你用非 root 使用者,則將被要求輸入 sudo 密碼。

curl -fsSL https://get.docker.com/ | sh

直到最後的安裝過程,你會看到建議增加你的 username 到 Docker 的 users group。這樣可以讓你在執行 Docker 指令時不用每次都要調用 suoo。

sudo usermod -aG docker <username>

在繼續前,記得在加入你自己到 Docker 的 users group 後要先登出再登入。

你可以用下列的測試程式來檢查安裝是否成功:

docker run hello-world

你應該要可以看到類似像下列範例的輸出

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
...
Hello from Docker.
This message shows that your installation appears to be working correctly.
...

如果指令沒有立即生效,用下列方式重啟 Docker 服務並試著再執行 hello-world app

sudo systemctl restart docker

現在 Docker 應該已經完成安裝且運作正常。繼續下面剩下的 WordPress 安裝。

MariaDB in a container

在用 Docker 安裝 WordPress 之前,你會需要有地方可以儲存資料。MariaDB 是社群開發的關聯式資料庫管理系統而且是 MySQL 的直接替代元件。它在 Docker 有官方可用的最新 image 且提供簡易指令。

從建立一個新的目錄開始,在一個你希望存放 WordPress 及 MariaDB 的資料,例如你的 Home 目錄。

mkdir ~/wordpress && cd ~/wordpress

可以執行單一指令來下載及安裝一個新的 MariaDB 容器。在跳進去執行前先檢查需要的參數。

MariaDB 環境變數在 Docker 指令用 -e 來標示:

  • -e MYSQL_ROOT_PASSWORD= 在這設定你的密碼
  • -e MYSQL_DATABASE= 建立並命名一個資料庫,例如 wordpress

Docker 參數:

  • name wordpressdb 去命名容器的名稱
  • -v “$PWD/database”:/var/lib/mysql –建立一個資料目錄被連結到容器的儲存空間來確認資料的持久性數據
  • -d 讓 Docker 知道用 daemon 來跑這個容器
  • mariadb:latest 最後定義要裝什麼及哪一個版本

然後執行下面的指令同時要用你自己的密碼取代 <password>

docker run -e MYSQL_ROOT_PASSWORD=<password> -e MYSQL_DATABASE=wordpress --name wordpressdb -v "$PWD/database":/var/lib/mysql -d mariadb:latest
...
Status: Downloaded newer image for mariadb:latest
23df0ec2e48beb1fb8704ba612e9eb083f4193ecceb11102bc91232955cccc54

如果 Docker 已經成功建立容器,你應該可以在最後的輸出看到一串字碼,就像上面的例子。你可以用下列的指令來確認 MariaDB 容器是否正在執行:

docker ps

檢查你的 MariaDB 安裝狀態,它應該顯示 “Up” 和它執行的時間就像下面例子的輸出

CONTAINER ID IMAGE          COMMAND                CREATED        STATUS        PORTS      NAMES
14649c5b7e9a mariadb:latest "/docker-entrypoint.s" 12 seconds ago Up 12 seconds 3306/tcp   wordpressdb

Other useful commands for working with containers are ‘start’, ‘stop’ and ‘remove’.

其他常用的 container 指令有 ‘start’、 ‘stop’ 和 ‘remove’

docker start <container name>
docker stop <container name>
docker rm <container name>

You can find out more about available commands and options for specific commands.

你可以找到更多可用的指令和特定的指令選項

docker --help
docker <command> --help

Full command-line documentation is also available over at the Docker support page.

完整的指令列文件同樣在下列的 Docker support page。

WordPress with Docker

Applications in containers run isolated from one another in the userspace of the host operating system sharing the kernel with other containers. This reduces the overhead required to run packaged software while also enabling the containers to run on any kind of infrastructure. To allow applications within different containers to work with one another Docker supports container linking.

應用程式彼此隔離在容器和其他容器分享核心的Host OS 使用者空間裡執行。這樣減少必要的運行花費去執行封裝好的軟體同時也能夠讓容器可以在任何一種基礎架構上執行。為了讓應用程式在不同的容器互相合作,Docker 支援容器連結。

WordPress is also made officially available on Docker Hub, pull the image using the command below. When the version to download is not specified Docker will fetch the latest available.

WordPress 同樣在 Docker Hub 也有官方版本可用,用下列指令可以取得映像檔。如果沒有指定版本,Docker將會取最新的可用版本。

docker pull wordpress

WordPress container also takes environment variables and Docker parameters:

WordPress 容器同樣使用環境變數和 Docker 參數

  • -e WORDPRESS_DB_PASSWORD= Set the same database password here.
  • –name wordpress – Gives the container a name.
  • –link wordpressdb:mysql – Links the WordPress container with the MariaDB container so that the applications can interact.
  • -p 80:80 – Tells Docker to pass connections from your server’s HTTP port to the containers internal port 80.
  • -v “$PWD/html”:/var/www/html – Sets the WordPress files accessible from outside the container. The volume files will remain even if the container was removed.
  • -d – Makes the container run on background
  • wordpress – Tells Docker what to install. Uses the package downloaded earlier with the docker pull wordpress -command.

Run the command below while replacing the <password> as you did for the MariaDB container.

執行下列的指令同時取代 <password> 和作 MariaDB 容器一樣的作法

docker run -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=<password> --name wordpress --link wordpressdb:mysql -p 80:80 -v "$PWD/html":/var/www/html -d wordpress

Then open your server’s domain name or IP address in a web browser to test the installation. You should be redirected to the initial WordPress setup page at http://<public IP>/wp-admin/install.php. Go through the setup wizard and you are done.

然後打開你的伺服器網域名稱或IP位址在瀏灠器來測試該安裝。你應該會被導到初始化 WordPress 安裝頁在 http://<public IP/wp-admin/install.php。透過安裝精靈後就可以完成。

Leave a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *