服务器:centos7.4
mysql:5.7
1.下载mysql镜像
[root@localhost ~]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
85b1f47fba49: Pull complete
5671503d4f93: Pull complete
3b43b3b913cb: Pull complete
4fbb803665d0: Pull complete
05808866e6f9: Pull complete
1d8c65d48cfa: Pull complete
e189e187b2b5: Pull complete
02d3e6011ee8: Pull complete
d43b32d5ce04: Pull complete
2a809168ab45: Pull complete
Digest: sha256:1a2f9361228e9b10b4c77a651b460828514845dc7ac51735b919c2c4aec864b7
2.镜像下载完成后,查看是否下载成功
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/jenkins latest 3f08dc4f3f5d 3 weeks ago 808.9 MB
docker.io/mysql latest 5709795eeffa 3 weeks ago 408.2 MB
可以看到repository一栏确实有mysql这个镜像
3.创建并启动一个mysql容器
[root@localhost ~]# docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=Aa123456! mysql
265c55de36095f1938f1aa27dcc2887fc4d1c929e51a932daf5061085632ef76
说明:
-d:以后台运行容器
--name:指定容器得名称
-p:将容器得端口映射到宿主机得端口
-e:指定运行参数,现在指定得是root得密码
mysql:镜像得名称
4.查看容器运行状态
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
265c55de3609 mysql "docker-entrypoint.sh" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp mysql
3cfe03dd6e31 jenkins "/bin/tini -- /usr/lo" 2 days ago Up 2 days 0.0.0.0:50000->50000/tcp, 0.0.0.0:8081->8080/tcp jenkins
5.验证
随便找个mysql客户端,输入刚刚创建得mysql信息,然后点击save,之后可以成功进入mysql就算安装成功了
6.创建数据持久化得mysql容器,首先先建立一个数据挂载目录,然后进入目录
[root@localhost home]# mkdir mysql
[root@localhost home]# cd mysql/
7.创建容器,并且运行
[root@localhost mysql]# docker run -p 3306:3306 --name mysql -v $PWD/conf:/etc/mysql -v $PWD/logs:/logs -v $PWD/data:/mysql_data -e MYSQL_ROOT_PASSWORD=Aa123456! -d mysql
158ee4e9bed0220925ed1402a17b504c482f3e5b1ffc62494de0c3b47fb8fa74
8.查看容器状态
[root@localhost mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
158ee4e9bed0 mysql "docker-entrypoint.sh" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp mysql
3cfe03dd6e31 jenkins "/bin/tini -- /usr/lo" 3 days ago Up 3 days 0.0.0.0:50000->50000/tcp, 0.0.0.0:8081->8080/tcp jenkins
9.验证
虽然看起来并没有什么差别,但是这样做已经将数据持久化,不会随着容器消失而丢失数据