博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ubuntu安装Docker引擎和支持HTTPS的docker-registry服务
阅读量:7082 次
发布时间:2019-06-28

本文共 8426 字,大约阅读时间需要 28 分钟。

虽然本文标题是针对Ubuntu的,但对于CentOS也是可以变通的,无非是几个命令不完全相同,有经验的人可以自行修改,部分不容易找的包或者命令已经在文档中作出了注释,可以作为参考。

目录导航:

  • 安装Docker引擎服务

  • 安装docker-enter工具,便于使用docker

  • 安装docker-registry服务

  • 支持HTTPS的docker-registry服务

    • 编译安装nginx

    • 免费制作https所需的可被权威CA认证的SSL证书(DV Certificates)

    • 配置nginx https服务

  • 验证最终结果

  • CentOS运行python2.7的方法

(1)系统初始化完成后开始安装Docker引擎服务

# for service docker

# Refer: https://www.docker.com/

# Refer: https://docs.docker.com/linux/step_one/

静默更新系统,-qq参数需要用在apt-get子命令之前,-y随意

apt-get -qq update -y

安装wget工具用于下载文件

which wget  > /dev/null || apt-get -qq install -y wget

添加docker的软件仓库key

wget -qO- https://get.docker.com/gpg | apt-key add -

执行docker安装程序

wget -qO- https://get.docker.com/ | sh

执行docker的测试命令,通过此命令看一看到docker引擎的版本等信息

docker version

# If you would like to use Docker as a non-root user, you should now consider adding your user to the "docker" group with something like:

# sudo usermod -aG docker your-user

(2)安装docker-enter工具,便于使用docker

# for program docker-enter

# Refer: http://dockerpool.com/static/books/docker_practice/container/enter.html

安装curl用于下载文件,此步骤可以省略,但建议安装

which curl > /dev/null || apt-get -qq install -y curl

下载并解压util工具,编译并安装nsenter,nsenter是docker-enter的主要组件

#cd /tmp; curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz | tar -zxf-; cd util-linux-2.24;

cd /tmp; wget -q https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz; tar xzvf util-linux-2.24.tar.gzcd util-linux-2.24./configure --without-ncurses && make nsentercp nsenter '/usr/local/bin'which nsentercd

下载.bashrc_docker文件,并将此文件添加到当前用户的bash初始化文件中

wget -P ~ https://github.com/yeasy/docker_practice/raw/master/_local/.bashrc_docker;echo "[ -f ~/.bashrc_docker ] && . ~/.bashrc_docker" >> ~/.bashrc; source ~/.bashrc

(3)安装docker-registry服务

# for service docker-registry

静默安装依赖软件docker-registry的软件包,执行安装程序

apt-get -qq -y install build-essential python-dev libevent-dev python-pip libssl-dev liblzma-dev libffi-dev swig

从github获取docker-registry,安装方式还有其他几种,可以参考官方文档

git clone https://github.com/docker/docker-registry.gitcd docker-registrypython setup.py install

如果目录存在则进入目录

test -d '/usr/local/lib/python2.7/dist-packages/docker_registry-1.0.0_dev-py2.7.egg/config/' && cd /usr/local/lib/python2.7/dist-packages/docker_registry-1.0.0_dev-py2.7.egg/config/

以config_sample.yml模板创建config.yml文件

cp config_sample.yml config.yml

以后台程序的方式运行gunicorn,以启动docker-registry

nohup gunicorn --access-logfile - --error-logfile - -k gevent -b 0.0.0.0:5000 -w 4 --max-requests 100 docker_registry.wsgi:application >>/tmp/docker-registry.log &

如果要结束docker-registry则执行killall gunicorn

killall gunicorn

配置docker可以使用刚刚配置好的docker-registry

把docker.domain.com改成你的域名,后面在配置支持HTTPS的docker-registry服务时会继续使用。

# Insecure Registry, Deploying a plain HTTP registry,Using self-signed certificates grep insecure /etc/default/docker || echo 'DOCKER_OPTS="--insecure-registry docker.domain.com:5000"' >> /etc/default/dockerservice docker stop && service docker start

(4)配置支持HTTPS的docker-registry服务

编译安装nginx

