DEV Community

Cover image for 給 PHP 開發者的 Docker 文件(二)
Leon
Leon

Posted on • Edited on • Originally published at editor.leonh.space

給 PHP 開發者的 Docker 文件(二)

(本文譯自〈Docker for PHP Developers〉)

哈囉!Docker

Docker 是一個包含許多技術的概括性詞彙,它提供的工具可以幫助你把某個應用(application)的基礎架構(infrastructure)分離成數個邏輯模塊(容器),你可以只組合某些必要的模塊去把應用的基礎架構建構成可移植(portable)的,這樣的基礎架構可以於開發(development)、上線(staging)、生產(production)環境中遷移。

什麼是 Docker 容器(Container)?

一個容器指的是一個大型應用內的某個單一的服務。舉例來說,假設我們正在建構一個 PHP 應用,那麼我們需要一個網頁伺服器(如 nginx);我們也需要一個應用伺服器(如 PHP-FPM);我們還需要一個資料庫伺服器(如 MySQL)。也就是說我們的應用需要三個這樣的獨立的服務:網頁伺服器、應用伺服器、資料庫伺服器,這三樣服務都可以被分離成各自專屬的 Docker 容器,只要我們把這三個容器連結起來,我們就有了一個完整的應用。

什麼是 Docker 映像(Image)?

把某個應用分成數個容器看來像是吃力不討好的工作,至少我以往是這麼認為的,不過實際上並不然,首先,容器實際上是映像的實例(instance),若把 Docker 的映像想像成 PHP 的類別(class),就像 PHP 類別可以讓我們實例化(instantiate)出許多獨立的物件(object)一樣,Docker 的映像也可以實例化出許多獨立的 Docker 容器。例如我們可以拿一個 PHP-FPM Docker 映像去為我們的每個應用實例化出它們的獨立的 PHP-FPM 容器。

我們也可以自製 Docker 映像,不過還是從 Docker Hub 去找適合的 Docker 映像會比較簡單些,像是我們會抓 sameersbn/mysql 這個 Docker 映像來建立 MySQL 資料庫容器。如果別人已經建好了能幫我們解決問題的 Docker 映像,為什麼還要重新發明輪子呢

(待續…)

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay