SpringBoot与Docker(学习笔记23)

本文记录了在Linux(CentOS)虚拟机中安装Docker的过程,包括检查内核版本、安装Docker、解决启动失败的SELinux问题,以及设置Docker开机启动的步骤。在遇到因SELinux不支持overlay2导致的启动错误时,通过修改配置禁用了SELinux以使Docker正常运行,并展示了如何查看Docker版本号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

docker镜像参考:https://hub.docker.com

更多的命令参考:https://docs.docker.com/engine/reference/commandline/docker/

1、简介

Docker是一个开源的应用容器引擎;是一个轻量级容器技术;
Docker支持将软件编译成一个镜像;然后在镜像中各种软件做好配置,将镜像发布出去,其他使用者可以直接使用这个镜像;

运行中的这个镜像称为容器,容器启动是非常快速的。


2、核心概念

docker主机(Host):安装了Docker程序的机器(Docker直接安装在操作系统之上);
docker客户端(Client):连接docker主机进行操作;
docker仓库(Registry):用来保存各种打包好的软件镜像;
docker镜像(Images):软件打包好的镜像;放在docker仓库中;

docker容器(Container):镜像启动后的实例称为一个容器;容器是独立运行的一个或一组应用


使用Docker的步骤:
1)、安装Docker
2)、去Docker仓库找到这个软件对应的镜像;
3)、使用Docker运行这个镜像,这个镜像就会生成一个Docker容器;
4)、对容器的启动停止就是对软件的启动停止;

3、安装Docker

    1)、安装linux虚拟机
        1)、VMWare、VirtualBox(安装);
        2)、导入虚拟机文件centos7-atguigu.ova;
        3)、双击启动linux虚拟机;使用 root/ 123456登陆
        4)、使用客户端连接linux服务器进行命令操作;
        5)、设置虚拟机网络;

                桥接网络=选好网卡==接入网线;

        6)、设置好网络以后使用命令重启虚拟机的网络       

service network restart 1

        7)、查看linux的ip地址        

ip addr
        8)、使用客户端连接linux;


          

















-------------------------------------------------------------------------------------------------------------------------------------------------------------

2)、在linux虚拟机上安装docker

    1、查看centos内核版本(Docker 要求 CentOS 系统的内核版本高于3.10):

uname -r


    如果CentOS系统内核低于3.10,可以升级软件包及内核,通过

yum update

    2、安装docker (前提是连上网),通过下面命令安装:

yum install docker

安装过程中,输入y确认安装

安装成功。


    3、启动 docker:

systemctl start docker


    

启动失败,报错:

Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.


按照提示执行:systemctl status docker.service



红色部分告诉我们此linux的内核中的SELinux不支持 overlay2 graph driver,解决方法有两个,要么启动一个新内核,要么就在docker里禁用selinux,设置--selinux-enabled=false。我们采用第二种方式。

vi /etc/sysconfig/docker

然后将--selinux-enabled设置成false,保存并退出。



重新启动ok。

查看docker版本号:

docker -v


    4、设置 docker 服务开机启动:

systemctl enable docker


    5、停止 docker:

systemctl stop docker


