如何使用Docker Compose构建和运行应用程序
Docker compose(以前称为Fig)是一种可以使用Docker定义和运行复杂应用程序的工具。
基本上,它完成了创建多个容器以及它们之间的链接的工作。
使用Docker Compose要求使用Dockerfile定义应用程序所需的环境,并在'.yml'文件中定义应用程序所需的服务。
然后,我们可以使用一个命令从配置文件中创建并启动所有服务。
在本文中,我们将学习如何在Ubuntu 15.10上安装和使用compose。
我们还将介绍如何使用它来运行应用程序。
安装
安装Docker Compose的前提条件是Docker Engine。
如果我们尚未安装Docker Engine,请按照以下步骤进行操作:
$sudo apt-get update $sudo apt-get install linux-image-extra-$(uname -r) $sudo apt-get install docker-engine
为了验证Docker安装是否成功,让我们运行一个hello-world程序:
$sudo docker run hello-world
poornima@BNPTHKPD:~$sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 03f4658f8b78: Pull complete a3ed95caeb02: Pull complete Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7 Status: Downloaded newer image for hello-world:latest Hello from Docker. This message shows that your installation appears to be working correctly. .......
现在要安装Compose,请在终端中执行以下curl命令:
$curl -L https://github.com/docker/compose/releases/download/1.7.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
将可执行权限设置为二进制
$chmod +x /usr/local/bin/docker-compose
验证安装是否完成:
$docker compose --version
root@BNPTHKPD:/home/poornima# docker-compose --version docker-compose version 1.7.0, build 0d7bf73
Docker撰写命令
在继续学习如何使用Compose运行应用程序之前,让我向我们介绍其一些重要命令。
docker-compose命令的语法是
docker-compose [-f=...] [options] [commands] [args]
可以使用以下命令验证docker-compose的版本:
docker-compose -v, --version
与“ ps”命令一起使用时,它列出了可用的容器
docker-compose ps
我们可以使用“ up”命令创建和启动容器,并添加“ -d”选项将在后台运行容器
docker-compose up -d
下面列出了一些可与docker-compose结合使用的命令:
build - to build services pause/unpause - pause and unpause services stop - stop services restart - restart services rm - remove stopped containers logs - view output from containers help - to get help on a command
生成并运行应用
在本节中,我们将重点介绍构建一个简单的Python Web应用程序并使用Docker Compose运行它。
设置
让我们首先为我们的应用程序创建一个目录。
poornima@BNPTHKPD:~$mkdir dockcompose poornima@BNPTHKPD:~$cd dockcompose/
现在,我们将使用此目录中的Flask框架创建一个Hello World Web应用程序myapp.py,其内容如下:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World using Docker Compose' if __name__ == '__main__': app.run(debug=True,host='0.0.0.0')
在包含以下内容的同一项目目录中创建requirements.txt文件:
Flask==0.10.1
在下一步中,我们需要创建一个Docker镜像,其中包含Python应用程序需要的所有依赖项。
创建一个Docker镜像
为此,请在项目目录中再次创建一个名为Dockerfile的文件,其内容如下:
FROM python:2.7 ADD . /code WORKDIR /code RUN pip install -r requirements.txt CMD python myapp.py
该文件通知Docker使用Python 2.7构建镜像,并添加目录“。
”。
进入图像中的路径“/code”,将工作目录设置为/code,按照Requirements.txt文件中的说明安装Python依赖项,并将容器的默认命令设置为“ python myapp.py”
现在,我们将构建图像。
$docker build -t web .
poornima@BNPTHKPD:~/dockcompose$sudo docker build -t web . Sending build context to Docker daemon 5.632 kB Step 1 : FROM python:2.7 ---> a3b29970a425 Step 2 : ADD . /code ---> 855e1a126850 Removing intermediate container 2e713165c053 Step 3 : WORKDIR /code ---> Running in 431e3f52f421 ---> 157b4cffd6df Removing intermediate container 431e3f52f421 Step 4 : RUN pip install -r requirements.txt ---> Running in 07c294591a76 Collecting Flask==0.10.1 (from -r requirements.txt (line 1)) Downloading Flask-0.10.1.tar.gz (544kB) Collecting Werkzeug>=0.7 (from Flask==0.10.1->-r requirements.txt (line 1)) Downloading Werkzeug-0.11.8-py2.py3-none-any.whl (306kB) Collecting Jinja2>=2.4 (from Flask==0.10.1->-r requirements.txt (line 1)) Downloading Jinja2-2.8-py2.py3-none-any.whl (263kB) Collecting itsdangerous>=0.21 (from Flask==0.10.1->-r requirements.txt (line 1)) Downloading itsdangerous-0.24.tar.gz (46kB) Collecting MarkupSafe (from Jinja2>=2.4->Flask==0.10.1->-r requirements.txt (line 1)) Downloading MarkupSafe-0.23.tar.gz Building wheels for collected packages: Flask, itsdangerous, MarkupSafe Running setup.py bdist_wheel for Flask: started Running setup.py bdist_wheel for Flask: finished with status 'done' Stored in directory: /root/.cache/pip/wheels/d2/db/61/cb9b80526b8f3ba89248ec0a29d6da1bb6013681c930fca987 Running setup.py bdist_wheel for itsdangerous: started Running setup.py bdist_wheel for itsdangerous: finished with status 'done' Stored in directory: /root/.cache/pip/wheels/97/c0/b8/b37c320ff57e15f993ba0ac98013eee778920b4a7b3ebae3cf Running setup.py bdist_wheel for MarkupSafe: started Running setup.py bdist_wheel for MarkupSafe: finished with status 'done' Stored in directory: /root/.cache/pip/wheels/94/a7/79/f79a998b64c1281cb99fa9bbd33cfc9b8b5775f438218d17a7 Successfully built Flask itsdangerous MarkupSafe Installing collected packages: Werkzeug, MarkupSafe, Jinja2, itsdangerous, Flask Successfully installed Flask-0.10.1 Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.11.8 itsdangerous-0.24 ---> 9ef07d3ed698 Removing intermediate container 07c294591a76 Step 5 : CMD python myapp.py ---> Running in ac5ce91ddc85 ---> 65d218cbea14 Removing intermediate container ac5ce91ddc85 Successfully built 65d218cbea14 poornima@BNPTHKPD:~/dockcompose$sudo docker-compose up Starting dockcompose_web_1 Attaching to dockcompose_web_1 web_1 | * Running on http://0.0.0.0:5000/(Press CTRL+C to quit) web_1 | * Restarting with stat web_1 | * Debugger is active! web_1 | * Debugger pin code: 151-559-328 web_1 | 172.17.0.1 - - [19/Apr/2015 08:17:43] "GET/HTTP/1.1" 200 web_1 | 172.17.0.1 - - [19/Apr/2015 08:17:43] "GET /favicon.ico HTTP/1.1" 404
该命令可以自动找到所需的Dockerfile,Requirements.txt和myapp.py文件,并使用它们来构建镜像“ web”。
服务文件
应用程序中要使用的不同服务应放在一个名为docker-compose.yml的文件中。
python HelloWorld应用程序的服务文件如下所示:
web: build: . ports: - "5000:5000"
上面的文件定义了从当前目录构建的Web服务,并将容器的公开端口5000转发到主机上的端口5000。
运行应用
现在,我们转到项目目录并运行该应用程序:
$docker-compose up
为了查看正在运行的应用程序,请将Web浏览器指向以下位置:
http://0.0.0.0:5000
这是浏览器为我们显示消息的方式:
更多命令
让我们尝试一些命令,看看它们如何工作。
注意:所有docker compose命令都应从包含docker-compose.yml的目录中运行
要在后台运行服务,请使用“ -d”选项。
poornima@BNPTHKPD:~/dockcompose$sudo docker-compose up -d Starting dockcompose_web_1
要列出所有容器,请使用“ ps”选项。
poornima@BNPTHKPD:~/dockcompose$sudo docker-compose ps Name Command State Ports ------------------------------------------------------------------------ dockcompose_web_ /bin/sh -c Up 0.0.0.0:5000->50 1 python myapp.py 00/tcp
要查看已配置服务(在本例中为Web)中可用的环境变量,请使用“ env”选项。
poornima@BNPTHKPD:~/dockcompose$sudo docker-compose run web env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=d5c2b9eeab7f TERM=xterm WEB_PORT=tcp://172.17.0.2:5000 WEB_PORT_5000_TCP=tcp://172.17.0.2:5000 WEB_PORT_5000_TCP_ADDR=172.17.0.2 WEB_PORT_5000_TCP_PORT=5000 WEB_PORT_5000_TCP_PROTO=tcp WEB_NAME=/dockcompose_web_run_1/web WEB_ENV_LANG=C.UTF-8 WEB_ENV_GPG_KEY=C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF WEB_ENV_PYTHON_VERSION=2.7.11 WEB_ENV_PYTHON_PIP_VERSION=8.1.1 WEB_1_PORT=tcp://172.17.0.2:5000 WEB_1_PORT_5000_TCP=tcp://172.17.0.2:5000 WEB_1_PORT_5000_TCP_ADDR=172.17.0.2 WEB_1_PORT_5000_TCP_PORT=5000 WEB_1_PORT_5000_TCP_PROTO=tcp WEB_1_NAME=/dockcompose_web_run_1/web_1 WEB_1_ENV_LANG=C.UTF-8 WEB_1_ENV_GPG_KEY=C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF WEB_1_ENV_PYTHON_VERSION=2.7.11 WEB_1_ENV_PYTHON_PIP_VERSION=8.1.1 DOCKCOMPOSE_WEB_1_PORT=tcp://172.17.0.2:5000 DOCKCOMPOSE_WEB_1_PORT_5000_TCP=tcp://172.17.0.2:5000 DOCKCOMPOSE_WEB_1_PORT_5000_TCP_ADDR=172.17.0.2 DOCKCOMPOSE_WEB_1_PORT_5000_TCP_PORT=5000 DOCKCOMPOSE_WEB_1_PORT_5000_TCP_PROTO=tcp DOCKCOMPOSE_WEB_1_NAME=/dockcompose_web_run_1/dockcompose_web_1 DOCKCOMPOSE_WEB_1_ENV_LANG=C.UTF-8 DOCKCOMPOSE_WEB_1_ENV_GPG_KEY=C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF DOCKCOMPOSE_WEB_1_ENV_PYTHON_VERSION=2.7.11 DOCKCOMPOSE_WEB_1_ENV_PYTHON_PIP_VERSION=8.1.1 LANG=C.UTF-8 GPG_KEY=C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF PYTHON_VERSION=2.7.11 PYTHON_PIP_VERSION=8.1.1 HOME=/root
为了停止容器,请使用“停止”选项:
poornima@BNPTHKPD:~/dockcompose$sudo docker-compose stop Stopping dockcompose_web_1 ... done
我们可以使用“暂停”和“取消暂停”选项来暂停和取消暂停它们。
poornima@BNPTHKPD:~/dockcompose$sudo docker-compose pause Pausing dockcompose_web_1 ... done poornima@BNPTHKPD:~/dockcompose$sudo docker-compose ps Name Command State Ports ------------------------------------------------------------------------ dockcompose_web_ /bin/sh -c Paused 0.0.0.0:5000->50 1 python myapp.py 00/tcp poornima@BNPTHKPD:~/dockcompose$sudo docker-compose unpause Unpausing dockcompose_web_1 ... done
使用“ kill”命令一次性停止所有容器。
$sudo docker-compose kill
可以使用“ log”命令查看撰写日志。
$sudo docker-compose logs
如果需要帮助列出所有可用的命令/选项,请使用help命令。
poornima@BNPTHKPD:~/dockcompose$sudo docker-compose --help Define and run multi-container applications with Docker. Usage: docker-compose [-f=...] [options] [COMMAND] [ARGS...] docker-compose -h|--help Options: -f, --file FILE Specify an alternate compose file (default: docker-compose.yml) -p, --project-name NAME Specify an alternate project name (default: directory name) --verbose Show more output -v, --version Print version and exit -H, --host HOST Daemon socket to connect to --tls Use TLS; implied by --tlsverify --tlscacert CA_PATH Trust certs signed only by this CA --tlscert CLIENT_CERT_PATH Path to TLS certificate file --tlskey TLS_KEY_PATH Path to TLS key file --tlsverify Use TLS and verify the remote --skip-hostname-check Don't check the daemon's hostname against the name specified in the client certificate (for example if your docker host is an IP address) Commands: build Build or rebuild services config Validate and view the compose file create Create services down Stop and remove containers, networks, images, and volumes events Receive real time events from containers exec Execute a command in a running container help Get help on a command kill Kill containers logs View output from containers pause Pause services port Print the public port for a port binding ps List containers pull Pulls service images restart Restart services rm Remove stopped containers run Run a one-off command scale Set number of containers for a service start Start services stop Stop services unpause Unpause services up Create and start containers version Show the Docker-Compose version information