教學課程:開始使用 DockerTutorial: Get started with Docker

在本教學課程中,您將瞭解如何建立和部署 Docker 應用程式,包括搭配資料庫使用多個容器,以及使用 Docker Compose。In this tutorial, you'll learn about creating and deploying Docker apps, including using multiple containers with a database, and using Docker Compose. 您也會將容器化應用程式部署到 Azure。You'll also deploy your containerized app to Azure.

開始教學課程Start the tutorial

如果您已經執行此命令來開始進行教學課程,恭喜您!If you've already run the command to get started with the tutorial, congratulations! 如果沒有,請開啟命令提示字元或 bash 視窗,然後執行下列命令:If not, open a command prompt or bash window, and run the command:

docker run -d -p 80:80 docker/getting-started

您會發現有幾個旗標正在使用。You'll notice a few flags being used. 以下是一些詳細資訊:Here's some more info on them:

  • -d -在背景中以卸離模式執行容器 () -d - run the container in detached mode (in the background)
  • -p 80:80 -將主機的埠80對應至容器中的埠80-p 80:80 - map port 80 of the host to port 80 in the container
  • docker/getting-started -要使用的映射docker/getting-started - the image to use

提示

您可以結合單一字元旗標來縮短完整的命令。You can combine single character flags to shorten the full command. 例如,上述命令可以撰寫為:As an example, the command above could be written as:

docker run -dp 80:80 docker/getting-started

VS Code 擴充功能The VS Code Extension

在走到太遠之前,我們想要強調 Docker VS Code 擴充功能,讓您快速瞭解在電腦上執行的容器。Before going too far, we want to highlight the Docker VS Code Extension, which gives you a quick view of the containers running on your machine. 它可讓您快速存取容器記錄,讓您可以在容器內取得 shell,並讓您輕鬆管理容器生命週期 (停止、移除等) 。It gives you quick access to container logs, lets you get a shell inside the container, and lets you easily manage container lifecycle (stop, remove, and so on).

若要存取擴充功能,請遵循 此處的指示。To access the extension, follow the instructions here. 使用左側的 Docker 圖示來開啟 Docker view。Use the Docker icon on the left to open the Docker view. 如果您現在開啟延伸模組,您將會看到此教學課程正在執行!If you open the extension now, you will see this tutorial running! ) 下的容器名稱 (angry_taussig 是隨機建立的名稱。The container name (angry_taussig below) is a randomly created name. 因此,您可能會有不同的名稱。So, you'll most likely have a different name.

在 Docker 擴充功能中執行的教學課程容器

什麼是容器What is a container

現在您已執行容器,什麼 容器?Now that you've run a container, what is a container? 簡單來說,容器只是電腦上的另一個程式,與主機電腦上的所有其他進程隔離。Simply put, a container is simply another process on your machine that has been isolated from all other processes on the host machine. 該隔離會利用 核心命名空間和 cgroups,在 Linux 中已有很長一段時間的功能。That isolation leverages kernel namespaces and cgroups, features that have been in Linux for a long time. Docker 已經努力讓這些功能平易近人且容易使用。Docker has worked to make these capabilities approachable and easy to use.

注意

從頭開始建立容器 如果您想要瞭解如何從頭開始建立容器,從青色安全性 Liz Rice 有一段影片,讓她從頭開始建立容器:Creating Containers from Scratch If you'd like to see how containers are built from scratch, Liz Rice from Aqua Security has a video in which she creates a container from scratch in Go:

什麼是容器映射What is a container image

執行容器時,它會使用隔離檔案系統。When running a container, it uses an isolated filesystem. 此自訂檔案系統是由 容器映射提供。This custom filesystem is provided by a container image. 因為映射包含容器的檔案系統,所以它必須包含執行應用程式所需的所有專案,包括所有相依性、設定、腳本、二進位檔等等。Since the image contains the container's filesystem, it must contain everything needed to run an application - all dependencies, configuration, scripts, binaries, and so on. 映射也包含容器的其他設定,例如環境變數、要執行的預設命令,以及其他中繼資料。The image also contains other configuration for the container, such as environment variables, a default command to run, and other metadata.

我們稍後會深入探討影像,其中涵蓋了分層、最佳作法等主題。We'll dive deeper into images later on, covering topics such as layering, best practices, and more.

注意

如果您很熟悉 chroot ,請將容器視為的擴充版本 chrootIf you're familiar with chroot, think of a container as an extended version of chroot. 檔案系統只是來自映射。The filesystem is simply coming from the image. 但是,當您只使用 chroot 時,容器會新增額外的隔離。But, a container adds additional isolation not available when simply using chroot.

後續步驟Next steps

繼續進行本教學課程!Continue with the tutorial!