需求描述
很多時(shí)候我們需要多個(gè)容器配合使用,但是一個(gè)一個(gè)的去配置部署是一件很麻煩的事情,這時(shí)候就需要一個(gè)能夠一處配置,到處使用的方案,于是就有了compose。
Compose 安裝
安裝比較簡(jiǎn)單,查一下文檔就知道了,比如這里要安裝1.21.2版本:
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose # 下載可執(zhí)行文件
sudo chmod +x /usr/local/bin/docker-compose # 如果需要的話(huà),添加可執(zhí)行權(quán)限
docker-compose --version # 試一試安裝成功了沒(méi)有
一個(gè)例子
compose默認(rèn)使用名為docker-compose.yml
的配置文件,我們先來(lái)試一試。
下面的例子創(chuàng)建了一組container,主體部分是一個(gè)wordpress
服務(wù),使用mysql
作為數(shù)據(jù)庫(kù)。
docker-compose.yml
version: '3'
services:
wordpress:
image: wordpress
ports:
- 8080:80
depends_on:
- mysql
environment:
WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_PASSWORD: root
networks:
- my-bridge
mysql:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
volumes:
- mysql-data:/var/lib/mysql
networks:
- my-bridge
volumes:
mysql-data:
networks:
my-bridge:
driver: bridge
然后在命令行執(zhí)行
docker-compose up
程序便會(huì)在當(dāng)前目錄下找到docker-compose.yml
文件并根據(jù)配置創(chuàng)建相應(yīng)的container。
如果要指定yml
文件, 可執(zhí)行
docker-compose -f youfile.yml up
這樣的話(huà),運(yùn)行的日志會(huì)持續(xù)打印,如果要讓其后臺(tái)運(yùn)行的話(huà),和docker一樣,加入-d
參數(shù)就可以了。
然后執(zhí)行一下docker ps
,看看container是否正常啟動(dòng)。
最后打開(kāi)瀏覽器,看一看是否能打開(kāi)wordpress
。
更多命令
compose 還有許多功能,我們可以使用docker-compose --help
查看
docker-compose --help
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [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
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-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
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert deploy
keys in v3 files to their non-Swarm equivalent
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
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
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push 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
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
其中很多命令和docker
的差不多,比較容易理解。
再來(lái)一個(gè)例子
如果我們需要自己創(chuàng)建一個(gè)image
來(lái)啟動(dòng)container
怎么弄的
Dockerfile
FROM python:2.7
LABEL maintaner="Peng Xiao xiaoquwl@gmail.com"
COPY . /app
WORKDIR /app
RUN pip install flask redis
EXPOSE 5000
CMD [ "python", "app.py" ]
docker-compose.yml
version: "3"
services:
redis:
image: redis
web:
build:
context: .
dockerfile: Dockerfile
ports:
- 8080:5000
environment:
REDIS_HOST: redis
然后創(chuàng)建的方式和上面一樣。