《LINUX入門:容器:Ubuntu 16.04安裝Docker Compose詳解》要點:
本文介紹了LINUX入門:容器:Ubuntu 16.04安裝Docker Compose詳解,希望對您有用。如果有疑問,可以聯系我們。
什么是Docker Compose
Docker Compose是運行多容器Docker應用法式的工具.要使用Compose配置應用法式的服務,我們使用配置文件,然后執行單個命令,可以創建并啟動配置中指定的所有服務.
Docker Compose適用于許多分歧的項目,如:
在本教程中,我們將看到如安在Ubuntu 16.04機器上安裝Docker Compose.
安裝Docker
為了安裝Docker Compose,我們必要Docker.首先,為官方Docker存儲庫添加公鑰:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add-
接下來,將Docker存儲庫添加到apt源列表中:
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
更新數據庫包,使用用apt安裝Docker:
$ sudo apt-get update
$ sudo apt install docker-ce
在安裝過程結束時,Docker守護法式應該啟動,以便在引導時加載. 我們可以使用以下命令檢查其狀態:
$ sudo systemctl status docker --------------------------------- ● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running)
此時可以安裝Docker Compose. 通過執行以下命令下載當前版本:
# curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
去除執行權限:
# chmod +x /usr/local/bin/docker-compose
檢查Docker Compose版本:
$ docker-compose -v
輸出應該是這樣的:
docker-compose version 1.14.0, build c7bdf9e
Docker Hub包括用于演示的Hello World鏡像,說明了使用Docker Compose運行容器所需的配置.
創立一個新的目錄并進入它:
$ mkdir hello-world $ cd hello-world
創立一個新的YAML文件:
$ $EDITOR docker-compose.yml
在此文件中粘貼以下內容:
unixmen-compose-test: image: hello-world
注意:第一行用作容器名稱的一部門.
保留并退出.
$ sudo docker-compose up
如果一切正確,這應該是Compose所顯示的輸出:
Pulling unixmen-compose-test (hello-world:latest)... latest: Pulling from library/hello-world b04784fba78d: Pull complete Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f Status: Downloaded newer image for hello-world:latest Creating helloworld_unixmen-compose-test_1 ... Creating helloworld_unixmen-compose-test_1 ... done Attaching to helloworld_unixmen-compose-test_1 unixmen-compose-test_1 | unixmen-compose-test_1 | Hello from Docker! unixmen-compose-test_1 | This message shows that your installation appears to be working correctly. unixmen-compose-test_1 | unixmen-compose-test_1 | To generate this message, Docker took the following steps: unixmen-compose-test_1 | 1. The Docker client contacted the Docker daemon. unixmen-compose-test_1 | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. unixmen-compose-test_1 | 3. The Docker daemon created a new container from that image which runs the unixmen-compose-test_1 | executable that produces the output you are currently reading. unixmen-compose-test_1 | 4. The Docker daemon streamed that output to the Docker client, which sent it unixmen-compose-test_1 | to your terminal. unixmen-compose-test_1 | unixmen-compose-test_1 | To try something more ambitious, you can run an Ubuntu container with: unixmen-compose-test_1 | $ docker run -it ubuntu bash unixmen-compose-test_1 | unixmen-compose-test_1 | Share images, automate workflows, and more with a free Docker ID: unixmen-compose-test_1 | https://cloud.docker.com/ unixmen-compose-test_1 | unixmen-compose-test_1 | For more examples and ideas, visit: unixmen-compose-test_1 | https://docs.docker.com/engine/userguide/ unixmen-compose-test_1 | helloworld_unixmen-compose-test_1 exited with code 0
只要命令處于活動狀態,Docker容器就會運行,因此當測試完成運行時,容器將停止.
總結
本文介紹了關于在Ubuntu 16.04機器上安裝Docker Compose的教程. 我們還看到了如何通過YAML格式的Compose文件創立一個簡單的項目.
更多LINUX教程,盡在維易PHP學院專欄。歡迎交流《LINUX入門:容器:Ubuntu 16.04安裝Docker Compose詳解》!
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/9806.html