Docker Compose 多容器部署

需求描述

很多時候我們需要多個容器配合使用,但是一個一個的去配置部署是一件很麻煩的事情,這時候就需要一個能夠一處配置,到處使用的方案,于是就有了compose。

Compose 安裝

安裝比較簡單,查一下文檔就知道了,比如這里要安裝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   # 如果需要的話,添加可執(zhí)行權(quán)限

docker-compose --version  # 試一試安裝成功了沒有

一個例子

compose默認(rèn)使用名為docker-compose.yml的配置文件,我們先來試一試。

下面的例子創(chuàng)建了一組container,主體部分是一個wordpress服務(wù),使用mysql作為數(shù)據(jù)庫。

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

程序便會在當(dāng)前目錄下找到docker-compose.yml文件并根據(jù)配置創(chuàng)建相應(yīng)的container。

如果要指定yml文件, 可執(zhí)行

docker-compose -f youfile.yml up

這樣的話,運(yùn)行的日志會持續(xù)打印,如果要讓其后臺運(yùn)行的話,和docker一樣,加入-d參數(shù)就可以了。

然后執(zhí)行一下docker ps,看看container是否正常啟動。

最后打開瀏覽器,看一看是否能打開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的差不多,比較容易理解。

再來一個例子

如果我們需要自己創(chuàng)建一個image來啟動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)建的方式和上面一樣。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容