wget http://nginx.org/download/nginx-1.8.0.tar.gz  tar zxf nginx-1.8.0.tar.gz  wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz  tar zxf pcre-8.37.tar.gz  wget https://www.openssl.org/source/openssl-1.0.2e.tar.gz  tar zxf openssl-1.0.2e.tar.gz  wget http://zlib.net/zlib-1.2.8.tar.gz  tar zxf zlib-1.2.8.tar.gz  groupadd -r www  useradd -r -g www www -c "Web user" -d /dev/null -s /sbin/nologin  cd nginx-1.8.0  ./configure --prefix=/usr/local/nginx --with-http_ssl_module --user=www --group=www --with-pcre=/root/pcre-8.37 --with-zlib=/root/zlib-1.2.8 --with-openssl=/root/openssl-1.0.2e  make && make install  /usr/local/nginx/sbin/nginx -V

免费制作https所需的可被权威CA认证的SSL证书(DV Certificates)

制作证书可以自己制作自签名证书,既可以使用Windows自带的证书服务,也可以使用OpenSSL工具,但弊端是用户必须安装CA证书,浏览器才能承认此证书。如果想申请互联网公认的证书必须由公认的权威CA签发,这样的证书通常需要购买或者试用获得。如果不想花钱或者试用,可以借助Let’s Encrypt CA项目,自己给自己发放管理证书。

Let’s Encrypt CA项目是由Mozilla、思科、Akamai、IdenTrust、EFF和密歇根大学研究人员等共同创建的 ,项目官方网站是,Github项目网站是。此项目为网站(个人网站、非个人网站)提供免费SSL证书,向任何需要加密证书的网站自动发行和管理免费证书。

系统要求(先决条件):

  1. python2.6以上(不含python2.6),CentOS系统默认安装的是python2.6,CentOS升级python的方法参照下文。

  2. 要申请的证书的common name也就是证书使用者(持有人)的域名需要指向当前客户端所在的主机,例如你要给自己的网站域名为申请证书,则必须与你执行下列步骤时所在的主机是同一个主机(域名解析到此主机,不区分A记录还是CNAME记录),原因是在生成证书的过程中letsencrypt服务器会与当前客户端进行通信,如果不是同一个主机则将导致无法通过验证或者通信失败,进而导致证书生成失败。 如果,域名不是DNS运营商来得到的,则可以尝试修改hosts文件。例如在Linux下,修改/etc/hosts文件,添加your_ip_address 条目即可。

git clone https://github.com/letsencrypt/letsencrypt  cd letsencrypt  ./letsencrypt-auto certonly --standalone --email dgdenterprise@gmail.com -d www.domain.com

命令会自动根据当前的操作系统下载合适的依赖包(主要是python),并且判断当前系统环境是否满足生成证书的要求,如果不满足系统要求,则该程序会显示出警告或者错误信息。

在命令成功完成后会有较为明显的提示,生成的证书将存放在/etc/letsencrypt/live/www.domain.com/目录下,这个目录下的文件是/etc/letsencrypt/archive/www.domain.com/下的符号链接文件

配置nginx https服务

有了证书和key就可以开始配置nginx https服务了。

可选步骤,(尚未验证,读者可忽略此段斜体文字)为docker-registry设置身份验证,需要用到Apache项目中的htpasswd,在CentOS中htpasswd位于httpd-tools包中

# for htpasswd apt-get -qq -y install apache2-utils

配置nginx https服务,有两种选择,即可以像配置nginx http服务那样一步一步新建虚拟主机,也可以直接用https代理http。此处选择后者,简单易行。

mkdir /usr/local/nginx/conf/vhost

# sed在文件最后一行的上一行插入新行,如果是最后一行插入新行,则可以将i换成a  

sed -i '$i include vhost/*.conf;' /usr/local/nginx/conf/nginx.conf

# 注意:把下文中的换成想设置的域名  

#注意:把下文中的docker-registry-server换成上文监听5000端口的某个IP地址,建议将此IP地址使用内网IP地址  

vim /usr/local/nginx/conf/vhost/https_www.yourdomainname.com.conf

