制作此镜像的目的是为了后面HttprunnerManager 的整个环境搭建。基础镜像是基于docker官方的python3.6。
已经生成好的镜像地址:registry.cn-hangzhou.aliyuncs.com/hw_wei/py_mysql_redis:1.0
一、创建Dockerfile 文件,内容如下:# 基础镜像 FROM silverlogic/python3.6 # 描述 MAINTAINER python3.6+mysql5.6+redis # 添加必要秘钥,以便添加apt库 archive.ubuntu.com RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5 # 换中国源,其中archive.ubuntu.com用于安装mysql RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak \ && echo "deb http://mirrors.163.com/debian/ stretch main non-free contrib" >> /etc/apt/sources.list \ && echo "deb http://mirrors.163.com/debian/ stretch-updates main non-free contrib" >>/etc/apt/sources.list \ && echo "deb-src http://mirrors.163.com/debian/ stretch main non-free contrib" >>/etc/apt/sources.list \ && echo "deb-src http://mirrors.163.com/debian/ stretch-updates main non-free contrib" >>/etc/apt/sources.list \ && echo "deb http://archive.ubuntu.com/ubuntu trusty universe" >>/etc/apt/sources.list \ && apt-get update # 安装mysql5.6 RUN { \ echo mysql-server mysql-server/root_password password '123456'; \ echo mysql-server mysql-server/root_password_again password '123456'; \ } | debconf-set-selections \ && apt-get install -y mysql-server-5.6 mysql-client-5.6 # 装redis RUN apt install -y redis-server # 启动命令 ENTRYPOINT service mysql start && redis-server # 让进程一直跑, 否则容器会exit二、构建启动镜像 # 构建镜像
sudo docker build -t python3.6-mysql-redis .# 创建+启动容器, 如果启动不了, 需要调试并修改dockerfile或启动脚本.需要外部连接的就用-p 暴露端口
sudo docker run -itd -p 3308:3306 --name py_mysql_redis py_mysql_redis# 进入bash
sudo docker exec -it python3.6-mysql-redis bash
在容器里测试是否成功:
mysql -uroot -p123456 redis-cli python
三、遇到的问题:
数据库端口暴露之后,在容器内部可以访问,但是外部还是无法连接
报错:host XXXX is not allowed to connect to this MySOL server
解决:
进到 docker 容器里面修改一下mysql账号允许从远程登录就行,
#两条命令如下:
#by 后面是密码, 可自行修改。也是远程登录的密码
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
#刷新配置
flush privileges;
四、其他: 在调试过程中,会用到的命令 # 启动容器 sudo docker start python3.6-mysql-redis # 关闭容器 sudo docker stop python3.6-mysql-redis # 删容器 sudo docker rm python3.6-mysql-redis # 删镜像 sudo docker rmi python3.6-mysql-redis