4、常用操作

    1、镜像操作(相应的镜像信息都可以在 https://hub.docker.com 中查询)

      1.1、 查索操作-- search命令:docker search 关键字

       搜索mysql镜像信息:docker search mysql


INDEX:                索引
NAME:                镜像的名称,需要docker.io/后面的名称
DESCRIPTION:   镜像描述
STARS:              有多少人关注,单位K
OFFICIAL:          是指这个镜像不是官方的,OK代表官方镜像,官方的没有自动配置,需要自己配置

AUTOMATED:    是不是自动构建的,OK代表自动化的,一运行所有的配置都配置好了


会到其 https://hub.docker.com 搜索


-----------------------------------------------------

      1.2、 拉取操作-- pull命令:docker pull 镜像名:tag

        :tag是可选的,tag表示标签,多为软件版本号,如果不加 :tag,默认下载latest(下载最新版本)。 下载mysql:  





-----------------------------------------------------

      1.3、 查看所有本地镜像操作-- images命令:docker images


REPOSITORY:  从哪里下载的镜像

TAG:                 标签(版本号)

IMAGE ID:         镜像ID

CREATE:          镜像创建时间

SIZE:                 镜像大小


-----------------------------------------------------

      1.4、 删除镜像操作-- rmi image-id命令:docker rmi image-id



--------------------------------------------------------------------------------------------------------------------------------------------------------

    2、容器操作

        软件镜像相当于QQ安装程序

        运行镜像,就产生一个容器(正在运行的软件,运行的QQ)

        步骤:

            1、搜索镜像


            2、拉取镜像


            3、根据镜像启动容器

                3.1、镜像运行:

                        docker run --name container-name -d image-name :

                        --name:自定义容器名

                        -d:后台运行

                        image-name:指定镜像模板

                        例如:docker run --name mytomcat -d tomcat   如果tomcat有标签TAG,则需要加个TAG


                    3.2、查看运行中的容器

                            docker ps (查看运行中的容器)


                             docker ps -a (查看所有的容器,包括运行中的,停止的)


                    3.3、停止运行中的容器

                             docker stop container-name/container-id   停止当前运行的容器(推荐使用ID)

                    3.4、启动容器

                            docker start container-name/container-id     启动容器(推荐使用ID)


                   3.5、删除容器

                            docker rm container-id  删除指定容器

                   3.6、启动一个做了端口映射的tocmat

                            docker run -d -p 8888:8080 --name mytomcat tomcat 

                            -p:主机端口映射到容器内部的端口 (主机端口:容器内部端口)


通过端口映射,我们就可以访问tomcat:




注:如果访问不了,可能是防火墙原因(为了演示简单,我这里关闭了防火墙):


service firewalld status:查看防火墙状态

service firewalld stop:关闭防火墙


一个镜像可以启动多个容器:





都可以成功访问


                      3.7、查看容器日志

                               docker logs container-name/container-id  (推荐使用ID)




更多的命令参考:https://docs.docker.com/engine/reference/commandline/docker/

 
5、docker安装mysql

举例:

        步骤:

                1、搜索mysql:docker search mysql


                不过呢?我们推荐在 https://hub.docker.com 中搜索:



                    2、拉取mysql:docker pull mysql



                    3、查看本地镜像:docker images



                    4、启动mysql镜像:docker run --name mysql01 -d mysql



                    5、查看启动镜像信息:docker ps 


                          这里发现mysql并没有启动成功。为什么没有启动成功呢?我们下面一步一步的排查。通过 docker ps -a 命令查看所有的容器信息,发现mysql退出了,具体看看日志是什么原因?



                    6、查看容器日志:docker logs container-name/container-id  (推荐使用ID)


error: database is uninitialized and password option is not specified
  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and
MYSQL_RANDOM_ROOT_PASSWORD;这个三个参数必须指定一个


                       7、正确启动:docker run --name mysql01 -e MYSQL_ROOT_PASSWORD=123456 -d mysql


这里需要,设置容器开机启动:

使用在Docker run的时候使用--restart参数来设置。 
no - container:不重启 
on-failure - container:退出状态非0时重启 
always:始终重启

docker run --restart=always --name mysql01 -e MYSQL_ROOT_PASSWORD=123456 -d mysql



到这一步时,其实mysql也不能使用,因为我们还没有做端口映射。通过 Navicat Premium 软件连接不上mysql:


端口映射:


再次通过 Navicat Premium 软件连接不上mysql:


这时我们需要修改mysql配置,输入命令进入mysql:

docker run -it --link mysql01:mysql mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot

 -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'



1、查看用户信息:select host,user,plugin,authentication_string from mysql.user;  


备注:host为 % 表示不限制ip   localhost表示本机使用    plugin非mysql_native_password 则需要修改密码

2、修改用户密码

1.如果想要设置远程这样设置:ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'newpassword'; #更新一下用户的密码 root用户密码为newpassword  

2.如果要设置本地plugin为mysql_native_password这样设置:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword'; #更新一下用户的密码 root用户密码为newpassword  

flush privileges;



再次查看用户信息:



再交连接测试:


---------------------------------------------------------------------------------------------------------------------------------------

高级操作:

使用自定义的MySQL配置文件

$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

具体的细节使用,参考:https://hub.docker.com/_/mysql/


扩展--修改Docker镜像加速器

因为国外的docker镜像访问太慢,所以我们需要修改成阿里云的docker镜像。这样从国内镜像拉取速度会快点。

1.打开阿里云docker仓库地址https://dev.aliyun.com/search.html

2.淘宝账号即可登录,登录后点击自己的管理中心。

3.点击管理中心左侧菜单栏的“镜像加速器”,右边面板会有你的加速地址,面板下面有详细设置步骤。如下图:

[root@localhost ~]# vi /etc/docker/daemon.json

将下面整行字符拷贝进去,保存并退出。

"registry-mirrors": ["https://njrds9qc.mirror.aliyuncs.com"]

 刷新daemon

[root@localhost ~]# systemctl daemon-reload

重启docker

[root@localhost ~]# systemctl restart docker

拉取镜像

以hello-world为例

复制代码
[root@localhost tmp]# docker pull hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ... 
latest: Pulling from docker.io/library/hello-world
9bb5a5d4561a: Pull complete 
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for docker.io/hello-world:latest
复制代码

表示成功拉取

运行镜像

复制代码
[root@localhost tmp]# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
复制代码

成功运行。

感谢--尚硅谷


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值