Docker系列2:Docker安装配置与基本使用

网友投稿 232 2022-10-30

Docker系列2:Docker安装配置与基本使用

一、基础环境介绍

1、基础软硬件环境介绍

需要64位的CPUlinux内核版本3.10及以上内核支持cggroups 和 namespace系统版本为CentOS7.4【centos6也可安装,但是不稳定】Docker版本为18.06K8S版本为1.16 【k8s版本必须和docker版本兼容】

2、查看k8s与docker版本的对应关系

第一步:登录github

version,查看支持的docker版本

3、配置yum源

1)用阿里云的镜像即可,路径为 https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/Packages/,如下

2)配置yum源

[root@host1 ~]# cat /etc/yum.repos.d/docker.repo  [docker] name=aliyun docker repo enabled=1 gpgcheck=0 baseurl=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/7/x86_64/stable/

查看是否配置成功

[root@host1 ~]# yum repolist Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile  * base: mirror.bit.edu.cn  * extras: mirrors.huaweicloud.com  * updates: mirrors.huaweicloud.com repo id                                       repo name                                        status docker                                        aliyun docker repo                                   58 base/7/x86_64                                 CentOS-7 - Base                                  10,097 extras/7/x86_64                               CentOS-7 - Extras                                   307 updates/7/x86_64                              CentOS-7 - Updates                                  997

二、安装配置docker

1、安装docker 18.06.0

[root@host1 ~]# yum install docker-ce-18.06.0.ce

2、创建json文件加速docker镜像下载速度

[root@host1 ~]# mkdir /etc/docker [root@host1 ~]# cat /etc/docker/daemon.json {     "registry-mirrors": ["https://registry.docker-cn.com"] }

3、启动服务

[root@host1 ~]# systemctl start docker [root@host1 ~]# systemctl status docker

看下docker版本

[root@host1 ~]# docker --version Docker version 18.06.0-ce, build 0ffa825

查看docker信息

[root@host1 ~]# docker info

三、关于镜像的操作

1、关于防火墙问题

docker是需要启动iptables这个服务的,但是在centos7中,默认是用firewalld来管理,而不是iptables,所以可能会导致docer使用有问题,因为需要iptables做端口映射。可以用如下的方式来解决

[root@host1 ~]# systemctl disable firewalld          ^C [root@host1 ~]# yum install iptables-services -y       ^C [root@host1 ~]# systemctl enable iptables             ^C

2、查看本地的镜像

[root@host1 ~]# docker images REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

3、搜索镜像,如搜索nginx镜像

[root@host1 ~]# docker search nginx

从上面的search的结果可以看到,有些搜索出来的结果就是一个内容,而有些结果是用斜杠分割的

没有斜杠分隔的,我们称之为顶级仓库,一般就是官方的有分隔的称之为:用户仓库或者像仓库,一般就是用户自己注册账号,然后做个仓库,并放一些镜像给别人用。

4、拉去几个镜像

1)拉取nginx镜像,为节省带宽,此处选alpine版本

[root@host1 ~]# docker image pull nginx:1.14-alpine 1.14-alpine: Pulling from library/nginx bdf0201b3a05: Pull complete  3d0a573c81ed: Pull complete  8129faeb2eb6: Pull complete  3dc99f571daf: Pull complete  Digest: sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7 Status: Downloaded newer image for nginx:1.14-alpine

2)拉取busybox镜像

[root@host1 ~]# docker image pull busybox Using default tag: latest latest: Pulling from library/busybox 322973677ef5: Pull complete  Digest: sha256:1828edd60c5efd34b2bf5dd3282ec0cc04d47b2ff9caa0b6d4f07a21d1c08084 Status: Downloaded newer image for busybox:latest

3)查看本地已经下载的镜像

[root@host1 ~]# docker image ls REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE busybox             latest              b534869c81f0        2 weeks ago         1.22MB nginx               1.14-alpine         8a2fb25a19f5        8 months ago        16MB

REPOSITORY:仓库名称TAG:标签IMAGE ID:唯一ID值CREATED:什么时间创建的SIZE:大小

4)查看镜像的详细信息

[root@host1 ~]# docker image ls --no-trunc REPOSITORY          TAG                 IMAGE ID                                                                  CREATED             SIZE busybox             latest              sha256:b534869c81f05ce6fbbdd3a3293e64fd032e059ab4b28a0e0d5b485cf904be4b   2 weeks ago         1.22MB nginx               1.14-alpine         sha256:8a2fb25a19f5dc1528b7a3fabe8b3145ff57fe10e4f1edac6c718a3cf4aa4b73   8 months ago        16MB

四、关于容器的操作

1、容器操作包括如下

启动容器:start停止容器:stop强制停止:kill创建后直接启动容器:run暂停容器:pause取消暂停(继续运行)容器:unpause也可以列出来全部的容器:docker ps或docker container ls

2、启动容器操作

启动容器用run命令,包括如下参数

-t:这里是指定一个终端,如果没有终端,是无法登陆这个容器的-i, --interactive:如果想用交互式访问,就需要用这个选项--name string:这里是指定容器的名字--rm:当容器停止的时候,自动删除容器对象-d, --detach :让当前的这个容器工作在后台--network string :指定容器加入到哪个网络,如果不加的话,默认是一个bridge网络

