基礎原理:容器技术/Docker/Concepts/container:修订间差异
创建页面,内容为“== Explanation == Imagine you're developing a killer web app that has three main components - a React frontend, a Python API, and a PostgreSQL database. If you wanted to work on this project, you'd have to install Node, Python, and PostgreSQL. How do you make sure you have the same versions as the other developers on your team? Or your CI/CD system? Or what's used in production? How do you ensure the version of Python (or Node or the database) your app needs…” 标签:2017年版源代码编辑 |
无编辑摘要 标签:2017年版源代码编辑 |
||
| 第1行: | 第1行: | ||
== | == 說明 == | ||
假設你正在開發一款功能強大的網頁應用程式,它包含三個主要組件 - 一個 React 前端、一個 Python API 以及一個 PostgreSQL 資料庫。若您想參與此專案,必須安裝 Node、Python 和 PostgreSQL. | |||
您如何確保自己使用的版本與團隊中其他開發人員、CI/CD 系統,或是生產環境中使用的版本一致? | |||
您如何確保應用程式所需的 Python(或 Node、資料庫)版本不會受到電腦上現有軟體的影響?您如何處理潛在的衝突?? | |||
容器登場! | |||
什麼是容器?簡單來說,容器就是為應用程式的每個元件所建立的隔離進程。每個元件——無論是前端的 React 應用程式、Python API 引擎,還是資料庫——都在各自的隔離環境中運行,與機器上的其他一切完全隔離。 | |||
以下就是它們如此出色的原因。容器是: | |||
* | * 自包含。每個容器都具備運作所需的一切,無需依賴主機上預先安裝的任何依賴項. | ||
* | * 隔離。由於容器在隔離環境中運行,因此對主機和其他容器的影響極小,從而提升了應用程式的安全性. | ||
* | * 獨立運作。每個容器皆獨立管理。刪除其中一個容器不會影響其他容器. | ||
* | * 可攜式。容器可在任何地方運行!在您的開發機器上運行的容器,在資料中心或雲端任何地方都能以相同方式運作! | ||
=== | === 容器與虛擬機器的比較 (VMs) === | ||
不深入探討細節的話,虛擬機器(VM)是一個完整的作業系統,擁有自己的核心、硬體驅動程式、程式及應用程式。僅為了隔離單一應用程式而啟動虛擬機器,會造成相當大的系統負擔. | |||
容器本質上是一個獨立的進程,其中包含其運行所需的所有檔案。若同時執行多個容器,它們將共用同一個核心,讓您能在更少的基礎設施上運行更多應用程式.<blockquote>'''同時使用虛擬機器與容器''' | |||
在許多情況下,您會看到容器與虛擬機器(VM)被併用。舉例來說,在雲端環境中,所配置的機器通常是虛擬機器。然而,與其為運行單一應用程式而配置一台機器,配備容器執行環境的虛擬機器可以同時運行多個容器化應用程式,從而提高資源利用率並降低成本.</blockquote> | |||
== | == 試試看 == | ||
在這項實作中,您將了解如何透過 Docker CLI介面執行Docker容器. | |||
請依照指示,使用命令列介面 (CLI) 執行容器: | |||
# 開啟您的 CLI 終端機,並使用 <code>docker run</code> 命令: | |||
<code>$docker run -d -p 8080:80 docker/welcome-to-docker</code> | |||
此命令的輸出結果是完整的容器 ID. | |||
恭喜!您剛啟動了第一個容器! 🎉 | |||
=== 檢視正在執行的容器 === | |||
您可以透過以下方式確認容器是否已啟動並正在運行: <code>docker ps</code> 命令: | |||
<code>docker ps</code> | |||
您將看到類似以下的輸出: | |||
<code>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | |||
a1f7a4bb3a27 docker/welcome-to-docker "/docker-entrypoint.…" 11 seconds ago Up 11 seconds 0.0.0.0:8080->80/tcp gracious_keldysh</code> | |||
This container runs a web server that displays a simple website. When working with more complex projects, you'll run different parts in different containers. For example, a different container for the <code>frontend</code>, <code>backend</code>, and <code>database</code>. | |||
Tip | |||
The <code>docker ps</code> command will show you ''only'' running containers. To view stopped containers, add the <code>-a</code> flag to list all containers: <code>docker ps -a</code> | |||
=== Access the frontend === | |||
When you launched the container, you exposed one of the container's ports onto your machine. Think of this as creating configuration to let you to connect through the isolated environment of the container. | |||
For this container, the frontend is accessible on port <code>8080</code>. To open the website, select the link in the '''Port(s)''' column of your container or visit <nowiki>http://localhost:8080</nowiki> in your browser. | |||
=== Stop your container === | |||
The <code>docker/welcome-to-docker</code> container continues to run until you stop it. You can stop a container using the <code>docker stop</code> command. | |||
# Run <code>docker ps</code> to get the ID of the container | |||
# Provide the container ID or name to the <code>docker stop</code> command: <code>docker stop <the-container-id></code> | |||
Tip | |||
When referencing containers by ID, you don't need to provide the full ID. You only need to provide enough of the ID to make it unique. As an example, the previous container could be stopped by running the following command: | |||
<code>docker stop a1f</code> | |||
2026年5月8日 (五) 16:20的版本
說明
假設你正在開發一款功能強大的網頁應用程式,它包含三個主要組件 - 一個 React 前端、一個 Python API 以及一個 PostgreSQL 資料庫。若您想參與此專案,必須安裝 Node、Python 和 PostgreSQL.
您如何確保自己使用的版本與團隊中其他開發人員、CI/CD 系統,或是生產環境中使用的版本一致?
您如何確保應用程式所需的 Python(或 Node、資料庫)版本不會受到電腦上現有軟體的影響?您如何處理潛在的衝突??
容器登場!
什麼是容器?簡單來說,容器就是為應用程式的每個元件所建立的隔離進程。每個元件——無論是前端的 React 應用程式、Python API 引擎,還是資料庫——都在各自的隔離環境中運行,與機器上的其他一切完全隔離。
以下就是它們如此出色的原因。容器是:
- 自包含。每個容器都具備運作所需的一切,無需依賴主機上預先安裝的任何依賴項.
- 隔離。由於容器在隔離環境中運行,因此對主機和其他容器的影響極小,從而提升了應用程式的安全性.
- 獨立運作。每個容器皆獨立管理。刪除其中一個容器不會影響其他容器.
- 可攜式。容器可在任何地方運行!在您的開發機器上運行的容器,在資料中心或雲端任何地方都能以相同方式運作!
容器與虛擬機器的比較 (VMs)
不深入探討細節的話,虛擬機器(VM)是一個完整的作業系統,擁有自己的核心、硬體驅動程式、程式及應用程式。僅為了隔離單一應用程式而啟動虛擬機器,會造成相當大的系統負擔.
容器本質上是一個獨立的進程,其中包含其運行所需的所有檔案。若同時執行多個容器,它們將共用同一個核心,讓您能在更少的基礎設施上運行更多應用程式.
同時使用虛擬機器與容器 在許多情況下,您會看到容器與虛擬機器(VM)被併用。舉例來說,在雲端環境中,所配置的機器通常是虛擬機器。然而,與其為運行單一應用程式而配置一台機器,配備容器執行環境的虛擬機器可以同時運行多個容器化應用程式,從而提高資源利用率並降低成本.
試試看
在這項實作中,您將了解如何透過 Docker CLI介面執行Docker容器. 請依照指示,使用命令列介面 (CLI) 執行容器:
- 開啟您的 CLI 終端機,並使用
docker run命令:
$docker run -d -p 8080:80 docker/welcome-to-docker
此命令的輸出結果是完整的容器 ID. 恭喜!您剛啟動了第一個容器! 🎉
檢視正在執行的容器
您可以透過以下方式確認容器是否已啟動並正在運行: docker ps 命令:
docker ps
您將看到類似以下的輸出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1f7a4bb3a27 docker/welcome-to-docker "/docker-entrypoint.…" 11 seconds ago Up 11 seconds 0.0.0.0:8080->80/tcp gracious_keldysh
This container runs a web server that displays a simple website. When working with more complex projects, you'll run different parts in different containers. For example, a different container for the frontend, backend, and database.
Tip
The docker ps command will show you only running containers. To view stopped containers, add the -a flag to list all containers: docker ps -a
Access the frontend
When you launched the container, you exposed one of the container's ports onto your machine. Think of this as creating configuration to let you to connect through the isolated environment of the container.
For this container, the frontend is accessible on port 8080. To open the website, select the link in the Port(s) column of your container or visit http://localhost:8080 in your browser.
Stop your container
The docker/welcome-to-docker container continues to run until you stop it. You can stop a container using the docker stop command.
- Run
docker psto get the ID of the container - Provide the container ID or name to the
docker stopcommand:docker stop <the-container-id>
Tip When referencing containers by ID, you don't need to provide the full ID. You only need to provide enough of the ID to make it unique. As an example, the previous container could be stopped by running the following command:
docker stop a1f
