自己本地安装了docker desktop,安装mysql,自己本地用,可以方便的切换版本进行测试等;
很简单,直接上代码
mysql
docker命令:
#拉取最新的mysql,也可以在后面指定版本
docker pull mysql
#查看拉取结果
docker images
#直接docker run创建启动容器即可
docker run -it -v /D/develop/docker/mysql/data:/var/lib/mysql -v /D/develop/docker/mysql/config/my.cnf:/etc/mysql/my.cnf --restart=always --name mysql8.0 -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -d mysql
docker-compose:
创建docker-compose.yml文件
version: '2'
services:
mysql:
image: mysql
container_name: mysql8.0
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- //d/develop/docker/mysql/data:/var/lib/mysql
- //d/develop/docker/mysql/config/my.cnf:/etc/mysql/my.cnf
然后在同级目录下执行启动命令:
docker-compose up -d
问题1:
启动时候加了挂载,将mysql的data和config文件挂载到本地磁盘。D盘一定要这样写/D/,写成D:会报错。
问题2:
挂载还会提示没有挂载权限,在Dokcer Desktop右键,选择setting,选择share drives,选择自己想要共享的盘即可
问题3
挂载my.cnf文件的时候,本地创建成了my.cnf目录,报错
Are you trying to mount a directory onto a file
解决办法:自己此案创建好mysql/config/my.cnf文件,再启动就不会有问题了 ,同时my.cnf添加内容
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysql]
#设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure_file_priv=/var/lib/mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#服务端使用的字符集默认为8比特编码的latin1字符集
character_set_server = UTF8MB4
#创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#设置不区分大小写
# 必须在安装好MySQL后 修改mySQL配置文件设置为不敏感,一旦启动后,再设置是无效的,而且启动报错;
# 如果已经晚了,那必须把MySQL数据库文件全部 删除,修改配置文件再启动。
#lower_case_table_names=1
nginx
nginx和mysql一样
docker命令:
#拉取最新的nginx,也可以在后面指定版本
docker pull nginx
#查看拉取结果
docker images
#直接docker run创建启动容器即可
docker run -it -v /D/develop/docker/nginx/conf/:/etc/nginx/conf.d/ --restart=always --name nginx -p 8101:80 -d nginx
启动时候加了挂载,将nginx的配置文件夹挂载到了本地磁盘
docker-compose:
version: '2'
services:
redis:
image: nginx
container_name: nginx
restart: always
ports:
- "8101:80"
volumes:
- //d/develop/docker/nginx/conf/:/etc/nginx/conf.d/