3、基于busybox启动容器

[root@host1 ~]# docker run --name bbox -it busybox / #

此时就启动了容器,同时进入了容器的shell中

此时,在其他的终端可以看到busybox容器已经启动了

[root@host1 ~]# docker ps CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES dc696a322096        busybox             "sh"                About a minute ago   Up About a minute                       bbox

查看busybox容器中的信息可以看到此时id为1的进程是sh而不是init

/ # ps PID   USER     TIME  COMMAND     1 root      0:00 sh     6 root      0:00 ps

在busybox容器中模拟apache

/ # mkdir /data/html -p / # echo "test page">>/data/html/index.html / #  / # httpd -f -h /data/html/

也可以在bash中查看一下容器的详细信息

[root@host1 ~]# docker inspect bbox2 | grep IPAddress             "SecondaryIPAddresses": null,             "IPAddress": "172.17.0.3",                     "IPAddress": "172.17.0.3",

可看到这个容器的地址是172.16.0.3

其实在安装了docker以后,系统默认生成一个虚拟网卡docker0,物理机就是用docker0和虚拟机通信的

[root@host1 ~]# ip addr  1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 2: ens33:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 3: ens37:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 4: docker0:  mtu 1500 qdisc noqueue state UP group default      link/ether 02:42:d5:cc:54:6d brd ff:ff:ff:ff:ff:ff     inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0        valid_lft forever preferred_lft forever     inet6 fe80::42:d5ff:fecc:546d/64 scope link         valid_lft forever preferred_lft forever 6: veth96dc278@if5:  mtu 1500 qdisc noqueue master docker0 state UP group default  10: veth693182e@if9:  mtu 1500 qdisc noqueue master docker0 state UP group default

docker0的地址是172.17.0.1

在物理机用curl可以访问容器中的apache

[root@host1 ~]# curl 172.17.0.3 test page

3、停止容器与重新启动容器

执行exit就可以退出容器

/ # exit [root@host1 ~]#

退出容器后,容器只是停止了,但是依然存在

docker ps:查看正在运行的容器docker ps -a:查看全部的容器,包括已经停止的

[root@host1 ~]# docker ps CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES [root@host1 ~]# docker ps -a CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES dc696a322096        busybox             "sh"                2 hours ago         Up 2 hours                                       bbox

重启启动这个停止的容器,用start命令,可能用两个选项

-i:交互模式-a:附加到终端上

[root@host1 ~]# docker start -i -a bbox

4、删除已经停止的容器

[root@host1 ~]# docker rm bbox bbox [root@host1 ~]# docker ps -a CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES

在创建容器的时候加入rm选项,可以实现当容器停止的时候,自动删除

[root@host1 ~]# docker run --rm -it --name bbox2 busybox / #  / # exit [root@host1 ~]# docker ps -a CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

在删除容器时候可以指定名称来删除,也可以指定容器的ID来删除

在指定ID的时候,不需要写全部的ID内容,写开头的一部分即可

[root@host1 ~]# docker ps -a CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES 9c2d1bbe32e8        busybox             "sh"                3 seconds ago       Exited (0) 1 second ago                       bbox [root@host1 ~]# docker rm 9c2d 9c2d [root@host1 ~]# docker ps -a CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

下面的命令可以一次将所有已经停止的容器删除

[root@host1 ~]# docker rm $(docker ps -a -q)

5、删除镜像、导出镜像、导入镜像

rmi:删除本地的镜像load:导入镜像文件save:导出镜像文件

查看当前有哪些镜像

[root@host1 ~]# docker image ls REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE busybox             latest              b534869c81f0        2 weeks ago         1.22MB nginx               1.14-alpine         8a2fb25a19f5        8 months ago        16MB

导出busybox的镜像到tmp下

[root@host1 ~]# docker save busybox:latest>/tmp/busybox-latest.tar.gz [root@host1 ~]# ls /tmp/ busybox-latest.tar.gz

删除本地的busybox镜像

[root@host1 ~]# docker rmi b53 Untagged: busybox:latest Untagged: busybox@sha256:1828edd60c5efd34b2bf5dd3282ec0cc04d47b2ff9caa0b6d4f07a21d1c08084 Deleted: sha256:b534869c81f05ce6fbbdd3a3293e64fd032e059ab4b28a0e0d5b485cf904be4b Deleted: sha256:eac247cb7af5edc34d3620e8bce653d4af7d4e3a0427d487a97530c7fac0b841 [root@host1 ~]# docker image ls REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE nginx               1.14-alpine         8a2fb25a19f5        8 months ago        16MB

从tmp下导入镜像文件

[root@host1 ~]# docker load]  1.437MB/1.437MB Loaded image: busybox:latest [root@host1 ~]# docker image ls REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE busybox             latest              b534869c81f0        2 weeks ago         1.22MB nginx               1.14-alpine         8a2fb25a19f5        8 months ago        16MB

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ADC0808/0809与8031单片机接口设计
下一篇:Java实现经典捕鱼达人游戏的示例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~