server {  

        listen 443 ssl;  
        server_name ;  
        index index.html index.htm;  
       
        ssl on;  
        ssl_certificate /etc/letsencrypt/live/www.yourdomainname.com/fullchain.pem;  
        ssl_certificate_key /etc/letsencrypt/live/www.yourdomainname.com/privkey.pem;  
        #ssl_password_file /usr/local/;

        location / {  

            client_max_body_size    0;  
            proxy_connect_timeout 300s;  
            proxy_send_timeout   900;  
            proxy_read_timeout   900;  
            proxy_buffer_size    32k;  
            proxy_buffers      4 32k;  
            proxy_busy_buffers_size 64k;  
            proxy_redirect     off;  
            proxy_hide_header  Vary;  
            proxy_set_header   Accept-Encoding '';  
            proxy_set_header   Host   $host;  
            proxy_set_header   Referer $http_referer;  
            proxy_set_header   Cookie $http_cookie;  
            proxy_set_header   X-Real-IP  $remote_addr;  
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;  
            proxy_set_header   Host $host;  
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;          
            proxy_headers_hash_max_size 51200;  
            proxy_headers_hash_bucket_size 6400;  
            proxy_pass         ;  
        }

}  

vim /usr/local/nginx/conf/vhost/http_www.yourdomainname.com.conf

server {  

        listen 80;  
        server_name ;  
        index index.html index.htm;

        location / {  

            client_max_body_size    0;  
            proxy_connect_timeout 300s;  
            proxy_send_timeout   900;  
            proxy_read_timeout   900;  
            proxy_buffer_size    32k;  
            proxy_buffers      4 32k;  
            proxy_busy_buffers_size 64k;  
            proxy_redirect     off;  
            proxy_hide_header  Vary;  
            proxy_set_header   Accept-Encoding '';  
            proxy_set_header   Host   $host;  
            proxy_set_header   Referer $http_referer;  
            proxy_set_header   Cookie $http_cookie;  
            proxy_set_header   X-Real-IP  $remote_addr;  
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;  
            proxy_set_header   Host $host;  
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;          
            proxy_headers_hash_max_size 51200;  
            proxy_headers_hash_bucket_size 6400;  
            proxy_pass         ;  
        }

}

/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload

(5)验证最终结果

此时可以使用docker命令,将从其他地方导入的images或从hub.docker.com网站pull的镜像,commit或tag成 ,然后push到自己建的docker-registry上。          

附加:CentOS将python2.6切换到python2.7的方法

文章可以参考《》或《》。

yum groupinstall -y "Development tools"yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-develecho '/usr/local/lib' >> /etc/ld.so.confldconfig# Python 2.7.6:wget -c http://python.org/ftp/python/2.7.6/Python-2.7.6.tar.xztar xf Python-2.7.6.tar.xzcd Python-2.7.6./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"make && make altinstallcd# First get the setup script for Setuptools:wget -c https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.pypython2.7 ez_setup.pyeasy_install-2.7 pip# Install virtualenv for Python 2.7 and create a sandbox called my27project:pip2.7 install virtualenvvirtualenv-2.7 my27project# Check the system Python interpreter version:python --version# This will show Python 2.6.6# Activate the my27project sandbox and check the version of the default Python interpreter in it:source my27project/bin/activatepython --version# This will show Python 2.7.6# 在这里运行生成证书的程序# 生成证书完毕后执行deactivate,退出virtualenvdeactivate

tag:Ubuntu安装docker,Ubuntu安装docker-registry,Ubuntu安装docker-enter,Ubuntu安装docker引擎,Ubuntu 容器虚拟化

--end--

转载地址:http://jeoml.baihongyu.com/

你可能感兴趣的文章
OA系统渠道为王 华天动力OA布局全国20个城市分公司
查看>>
Aryaka拟2018年上市 当前估值10亿美元
查看>>
CIO:2017大数据发展趋势
查看>>
什么是人工智能(AI)?
查看>>
[erlang]一次erlcron崩溃引起的事故分析
查看>>
在MATLAB下调试Caffe
查看>>
大图做帧动画卡顿?不存在的!—— 优化帧动画之SurfaceView 滑动窗口式帧复用...
查看>>
vue使用element-ui的日期选择器,框内默认显示当前时间
查看>>
左滑显示删除按钮
查看>>
Data Lake Analytics: 使用DataWorks来调度DLA任务
查看>>
Android V1及V2签名原理简析
查看>>
Hy (lisp)
查看>>
Android 8.0 新特性 之通知、自适应图标
查看>>
JavaScript模块化01:模块化进程
查看>>
在 React 中使用 Redux
查看>>
npm script 命令补全的实现
查看>>
微信小程序--根据首字母选择城市
查看>>
BCHD开发出可替代BCH全节点的公开API——gRPC
查看>>
Python 爬虫的工具链
查看>>
python 使用PyQt5
查看>>