xiaoshushu-d

作为云计算工程师,不可避免地会遇到这种情况:同样的代码,运行环境发生变化之后就无法正常运行。例如:

  1. 代码从笔记本电脑切换到测试服务器
  2. 从一台物理服务器切换到公有云/私有云
  3. 代码依赖的运行库版本发生变化
  4. 代码运行的操作系统发生变化

当出现这些问题的时候,可能需要花大量的时间去处理环境迁移问题。为了解决这个问题,我们可以搭建一个云容器。云容器包含了完整的运行时环境,除了应用程序本身之外,这个程序所需的全部依赖、类库等,其他二进制文件、配置文件等,都统一被打包到一个镜像中。

容器技术让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的操作系统上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。

搭建容器基础环境

环境准备:centos7 64 ,linux操作系统,图形化界面,可以访问百度

清除防火墙规则

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -Z
[root@localhost ~]# iptables -X
[root@localhost ~]# iptables-save

修改selinux配置

[root@localhost ~]# sed -i \'s/SELINUX=enforcing/SELINUX=disabled/g\' /etc/selinux/config

关闭交换分区,配置路由转发

[root@localhost ~]# swapoff -a 
[root@localhost ~]# cat >>/etc/sysctl.conf << EOF
>net.ipv4.ip_forward=1
>net.bridge.bridge-nf-call-ip6tables=1
>net.bridge.bridge-nf-call-iptables=1
>EOF

向内核中加载模块或移除模块

[root@localhost ~]# modprobe br_netfilter

安装docker容器服务

安装docker社区版

[root@localhost ~]# yum -y install docker-ce

启动docker

[root@localhost ~]# systemctl start docker;systemctl enable docker

重新加载服务,使配置生效

[root@localhost ~]# systemctl daemon-reload

使用docker搭建云硬盘

  • daoCloud,企业级容器云平台。访问国内最大的容器仓库(https://hub.daocloud.io/),查看仓库里的镜像,将需要的镜像拉取到本地。


拉取镜像

[root@localhost ~]# docker pull daocloud.io/library/owncloud:8

查看下载的镜像

[root@localhost ~]# docker images

创建容器

[root@localhost ~]# docker run -d -p 8080:80 daocloud.io/library/owncloud:8.1

打开火狐浏览器,输入本机IP:端口


云硬盘创建成功,可以上传和下载。

使用容器私有仓库

本地导入registry镜像

registry是一个仓库镜像管理工具

[root@localhost ~]# docker load < /root/images/registry_latest.tar  //存放registry镜像的路径

或者直接从网上拉取

[root@localhost ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
81033e7c1d6a: Pull complete 
b235084c2315: Pull complete 
c692f3a6894b: Pull complete 
ba2177f3a70e: Pull complete 
a8d793620947: Pull complete 
Digest: sha256:672d519d7fd7bbc7a448d17956ebeefe225d5eb27509d8dc5ce67ecb4a0bce54
Status: Downloaded newer image for registry:latest

创建一个registry容器

[root@localhost ~]# docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000  --restart=always --name registry registry:latest
  • -d : 后台运行,返回容器id
  • -v : 挂载宿主机的一个目录,格式:宿主机目录/容器内目录
  • -p:内外端口映射
  • --restart=always:遇到问题关闭重启

查看docker容器状态

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
0645fe22e6d8        registry:latest     "/entrypoint.sh /etc…"   30 hours ago        Up 12 minutes       0.0.0.0:5000->5000/tcp   registry

配置仓库地址

[root@localhost ~]# cat /etc/docker/daemon.json 
{
	"insecure-registries":["本机IP:5000"]
}
[root@localhost ~]#systemctl restart docker;systemctl daemon-reload

使用curl命令访问仓库

[root@localhost ~]# curl http://本机IP:5000/v2
<a href="/v2/">Moved Permanently</a>.

上传镜像到仓库,首先将需要上传的镜像打标签

[root@localhost ~]# docker tag registry 本机IP:5000/registry:latest

推送镜像

[root@localhost ~]# docker push 本机IP:5000/registry:latest

访问仓库,发现registry镜像(http://本机IP:5000/v2/_catalog)

私有仓库创建成功!

补充:docker rmi 镜像名 (删除本地镜像)

docker pull 镜像名(下载镜像)

分类:

技术点:

相关文章: