符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
本篇内容主要讲解“Docker Compose的基本使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Docker Compose的基本使用方法”吧!
创新互联公司长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为江华企业提供专业的成都网站建设、网站建设,江华网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。
默认情况下在mac或者Windows安装docker的时候会自动安装docker-compose。对于Linux来说是需要独立安装docker compose。
前提是已经安装好docker。
去github上下载docker-compose-Linux-x86_64 。
然后将下载的compose文件 "docker-compose-Linux-x86_64“ 改名为 “docker-compose”,移动到 /usr/local/bin
sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
添加可执行权限:
sudo chmod +x /usr/local/bin/docker-compose
这样docker-compose就安装好了,查看version:
iie4bu@hostdocker:~$ docker-compose --version docker-compose version 1.24.1, build 4667896b
一般情况下,我们使用docker-compose时,往往会联合使用docker-compose.yml文件。
这个命令可以将docker-compose.yml文件中的services全部启动。
这个命令执行的文件名是docker-compose.yml。如果要指定文件,需要加上-f参数。例如:
docker-compose -f docker-compose-2.yml up
-f 的默认值是docker-compose.yml,所以可以不用写。
version: '3' services: wordpress: image: wordpress ports: - 8080:80 environment: WORDPRESS_DB_HOST: MySQL 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
执行命令:
docker-compose up
Creating docker-compose-dir_wordpress_1 ... done Creating docker-compose-dir_mysql_1 ... done Attaching to docker-compose-dir_mysql_1, docker-compose-dir_wordpress_1 mysql_1 | Initializing database wordpress_1 | WordPress not found in /var/www/html - copying now... wordpress_1 | Complete! WordPress has been successfully copied to /var/www/html mysql_1 | 2019-07-04T14:41:38.299244Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. mysql_1 | 2019-07-04T14:41:38.299306Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.16) initializing of server in progress as process 27 mysql_1 | 2019-07-04T14:41:41.616051Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. wordpress_1 | [04-Jul-2019 14:41:43 UTC] PHP Warning: mysqli::__construct(): (HY000/2002): Connection refused in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2002) Connection refused mysql_1 | 2019-07-04T14:41:43.800607Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (mysqld 8.0.16) initializing of server has completed mysql_1 | Database initialized mysql_1 | MySQL init process in progress... wordpress_1 | wordpress_1 | MySQL Connection Error: (2002) Connection refused mysql_1 | 2019-07-04T14:41:45.680731Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. mysql_1 | 2019-07-04T14:41:45.680839Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 78 mysql_1 | 2019-07-04T14:41:46.335401Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. mysql_1 | 2019-07-04T14:41:46.338587Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. mysql_1 | 2019-07-04T14:41:46.401763Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.16' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server - GPL. mysql_1 | 2019-07-04T14:41:46.638569Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it. mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it. wordpress_1 | wordpress_1 | MySQL Connection Error: (2002) Connection refused mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it. mysql_1 | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it. mysql_1 | mysql: [Warning] Using a password on the command line interface can be insecure. mysql_1 | mysql_1 | 2019-07-04T14:41:52.014266Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.16) MySQL Community Server - GPL. mysql_1 | mysql_1 | MySQL init process done. Ready for start up. mysql_1 | wordpress_1 | wordpress_1 | MySQL Connection Error: (2002) Connection refused mysql_1 | 2019-07-04T14:41:52.395589Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. mysql_1 | 2019-07-04T14:41:52.395700Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 1 mysql_1 | 2019-07-04T14:41:52.795509Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed. mysql_1 | 2019-07-04T14:41:52.797900Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. mysql_1 | 2019-07-04T14:41:52.831221Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.16' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL. mysql_1 | 2019-07-04T14:41:52.902234Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060 wordpress_1 | [04-Jul-2019 14:41:55 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:41:55 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | [04-Jul-2019 14:41:58 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:41:58 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | [04-Jul-2019 14:42:01 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:42:01 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | [04-Jul-2019 14:42:04 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:42:04 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | [04-Jul-2019 14:42:07 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:42:07 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | [04-Jul-2019 14:42:10 UTC] PHP Warning: mysqli::__construct(): The server requested authentication method unknown to the client [caching_sha2_password] in Standard input code on line 22 wordpress_1 | [04-Jul-2019 14:42:10 UTC] PHP Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in Standard input code on line 22 wordpress_1 | wordpress_1 | MySQL Connection Error: (2054) The server requested authentication method unknown to the client wordpress_1 | wordpress_1 | WARNING: unable to establish a database connection to 'mysql' wordpress_1 | continuing anyways (which might have unexpected results) wordpress_1 | wordpress_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.2. Set the 'ServerName' directive globally to suppress this message wordpress_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.2. Set the 'ServerName' directive globally to suppress this message wordpress_1 | [Thu Jul 04 14:42:10.695076 2019] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.25 (Debian) PHP/7.3.6 configured -- resuming normal operations wordpress_1 | [Thu Jul 04 14:42:10.696660 2019] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND' mysql_1 | mbind: Operation not permitted
先是生成了docker-compose-dir_wordpress_1和docker-compose-dir_mysql_1,然后接下来是各自的日志输出。
查看当前有那些service在启动
docker-compose ps Name Command State Ports ---------------------------------------------------------------------------------------------- docker-compose-dir_mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp docker-compose-dir_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:8080->80/tcp
这个命令会停止当前运行的services
docker-compose stop Stopping docker-compose-dir_mysql_1 ... done Stopping docker-compose-dir_wordpress_1 ... done
启动services
docker-compose start Starting wordpress ... done Starting mysql ... done
先停止container,然后删除container,然后删除network,但是并没有删除本地的image。
docker-compose down Stopping docker-compose-dir_mysql_1 ... done Stopping docker-compose-dir_wordpress_1 ... done Removing docker-compose-dir_mysql_1 ... done Removing docker-compose-dir_wordpress_1 ... done Removing network docker-compose-dir_my-bridge
会在后台启动container,但是不会打印输出log.
列举出docker中的container所使用的image
docker-compose images Container Repository Tag Image Id Size ---------------------------------------------------------------------------- docker-compose-dir_mysql_1 mysql 5.7 a1aa4f76fab9 356 MB docker-compose-dir_wordpress_1 wordpress latest a541a1a59631 427 MB
作用与docker exec 类似,比如执行docker-compose.yml中定义的mysql service,那么命令如下:
docker-compose exec mysql /bin/bash root@3669e2265f7d:/#
这样就进入了mysql service中去了。
注意,虽然我们使用docker contaienr ls 看到的container name:
docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3669e2265f7d mysql:5.7 "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 3306/tcp, 33060/tcp docker-compose-dir_mysql_1 1e882117f6a1 wordpress "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 0.0.0.0:8080->80/tcp docker-compose-dir_wordpress_1
但是我们的service name 并不是docker-compose-dir_mysql_1,而是我们在docker-compose.yml中定义的mysql
docker network ls也同理,虽然显示的网络名称是docker-compose-dir_my-bridge,但真正的名称是在docker-compose.yml中定义的my-bridge
docker network ls NETWORK ID NAME DRIVER SCOPE 52a131a54732 bridge bridge local 3ba28f3c55c6 docker-compose-dir_my-bridge bridge local 8212167bd2d1 host host local 089cabdcfa5a none null local
我们还是使用之前的flask-redis项目:
新建文件夹flask-redis,文件夹下新建app.py,内容如下:
from flask import Flask from redis import Redis import os import socket app = Flask(__name__) redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379) @app.route('/') def hello(): redis.incr('hits') return 'Hello Container World! I have bean seen %s times and my hostname is %s. \n' % (redis.get('hits'), socket.gethostname()) if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=True)
新建Dockerfile,内容如下:
FROM python:3.5 LABEL maintainer="vincent" 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
这次我们不是使用拉取image的方式,而是使用Dockerfile方式去构建。分别指定:context是Dockerfile的位置,以及dockerfile的名字。
输出结果:
Building web Step 1/7 : FROM python:3.5 ---> 6da24b346f6e Step 2/7 : LABEL maintainer="vincent" ---> Using cache ---> 06d9f068cf9e Step 3/7 : COPY . /app ---> 7685dbbee266 Step 4/7 : WORKDIR /app Removing intermediate container d47965af9324 ---> acbb4ee85216 Step 5/7 : RUN pip install flask redis ---> Running in 46f5a5fc67ef Collecting flask Installing collected packages: Werkzeug, click, itsdangerous, MarkupSafe, Jinja2, flask, redis Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.4 click-7.0 flask-1.0.3 itsdangerous-1.1.0 redis-3.2.1 Removing intermediate container 46f5a5fc67ef ---> 393d35fbafa6 Step 6/7 : EXPOSE 5000 ---> Running in 64a952eff901 Removing intermediate container 64a952eff901 ---> 067f011bf9ae Step 7/7 : CMD [ "python", "app.py" ] ---> Running in 9511e8b91db4 Removing intermediate container 9511e8b91db4 ---> 3a601628a9b9 Successfully built 3a601628a9b9 Successfully tagged flask-redis_web:latest
访问:
curl 192.168.0.109:8080 Hello Container World! I have bean seen b'5' times and my hostname is 1282ec5ae336.
成功!
到此,相信大家对“Docker Compose的基本使用方法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!