当前位置: 代码迷 >> 综合 >> Docker学习笔记(6)——Docker Compose
  详细解决方案

Docker学习笔记(6)——Docker Compose

热度:49   发布时间:2023-11-13 23:54:24.0

Docker Compose编排容器

1.安装Compose

方法1.python pip
$ sudo pip install -U docker-compose
方法2:下载二进制包(主机,容器都可以这样操作)
$ sudo curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ sudo chmod a+x /usr/local/bin/docker-compose
容器中运行本质是下载了docker/compose镜像运行
卸载操作
$ sudo rm /usr/local/bin/docker-compose
$ sudo pip uninstall docker-compose

2.docker-compose常用命令

Commands:build              Build or rebuild servicescreate             Create servicesdown               Stop and remove containers, networks, images, and volumesexec               Execute a command in a running containerhelp               Get help on a commandkill               Kill containerslogs               View output from containerspause              Pause servicesport               Print the public port for a port bindingps                 List containerspull               Pull service imagespush               Push service imagesrestart            Restart servicesrm                 Remove stopped containersrun                Run a one-off commandscale              Set number of containers for a servicestart              Start servicesstop               Stop servicesup                 Create and start containersversion            Show the Docker-Compose version information

3.实例:Docker Compose创建WordPress站点

1).在文件路径下创建docker-compose.yml文件,本样例在YAML文件中定义:暴露的端口号,环境变量和已挂载的卷
wordpress:image:wordpress                 links:- mysqlports:- "80:80"environment:- WORDPRESS_DB_NAME=wordpress- WRODPRESS_DB_USER=wordpress- WORDPRESS_DB_PASSWORD=wordpresspwdmysql: image:mysql volumes:- /home/docker/mysql:/var/lib/mysqlenvironment:- MYSQL_ROOT_PASSWORD=wordpressdocker- MYSQL_DATABASE=wordpress- MYSQL_USER=wordpress- MYSQL_PASSWORD=wordpresspwd

2).在docker-compose所在文件目录下,在命令行中键入

$ docker-compose up -d
Creating dockercompose_mysql_1 ... 
Creating dockercompose_mysql_1 ... done
Creating dockercompose_wordpress_1 ... 
Creating dockercompose_wordpress_1 ... done

3).之后可以通过http://ip_of_host来访问这个WordPress站点

可以随时查看和控制这项服务

$ docker-compose ps
$ docker-compose stop wordpress
$ docker-compose start wordpress