欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

docker的使用

时间:2023-05-16
docker

安装


文章目录

docker一、docker的安装(aliyun)二、docker镜像三、Dockerfile方式


前言
先配虚拟机,保证可以联网。
本文采用rehl7.6系统

一、docker的安装(aliyun)

[root@docker1 ~]# cd /etc/yum.repos.d/[root@docker1 yum.repos.d]# lsredhat.repo rhel7.6.repo[root@docker1 yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

修改下仓库配置文件,删掉先不用的,方便当前阶段测试使用。

[root@docker1 yum.repos.d]# cat docker-ce.repo[docker-ce-stable]name=Docker CE Stable - $basearchbaseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/$basearch/stableenabled=1gpgcheck=0gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

安装docer

[root@docker1 yum.repos.d]# yum install docker-ce

若发现依赖性的问题有如下解决方案
还是再阿里云上
找一个centos7的仓库
wget https://mirrors.aliyun.com/repo/Centos-7.repo
调整下仓库,关掉不用的

[root@docker1 yum.repos.d]# cat Centos-7.repo# CentOS-base.repo## The mirror system uses the connecting IP address of the client and the# update status of each mirror to pick mirrors that are updated to and# geographically close to the client、 You should use this for CentOS updates# unless you are manually picking other mirrors.## If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead.## [base]name=CentOS-$releasever - base - mirrors.aliyun.comfailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/gpgcheck=1gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7enabled=0#released updates [updates]name=CentOS-$releasever - Updates - mirrors.aliyun.comfailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/gpgcheck=1gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7enabled=0 #additional packages that may be useful[extras]name=CentOS-$releasever - Extras - mirrors.aliyun.comfailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/7/extras/$basearch/gpgcheck=0gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that extend functionality of existing packages[centosplus]name=CentOS-$releasever - Plus - mirrors.aliyun.comfailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/gpgcheck=1enabled=0gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7enabled=0#contrib - packages by Centos Users[contrib]name=CentOS-$releasever - Contrib - mirrors.aliyun.comfailovermethod=prioritybaseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/gpgcheck=1enabled=0gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7enabled=0

然后开启docker

[root@docker1 yum.repos.d]# systemctl start docker[root@docker1 yum.repos.d]# systemctl enable dockerCreated symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.[root@docker1 yum.repos.d]# docker info

info开启不成功
是因为火墙的问题

解决方法


在sysctl.d下新建一个文件

[root@docker1 yum.repos.d]# cat /etc/sysctl.d/docker.confnet.bridge.bridge-nf-call-iptables = 1net.bridge.bridge-nf-call-ip6tables = 1

然后启用
sysctl --system
再次输入docker info就好了

测试


运行后打开 浏览器
输入虚拟机ip+80端口

就可以看到运行的内容了

二、docker镜像

分层结构
共享资源
所有镜像层可读
最上层可写
操作对下层都不产生影响
过程就类似于累积木

以busybox为例展示

[root@docker1 ~]# docker pull busyboxUsing default tag: latestlatest: Pulling from library/busybox009932687766: Pull complete Digest: sha256:afcc7f1ac1b49db317a7196c902e61c6c3c4607d63599ee1a82d702d249a0ccbStatus: Downloaded newer image for busybox:latestdocker.io/library/busybox:latest

删掉原来的运行容器,再创建

[root@docker1 ~]# docker rm -f demodemo[root@docker1 ~]# docker run -it --name demo busybox


此时就在可写容器层
退出后可以用
docker ps -a
来查看
在使用中可以用
ctrl+p+q
打入后台
docker stop demo(自定义名称)
完全退出
docker rm demo(自定义名称)
删除
若要保存操作可以用commit

此时可以保存所有操作
用history查看
这样就算删掉了demo
从v1开始创建,操作会保留

删除镜像
docker rmi demo1:v1
可以看到如果容器正在被占用,不能直接删除,必须先删掉demo1,然后才能删掉v1。v3没有被使用可以直接删除。

在history中信息也不符合企业生产环境中的安全规范性
效率也很低下
不建议使用这种方式
所以下面介绍另一种方式

三、Dockerfile方式

创建一个目录
然后在目录中创建文档Dockerfile

这样操作的步骤在history中都很清楚
操作步骤都可以重复利用,提升效率

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。