环境:
centos 7.5
docker 20.10.2
docker-compose 1.25.5
[root@localhost ~]# cat /etc/redhat-releasecentos linux release 7.5.1804 (core)[root@localhost ~]# docker version # docker版本client: docker engine - community version: 20.10.2 api version: 1.41 go version: go1.13.15 git commit: 2291f61 built: mon dec 28 16:17:48 2020 os/arch: linux/amd64 context: default experimental: trueserver: docker engine - community engine: version: 20.10.2 api version: 1.41 (minimum version 1.12) go version: go1.13.15 git commit: 8891c58 built: mon dec 28 16:16:13 2020 os/arch: linux/amd64 experimental: false containerd: version: 1.4.3 gitcommit: 269548fa27e0089a8b8278fc4fc781d7f65a939b runc: version: 1.0.0-rc92 gitcommit: ff819c7e9184c13b7c2607fe6c30ae19403a7aff docker-init: version: 0.19.0 gitcommit: de40ad0[root@localhost ~]# docker-compose version # docker-compose版本docker-compose version 1.25.5, build 8a1c60f6docker-py version: 4.1.0cpython version: 3.7.5openssl version: openssl 1.1.0l 10 sep 2019
参考:
docker-compose官方搭建wordpress教程:https://docs.docker.com/compose/wordpress/
1.下载项目(docker-compose.yml)
[root@localhost ~]# cd /home/[root@localhost home]# mkdir my_wordpress[root@localhost home]# cd my_wordpress/[root@localhost my_wordpress]# vim docker-compose.ymlversion: '3.3' #compose文件版本services: db: # 服务1:db image: mysql:5.7 # 使用镜像 mysql:5.7版本 volumes: - db_data:/var/lib/mysql # 数据持久化 restart: always # 容器服务宕机后总是重启 environment: # 环境配置 mysql_root_password: somewordpress mysql_database: wordpress mysql_user: wordpress mysql_password: wordpress wordpress: # 服务2:wordpress depends_on: # wordpress服务启动时依赖db服务,所以会自动先启动db服务 - db image: wordpress:latest # 使用镜像 wordpress:latest最新版 ports: - "8000:80" #端口映射8000:80 restart: always environment: # 环境 wordpress_db_host: db:3306 # wordpress连接db的3306端口 wordpress_db_user: wordpress # wordpress的数据库用户为wordpress wordpress_db_password: wordpress # wordpress的数据库密码是wordpress wordpress_db_name: wordpress # wordpress的数据库名字是wordpressvolumes: db_data: {}
说明:
可以看到上面的docker-compose.yml文件中第一行为version:'3.3',这个是compose文件的版本,它是与docker引擎的版本相对应的,并且向下兼容。
2.启动项目
注意:大部分的compose命令都需要到docker-compose.yml文件所在的目录下才能执行。
前台启动项目: docker-compose up
后台执行该服务可以加上 -d 参数: docker-compose up -d
指定yml文件启动:docker-compose -f 【yml文件绝对路径】 up / -d
[root@localhost my_wordpress]# docker-compose up -d # 后台运行项目creating network "my_wordpress_default" with the default driver -----》# 为wordpress项目创建my_wordpress_default网络creating volume "my_wordpress_db_data" with default driverpulling db (mysql:5.7)...5.7: pulling from library/mysql6ec7b7d162b2: downloading [======================================> ] 20.64mb/27.1mbfedd960d3481: download complete7ab947313861: download complete64f92f19e638: downloading [=============================================> ] 1.278mb/1.419mb3e80b17bff96: download complete014e976799f9: waiting59ae84fee1b3: waiting7d1da2a18e2e: waiting301a28b700b9: waiting529dc8dbeaf3: waitingbc9d021dc13f: waiting529dc8dbeaf3: pull completebc9d021dc13f: pull completedigest: sha256:c3a567d3e3ad8b05dfce401ed08f0f6bf3f3b64cc17694979d5f2e5d78e10173status: downloaded newer image for mysql:5.7pulling wordpress (wordpress:latest)...latest: pulling from library/wordpress6ec7b7d162b2: already existsdb606474d60c: pull completeafb30f0cd8e0: pull complete3bb2e8051594: pull complete4c761b44e2cc: pull completec2199db96575: pull complete1b9a9381eea8: pull complete50f0689715a3: pull complete8a6cc018dd45: pull complete052299cf2d76: pull completeee83da709c88: pull complete5b10df91e6d0: pull completea2eb858e27d8: pull completee6269830d5ad: pull complete228a0fa8a95f: pull complete91f7abe86332: pull complete5c3d3e1e4145: pull completee75d27a32f14: pull complete3c632295f58e: pull complete84352e306791: pull completedigest: sha256:e3a851040e7eef9c2b6dd954bfcf08b5a9847b2efbc252d4ccb1b0864225d9fcstatus: downloaded newer image for wordpress:latestcreating my_wordpress_db_1 ... donecreating my_wordpress_wordpress_1 ... doneattaching to my_wordpress_db_1, my_wordpress_wordpress_1db_1 | 2021-01-09 03:56:06+00:00 [note] [entrypoint]: entrypoint script for mysql server 5.7.32-1debian10 started.db_1 | 2021-01-09 03:56:06+00:00 [note] [entrypoint]: switching to dedicated user 'mysql'db_1 | 2021-01-09 03:56:06+00:00 [note] [entrypoint]: entrypoint script for mysql server 5.7.32-1debian10 started.db_1 | 2021-01-09 03:56:06+00:00 [note] [entrypoint]: initializing database filesdb_1 | 2021-01-09t03:56:06.498622z 0 [warning] timestamp with implicit default value is deprecated. please use --explicit_defaults_for_timestamp server option (see documentation for more details).db_1 | 2021-01-09t03:56:06.956978z 0 [warning] innodb: new log files created, lsn=45790db_1 | 2021-01-09t03:56:06.992692z 0 [warning] innodb: creating foreign key constraint system tables.db_1 | 2021-01-09t03:56:07.053810z 0 [warning] no existing uuid has been found, so we assume that this is the first time that this server has been started. generating a new uuid: 99ff1af4-522e-11eb-a33c-0242ac130002.db_1 | 2021-01-09t03:56:07.054442z 0 [warning] gtid table is not ready to be used. table 'mysql.gtid_executed' cannot be opened.wordpress_1 | wordpress not found in /var/www/html - copying now...wordpress_1 | complete! wordpress has been successfully copied to /var/www/htmldb_1 | 2021-01-09t03:56:07.578842z 0 [warning] ca certificate ca.pem is self signed.wordpress_1 | [09-jan-2021 03:56:07 utc] php warning: mysqli::__construct(): (hy000/2002): connection refused in standard input code on line 22...db_1 | 2021-01-09t03:56:16.546749z 0 [note] skipping generation of ssl certificates as certificate files are present in data directory.db_1 | 2021-01-09t03:56:16.547255z 0 [warning] ca certificate ca.pem is self signed.db_1 | 2021-01-09t03:56:16.547310z 0 [note] skipping generation of rsa key pair as key files are present in data directory.db_1 | 2021-01-09t03:56:16.547780z 0 [note] server hostname (bind-address): '*'; port: 3306db_1 | 2021-01-09t03:56:16.547866z 0 [note] ipv6 is available.db_1 | 2021-01-09t03:56:16.547878z 0 [note] - '::' resolves to '::';db_1 | 2021-01-09t03:56:16.547892z 0 [note] server socket created on ip: '::'.db_1 | 2021-01-09t03:56:16.548648z 0 [warning] insecure configuration for --pid-file: location '/var/run/mysqld' in the path is accessible to all os users. consider choosing a different directory.db_1 | 2021-01-09t03:56:16.555145z 0 [note] event scheduler: loaded 0 eventsdb_1 | 2021-01-09t03:56:16.555312z 0 [note] mysqld: ready for connections.db_1 | version: '5.7.32' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mysql community server (gpl)wordpress_1 | ah00558: apache2: could not reliably determine the server's fully qualified domain name, using 172.19.0.3. set the 'servername' directive globally to suppress this messagewordpress_1 | ah00558: apache2: could not reliably determine the server's fully qualified domain name, using 172.19.0.3. set the 'servername' directive globally to suppress this messagewordpress_1 | [sat jan 09 03:56:16.984658 2021] [mpm_prefork:notice] [pid 1] ah00163: apache/2.4.38 (debian) php/7.4.14 configured -- resuming normal operationswordpress_1 | [sat jan 09 03:56:16.984711 2021] [core:notice] [pid 1] ah00094: command line: 'apache2 -d foreground'
`docker网络(重要)`
可以看到在使用docker-compose管理项目的时候,docker-compose会自动给我们创建一个项目网络,这是docker-compose的网络默认规则,创建的项目网络可以方便我们管理维护项目,同一个项目的所有服务可以直接互相通过服务名访问!!!
这样的一个好处是:开发不需要在配置中写死某个服务的ip地址,只需要写服务名即可,当该服务异常挂掉,同一个服务重启后ip可能会不同,如果写死的话就得去更改,很麻烦。所以这就是docker-compose默认创建网络提供的便利性。
[root@localhost ~]# docker network lsnetwork id name driver scopeeb2d2ed62e6d bridge bridge local40a2cdab761b host host localaddc87976b45 my_wordpress_default bridge local #项目网络446393a43e7b none null local[root@localhost ~]# docker network inspect my_wordpress_default #查看项目网络详情[ { "name": "my_wordpress_default", "id": "addc87976b4535cdb24c84a00afc9744b5951a520e7230bc8ef76189ea06bf05", "created": "2021-01-09t11:55:20.242710185+08:00", "scope": "local", "driver": "bridge", "enableipv6": false, "ipam": { "driver": "default", "options": null, "config": [ { "subnet": "172.19.0.0/16", "gateway": "172.19.0.1" } ] }, "internal": false, "attachable": true, "ingress": false, "configfrom": { "network": "" }, "configonly": false, "containers": { "c95da3fa0f6c42a564d99b35f70e5c51c5a1163ab53b9a6ae305f98d6b2b6f9f": { "name": "my_wordpress_db_1", "endpointid": "e1a66aec2e36fb74d0d75af69ff6c74c20c06508bfe2d8c34384d45062007c25", "macaddress": "02:42:ac:13:00:02", "ipv4address": "172.19.0.2/16", "ipv6address": "" }, "cccd067d18e46beb27a345721148569464ad618c1cfd99357fe4b80d75e7ff27": { "name": "my_wordpress_wordpress_1", "endpointid": "f238817ce5358d7b57a1668ea9ddda40c88a227c2c1d762fa553e38fd22d7e2b", "macaddress": "02:42:ac:13:00:03", "ipv4address": "172.19.0.3/16", "ipv6address": "" } }, "options": {}, "labels": { "com.docker.compose.network": "default", "com.docker.compose.project": "my_wordpress", "com.docker.compose.version": "1.25.5" } }]