Docker中CentOS容器安装使用MySQL

文章目录

  • Docker中CentOS镜像使用MySQL
      • #1.1 环境
      • #1.2 需求
    • #2 docker的配置
      • #2.1 拉取CentOS镜像
      • #2.1 创建CentOS 6.9容器
      • #2.2 进入/退出容器
    • #3 在CentOS容器中使用MySQL
      • 3.1 安装MySQL
    • #4 保存当前镜像

Docker中CentOS镜像使用MySQL

##1 环境&需求

#1.1 环境

Mac
Docker
 -CentOS 6.9
    -MySQL 5.7

#1.2 需求

  • 在docker中创建一个CentOS
  • 在CentOS中安装MySQL
  • 在宿主机(Mac)链接CentOS中的MySQL

#2 docker的配置

  • Docker拉取CentOS镜像
  • 创建CentOS容器
  • 进入CentOS容器

#2.1 拉取CentOS镜像

docker pull  centos:6.9

Docker中CentOS容器安装使用MySQL_第1张图片

查看刚刚拉取的CentOS 6.9镜像

docker images

#2.1 创建CentOS 6.9容器

docker run -it centos:6.9  /bin/bash
# 命令注释如下:
# run: 创建容器的命令(固定格式)
# -it: 以交互式模式运行容器(一般-i-t同时出现)
# /bin/bash: 使用容器的命令输入命令(进入容器终端)

进入CentOS终端后, root@d2568b5fe7b3中@符号后面的内容指的是该容器对应的ID,容器ID不等于镜像ID

Docker中CentOS容器安装使用MySQL_第2张图片

  • 查看正在运行的容器
╭─[email protected] ~
╰─➤  docker ps                                                                                                                                                         130 ↵
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d2568b5fe7b3        centos:6.9          "/bin/bash"         5 minutes ago       Up 5 minutes                            happy_mcclintock
5437aa4212f8        centos:6.8          "/bin/bash"         About an hour ago   Up About an hour                        gifted_fermat

第一列CONTAINER ID是容器的ID, 也就是进入容器后, root@后面显示的那串内容

STATUS是容器的状态, 容器的状态7种

STATUS 说明
created 已创建
restarting 重启中
running 运行中
removing 迁移中
paused 暂停
exited 停止
dead 死亡

20200104185828-image.png

#2.2 进入/退出容器

  1. 进入容器
# 进入容器
docker attach d2568b5fe7b3
# docker attach 容器ID
╭─[email protected] ~
╰─➤  docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d2568b5fe7b3        centos:6.9          "/bin/bash"         33 minutes ago      Up 33 minutes                           happy_mcclintock
5437aa4212f8        centos:6.8          "/bin/bash"         About an hour ago   Up About an hour                        gifted_fermat
╭─[email protected] ~
╰─➤  docker attach d2568b5fe7b3
[root@d2568b5fe7b3 /]#

Docker中CentOS容器安装使用MySQL_第3张图片

  1. 退出容器
  • exit

退出并杀死容器

╭─[email protected] ~
╰─➤  docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5437aa4212f8        centos:6.8          "/bin/bash"         About an hour ago   Up About an hour                        gifted_fermat
╭─[email protected] ~
╰─➤  docker run -it centos:6.9  /bin/bash
[root@ced5ed59ec2c /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[root@ced5ed59ec2c /]# mkdir haha
[root@ced5ed59ec2c /]# ls
bin  dev  etc  haha  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[root@ced5ed59ec2c /]# exit
exit
╭─[email protected] ~
╰─➤  docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5437aa4212f8        centos:6.8          "/bin/bash"         About an hour ago   Up About an hour                        gifted_fermat
╭─[email protected] ~
╰─➤  docker run -it centos:6.9  /bin/bash
[root@6e12db3af46f /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[root@6e12db3af46f /]#

Docker中CentOS容器安装使用MySQL_第4张图片

  • Ctrl+Q+P

退出后,后台容器仍在运行

[root@6e12db3af46f /]# mkdir haha2
[root@6e12db3af46f /]# ls
bin  dev  etc  haha2  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[root@6e12db3af46f /]# %                                                                                                                                                     ╭─[email protected] ~
╰─➤  docker attach 6e12db3af46f
[root@6e12db3af46f /]# ls
bin  dev  etc  haha2  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[root@6e12db3af46f /]#

Docker中CentOS容器安装使用MySQL_第5张图片

#3 在CentOS容器中使用MySQL

3.1 安装MySQL

  1. 安装wget
yum install -y wget
  1. 安装MySQL官方的 Yum Repository
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
  1. 安装MySQL5.7
yum install -y mysql-server
  1. 启动MySQL
service mysqld start
或者
/etc/init.d/mysqld start
  1. 修改密码

https://blog.csdn.net/Coxhuang/article/details/103332961

  1. 进入MySQL
mysql -u root -p

#4 保存当前镜像

  1. 退出容器
exit
  1. 提交容器
docker commit 5437aa4212f8 centos:addmysql
# docker commit 容器ID 镜像名:TAG
[root@5437aa4212f8 /]# exit
exit
╭─[email protected] ~
╰─➤  docker commit 5437aa4212f8 centos:addmysql
sha256:25753ae423774c946936fd22ccb73d14967b0db87956b7fc0bed167e4049c572
╭─[email protected] ~
╰─➤  docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              addmysql            25753ae42377        7 seconds ago       442MB
centos              6.8                 82f3b5f3c58f        9 months ago        195MB
centos              6.9                 2199b8eb8390        9 months ago        195MB
mysql               5.7.21              5195076672a7        22 months ago       371MB
╭─[email protected] ~
╰─➤  docker run -it centos:addmysql  /bin/bash
[root@cca8b56a08c2 /]# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@cca8b56a08c2 /]# service mysqld start
Starting mysqld:                                           [  OK  ]
[root@cca8b56a08c2 /]#

Docker中CentOS容器安装使用MySQL_第6张图片

你可能感兴趣的:(Docker)