[docker] 四.Docker的三大帮手之一,docker-compose

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_36964056/article/details/85339306

Docker-compose

dokcer-compose是什么:

docker-compose 是一个用来把 docker 自动化的东西。
有了 docker-compose 你可以把所有繁复的 docker 操作全都一条命令,自动化的完成。

为什么要用 docker-compose,他解决了什么

用通俗的语言来说,我们平时操作 docker 还是很原始的一系列动作,你手动使用 docker 的动作可以拆分成

找到一个系统镜像 // docker search
安装好 vm 或者 virtual box // apt-get install docker
在 vm中安装镜像 // docker run -d -it 你的镜像
略…

这是最小的动作, 如果你要映射硬盘,设置nat网络或者桥接网络,等等…你就要做更多的 docker 操作, 这显然是非常没有效率的。
但是我们写在 docker-compose.file 里面就很好了。 你只需要写好后 只运行一句
docker-compose up -d
一切都是那么的简单
安装docker-compose

pip install docker-compose

docker-compose 常用命令

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
  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
解释一下

build 构建或重建服务
help 命令帮助
kill 杀掉容器
logs 显示容器的输出内容
port 打印绑定的开放端口
ps 显示容器
pull 拉取服务镜像
restart 重启服务
rm 删除停止的容器
run 运行一个一次性命令
scale 设置服务的容器数目
start 开启服务
stop 停止服务
up 创建并启动容器

docker-compose 如何配置

先看看我自己写的一个 docker-compose.yml

version: '2'
services:
    nginx:
            image: bitnami/nginx:latest
            ports:
                - '80:80'
                - '1443:443'
            volumes:
                - /root/wp_yunlan/nginx/:/bitnami/nginx/
    mariadb:
            image: bitnami/mariadb:latest
            volumes:
                - /root/wp_yunlan/mariadb:/bitnami/mariadb
    wordpress:
            image: bitnami/wordpress:latest
            depends_on:
                - mariadb
                - nginx
            environment:
                - WORDPRESS_USERNAME=neptunemoon    #这个账户你是自己设定的
                - WORDPRESS_PASSWORD=123123         #这个密码是你自己设定的
            ports:
                - '8080:80'
                - '8081:443'
            volumes:
                - /root/wp_yunlan/wordpress:/bitnami/wordpress
                - /root/wp_yunlan/apache:/bitnami/apache
                - /root/wp_yunlan/php:/bitnami/php

nginx 和 mariadb,wordpress 是要启动的三个服务
顺序不是重要的,我们看见wordpress中有个 depends_on: 的属性
depends_on: 依赖
代表wordpress 依赖于

  • mariadb

  • nginx
    两个服务, 所以他们两个会先启动
    image: 镜像
    就是你的 docker 镜像
    我们用
    docker search mariadb
    找到我们需要的镜像

    root@ubuntu:~/test# docker search mariadb
    NAME DESCRIPTION STARS OFFICIAL AUTOMATED
    mariadb MariaDB is a community-developed fork of M… 1192 [OK]
    paintedfox/mariadb A docker image for running MariaDB 5.5, a … 29 [OK]
    bitnami/mariadb Bitnami MariaDB Docker Image 29 [OK]
    million12/mariadb MariaDB 10 on CentOS-7 with UTF8 defaults 12 [OK]
    toughiq/mariadb-cluster Dockerized Automated MariaDB Galera Cluste… 9 [OK]
    webhippie/mariadb Docker images for mariadb 6 [OK]
    panubo/mariadb-galera MariaDB Galera Cluster 6 [OK]
    kakilangit/mariadb Docker for MariaDB with OQGraph & TokuDB E… 5 [OK]
    maxexcloo/mariadb Service container with MariaDB installed a… 4 [OK]
    tcaxias/mariadb MariaDB containers 1 [OK]
    desertbit/mariadb This is an extended docker image of the of… 1 [OK]
    russmckendrick/mariadb A MariaDB image 1 [OK]
    drupaldocker/mariadb MariaDB for Drupal 1 [OK]
    jpco/mariadb Mariadb, so I can have it on my raspberry 1 [OK]
    clearlinux/mariadb MariaDB Server 1 [OK]
    danielsreichenbach/mariadb Minimal MariaDB container to be used as co… 0 [OK]
    lucidfrontier45/mariadb Mariadb with some customizable properties 0 [OK]
    codete/mariadb MariaDB docker image used at Codete. 0 [OK]
    dogstudio/mariadb MariaDB Container for Dogs 0 [OK]
    babim/mariadb Mariadb Server on Alpine or Debian. Check … 0 [OK]
    objectstyle/mariadb ObjectStyle MariaDB Docker Image 0 [OK]
    oriaks/mariadb MariaDB 0 [OK]
    yannickvh/mariadb Custom build of MariaDB based on the offic… 0 [OK]
    gymnae/mariadb Alpine based simple mariadb 0 [OK]
    nimmis/mariadb MariaDB multiple versions based on nimmis/… 0 [OK]
    root@ubuntu:~/test#

    扫描二维码关注公众号,回复: 7649507 查看本文章

好了,就是bitnami/mariadb了
environment 环境变量

这个是在好理解不过的了。 不过这和我们程序语言设计层面的还是不一样的,这个是容器层面的环境变量。
如果我们写程序做一些逻辑判断的时候,肯定会使用 比如我们判断现在的编译器,我们会使用
#if GNUC 或者 #if _MSC_VER 相应的,我们的容器里面肯定也有这样的逻辑,我们经常使用环境变量来传值,或者定义一个行为。写过程序的人都懂。

ports 端口映射

映射本机还有镜像的端口。这个没有什么好说的。

volumes 文件映射

有两种格式,
可以对应 docker 操作中的 -v my/path/:/docker/path
还可以使用单方面的 -v /path
这样的话 就相当于 一个匿名映射, 其实还是在本机有对应目录的。
使用docker inspect -f {{.Volumes}} /path 可以看到详细信息
相对这个了解更多的 深入理解Docker Volume

docker-compose 需要注意的
我根据我自己的体验,给出几点需要注意的

  1. 不要把 docker 当做数据容器来使用,数据一定要用 volumes 放在容器外面
  2. 不要把 docker-compose 文件暴露给别人, 因为上面有你的服务器信息
  3. 多用 docker-compose 的命令去操作, 不要用 docker 手动命令&docker-compose 去同时操作
  4. 写一个脚本类的东西,自动备份docker 映射出来的数据。
  5. 不要把所有服务都放在一个 docker 容器里面

猜你喜欢

转载自blog.csdn.net/weixin_36964056/article/details/85339306
今日推荐