从零开始学习docker(十二)介绍Docker Compose基本使用
撸了今年阿里、头条和美团的面试,我有一个重要发现.......>>>
默认情况下在mac或者Windows安装docker的时候会自动安装docker-compose。对于Linux来说是需要独立安装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 up
这个命令可以将docker-compose.yml文件中的services全部启动。
这个命令执行的文件名是docker-compose.yml。如果要指定文件,需要加上-f参数。例如:
docker-compose -f docker-compose-2.yml up
-f 的默认值是docker-compose.yml,所以可以不用写。
实验一
新建docker-compose.yml,内容如下:
version: '3'services:wordpress:image: wordpressports:- 8080:80environment:WORDPRESS_DB_HOST: mysqlWORDPRESS_DB_PASSWORD: rootnetworks:- my-bridgemysql:image: mysql:5.7environment:MYSQL_ROOT_PASSWORD: rootMYSQL_DATABASE: wordpressvolumes:- mysql-data:/var/lib/mysqlnetworks:- my-bridgevolumes: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,然后接下来是各自的日志输出。
docker-compose ps
查看当前有那些service在启动
docker-compose psName 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
docker-compose stop
这个命令会停止当前运行的services
docker-compose stop
Stopping docker-compose-dir_mysql_1 ... done
Stopping docker-compose-dir_wordpress_1 ... done
docker-compose start
启动services
docker-compose start
Starting wordpress ... done
Starting mysql ... done
docker-compose down
先停止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
docker-compose up -d
会在后台启动container,但是不会打印输出log.
docker-compose images
列举出docker中的container所使用的image
docker-compose imagesContainer 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-compose exec
作用与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 socketapp = 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: redisweb:build:context: .dockerfile: Dockerfileports:- 8080:5000environment: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(十二)介绍Docker Compose基本使用相关推荐
- Docker(十二):Docker+Jenkins+Nginx+Spring Boot 自动化部署项目
前言 虽然Docker已经是风靡全球的容器技术了,统一环境避免环境问题上是Docker的主要吸引点之一,但使用时详细还是会遇到不少问题的,比如个人搭建时曾思考过这些问题: Jenkins官网既然有Do ...
- 从零开始学习React——(十二):React单项数据流和混用jQuery以及函数式编程
本节主要介绍一些理论性的东西:如 React单向数据流.React和其他框架一起使用和函数式编程. 1. 单向数据流 React的特性中有一个概念叫单向数据流.可以用上几节的Demo来说一下何为单向数 ...
- 从零开始学习jQuery (十) jQueryUI常用功能实战
本系列文章导航 从零开始学习jQuery (一) 开天辟地入门篇 从零开始学习jQuery (二) 万能的选择器 从零开始学习jQuery (三) 管理jQuery包装集 从零开始学习jQuery ( ...
- Docker系列(二): 搭建docker镜像仓库-Nexus、Harbor
Docker介绍及环境搭建参考: Docker系列一: docker介绍&&安装(含离线安装) Docker系列(二): 搭建docker镜像仓库-Nexus.Harbor Docke ...
- python新手教程 从零开始-Python零基础从零开始学习Python十分钟快速入门
原标题:Python零基础从零开始学习Python十分钟快速入门 学习Python的,都知道Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言.Python是一种动态解释型的 ...
- 吴恩达《机器学习》学习笔记十二——机器学习系统
吴恩达<机器学习>学习笔记十二--机器学习系统 一.设计机器学习系统的思想 1.快速实现+绘制学习曲线--寻找重点优化的方向 2.误差分析 3.数值估计 二.偏斜类问题(类别不均衡) 三. ...
- Python语言入门这一篇就够了-学习笔记(十二万字)
Python语言入门这一篇就够了-学习笔记(十二万字) 友情提示:先关注收藏,再查看,12万字保姆级 Python语言从入门到精通教程. 文章目录 Python语言入门这一篇就够了-学习笔记(十二万字 ...
- HTML学习第十二章------布局和排版
HTML学习第十二章------布局和排版 教材:Head First HTML与CSS 布局 浏览器用流(flow)来布置页面上的XHTML元素, 即跟着元素的流(顺序)来进行显示. 块元素是从头流 ...
- OpenCV学习笔记(十二)——图像分割与提取
在图像处理的过程中,经常需要从图像中将前景对象作为目标图像分割或者提取出来.例如,在视频监控中,观测到的是固定背景下的视频内容,而我们对背景本身并无兴趣,感兴趣的是背景中出现的车辆.行人或者其他对象. ...
- C++语言学习(十二)——C++语言常见函数调用约定
C++语言学习(十二)--C++语言常见函数调用约定 一.C++语言函数调用约定简介 C /C++开发中,程序编译没有问题,但链接的时候报告函数不存在,或程序编译和链接都没有错误,但只要调用库中的函数 ...
最新文章
- rman备份脚本shell版
- memset用法详解
- Dockerfile 之 ARG指令详解及示例
- 文件上传函数(get post assert)
- shell统计游戏活跃用户数之改进过程分析
- 瑞士Migros Ostschweiz使用RFID和EPCIS优化供应链的可视化
- Android 中文字体的设置方法和使用技巧
- Linux编译安装GCC-5.1.0
- Asp.Net MVC 控制器
- 利用低代码从0到1开发一款小程序
- golang语言环境搭建
- Java进阶诀窍httpwww.china-pub.com44147
- 宁录哨兵机器人_当天启碰上哨兵机器人孰强孰弱?这部漫画给出了答案!
- 迟到的Meltdown/Spectre分析
- 利用Python爬取前程无忧(51job)上的招聘岗位
- xpath 爬取某网站图片
- 最快最方便的献爱心方式——蚂蚁庄园
- SpringCloud——服务接口(api)
- 主成分分析(Principal Component Analysis,PCA)
- 抗混叠滤波matlab实现,采用抗混叠滤波器的高性能、12位、500 MSPS宽带接收机
热门文章
- Oracle Pipelined Table Functions简介
- IDEA 2020.3.2控制台中文乱码分享(亲测二、下图中控制台的编码改为UTF-8)
- php-fpm:No pool defined解决方法
- nginx负载均衡配置的几种策略
- PHP的instanceof作用
- PHP的转义字符与\r\n
- JS转字符 判断数字等
- php 表单跳转,html - 思路问题:php表单跳转
- css背景上能添加文字,CSS3怎么为文字添加背景?
- xposed框架在分机安装失败_免root用Xposed框架!安卓用户一定要学会