阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Docker使用教程

145次阅读
没有评论

共计 9951 个字符,预计需要花费 25 分钟才能阅读完成。

当我们把 CentOS 6.5 安装好以后,可以使用这个脚本来使用国内的 阿里云 镜像源

#!/bin/bash
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum makecache

有了国内的源地址,后面安装东西就方便了。
搜索 docker,可以看到 docker-io.x86_64 和 docker-registry.noarch,安装 docker 就简单了。

[root@localhost ~]# yum search docker
 Loaded plugins: fastestmirror
 Loading mirror speeds from cached hostfile
 ======================================================== N/S Matched: docker =========================================================
 Python-docker-py.x86_64 : An API client for docker written in Python
 docker-io.x86_64 : Automates deployment of containerized applications
 docker-registry.noarch : Registry server for Docker
 Fedora-dockerfiles.noarch : Example dockerfiles to assist standing up containers quickly
 imagefactory-plugins-Docker.noarch : Cloud plugin for Docker
 docker.x86_64 : KDE and GNOME2 system tray replacement docking application

  Name and summary matches only, use “search all” for everything.

 

CentOS 6/ 7 系列安装 Docker http://www.linuxidc.com/Linux/2014-07/104768.htm 

Docker 的搭建 Gitlab CI 全过程详解 http://www.linuxidc.com/Linux/2013-12/93537.htm 

Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm 

Docker 和一个正常的虚拟机有何区别? http://www.linuxidc.com/Linux/2013-12/93740.htm 

在 Docker 中使用 MySQL http://www.linuxidc.com/Linux/2014-01/95354.htm 

Docker 将改变所有事情 http://www.linuxidc.com/Linux/2013-12/93998.htm 

Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm 

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-09/106675p2.htm

Docker 使用系列一我们把镜像源改为了 阿里云 的,方便后续的操作。

执行这个命令就把源地址改为阿里的:

curl https://git.oschina.net/linuxidc/Docker_shell/raw/start/ali-CentOS.sh | sh

第一安装 Docker:

yum -y install docker-io

第二启动 Docker:

service docker start

第三安装制作 CentOS 镜像的工具:

yum -y install febootstrap

第四制作 CentOS 镜像文件 centos6-image 目录

febootstrap -i bash -i wget -i yum -i iputils -i iproute -i man -i vim-minimal -i openssh-server -i openssh-clients centos6 centos6-image http://mirrors.aliyun.com/centos/6/os/x86_64/

第五制作 Docker 镜像,镜像名字是 centos6-base

cd centos6-image && tar -c .|docker import – centos6-base

第六制作可以 ssh 登陆的 Docker 镜像,名字是 centos6-ssh

docker build -t centos6-ssh https://git.oschina.net/linuxidc/Docker_shell/raw/start/Dockerfile

经过前面的六个步骤,一个可以登陆的本地 docker 镜像就制作好了。

用户名是:root,密码是:123456

体验步骤:

创建容器:

docker run -d -p 127.0.0.1:33301:22 centos6-ssh

ssh 登陆容器:

ssh root@127.0.0.1 -p 33301

输入密码后就可以开始折腾了。

通过授信无密码登陆请看第四篇:

Docker 使用系列四:CentOS 6.5 制作自动授信 Docker 容器镜像 http://www.linuxidc.com/Linux/2014-09/106675p4.htm

当我们把 CentOS 6.5 安装好以后,可以使用这个脚本来使用国内的 阿里云 镜像源

#!/bin/bash
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum makecache

有了国内的源地址,后面安装东西就方便了。
搜索 docker,可以看到 docker-io.x86_64 和 docker-registry.noarch,安装 docker 就简单了。

[root@localhost ~]# yum search docker
 Loaded plugins: fastestmirror
 Loading mirror speeds from cached hostfile
 ======================================================== N/S Matched: docker =========================================================
 Python-docker-py.x86_64 : An API client for docker written in Python
 docker-io.x86_64 : Automates deployment of containerized applications
 docker-registry.noarch : Registry server for Docker
 Fedora-dockerfiles.noarch : Example dockerfiles to assist standing up containers quickly
 imagefactory-plugins-Docker.noarch : Cloud plugin for Docker
 docker.x86_64 : KDE and GNOME2 system tray replacement docking application

  Name and summary matches only, use “search all” for everything.

 

CentOS 6/ 7 系列安装 Docker http://www.linuxidc.com/Linux/2014-07/104768.htm 

Docker 的搭建 Gitlab CI 全过程详解 http://www.linuxidc.com/Linux/2013-12/93537.htm 

Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm 

Docker 和一个正常的虚拟机有何区别? http://www.linuxidc.com/Linux/2013-12/93740.htm 

在 Docker 中使用 MySQL http://www.linuxidc.com/Linux/2014-01/95354.htm 

Docker 将改变所有事情 http://www.linuxidc.com/Linux/2013-12/93998.htm 

Docker 安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm 

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-09/106675p2.htm

所有操作都是用 root 账号在 CentOS 6.5 上完成,假设没有安装 docker

依次使用 root 执行后面的命令就可以了

curl https://git.oschina.net/linuxidc/Docker_shell/raw/start/ali-centos.sh | sh
yum -y install docker-io
service docker start
yum -y install debootstrap
debootstrap –arch amd64 –include=vim,openssh-server,openssh-client trusty Ubuntu-trusty http://mirrors.aliyun.com/ubuntu/
cd ubuntu-trusty && tar -c .|docker import – ubuntu1404-base
docker build -t ubuntu1404-ssh https://git.oschina.net/linuxidc/Docker_shell/raw/start/trustyDockerfile
docker run -d -p 127.0.0.1:33303:22 ubuntu1404-ssh

前面完成后,登陆容器:

ssh root@127.0.0.1 -p 33303

输入密码:123456

效果如下:

[root@localhost lists]# ssh root@127.0.0.1 -p 33303
root@127.0.0.1’s password:
Welcome to Ubuntu 14.04 LTS (GNU/Linux 2.6.32-431.11.2.el6.x86_64 x86_64)
 
 * Documentation:  https://help.ubuntu.com/
Last login: Fri Apr 18 08:14:45 2014 from 172.17.42.1
root@0b26318a0d63:~# cat /etc/issue
Ubuntu 14.04 LTS \n \l
 
root@0b26318a0d63:~# uname -a
Linux 0b26318a0d63 2.6.32-431.11.2.el6.x86_64 #1 SMP Tue Mar 25 19:59:55 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

从上面可以看出,内核还是使用的 CentOS 的,只是版本和软件编程了 ubuntu 14.04
这好像是个问题

这篇文章依赖于 http://www.linuxidc.com/Linux/2014-09/106675p2.htm,在前面的文章中我们制作好了两个镜像,

CentOS6-base 和 centos6-ssh,今天我们要依赖于 centos6-ssh 镜像,制作第三个自动授信的镜像 centos-newssh。

方法如下:

curl https://git.oschina.net/linuxidc/Docker_shell/raw/start/Docker-centos-autossh.sh | sh

一个命令就行了,这条命令会创建一个新镜像 centos-newssh

开始体验,现在不需要输入 root 密码了:

docker run -d -p 127.0.0.1:33306:22 -m 256M centos-newssh
ssh root@127.0.0.1 -p 33306 “echo $LANG;ifconfig”

如果不想第一次连接的时候输入 yes,确认连接,可以使用下面的命令:

ssh -o StrictHostKeyChecking=no root@127.0.0.1 -p 33306 “echo $LANG;ifconfig”

体验完了,我们来分析下

https://git.oschina.net/linuxidc/Docker_shell/raw/start/Docker-centos-autossh.sh

#!/bin/sh
[! -d /root/.ssh] && mkdir -p /root/.ssh
ssh-keygen -q -N “” -t dsa -f /root/.ssh/id_dsa
KEY_PUB=`cat /root/.ssh/id_dsa.pub`
[! -d /root/docker-temp] && mkdir /root/docker-temp
cd /root/docker-temp
cat >/root/docker-temp/Dockerfile <<EOF
#Dockerfile
FROM centos6-ssh
MAINTAINER linuxidc <feedao@163.com>
EOF
echo “RUN echo \”$KEY_PUB\” > /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys ” >>Dockerfile
cd /root && cat /root/docker-temp/Dockerfile | docker build -t centos-newssh –
 

脚本内容比较简单,就是生成新的秘钥和公钥

ssh-keygen -q -N “” -t dsa -f /root/.ssh/id_dsa
 
然后把公钥自动加到 Docker 容器中,这里是修改 Dockerfile:

echo “RUN echo \”$KEY_PUB\” > /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys ” >>Dockerfile
 
最后创建新的镜像:

cd /root && cat /root/docker-temp/Dockerfile | docker build -t centos-newssh –

你可以把 /root/docker-temp 文件夹删除 

rm -rf /root/docker-temp

在国内使用 docker pull 总是不能正常拉取文件下来,现在可以自己制作镜像服务器了

希望你已经有了无需密码就能登陆的 CentOS-newssh 镜像,如果还没有,请看 Docker 使用系列四:http://www.linuxidc.com/Linux/2014-09/106675p4.htm

制作 Docker 镜像私服步骤如下:

本文假设本机 IP 是 192.168.56.101,Registry 服务的域名是 registry.linuxidc.com

第一步执行下面的命令会创建一个名为 centos-registry 镜像:

docker build -t centos-registry https://git.oschina.net/linuxidc/Docker_shell/raw/start/Dockerfile-registry
 

第二步,创建 registry 容器,这里要监听 22 和 5000 端口

docker run -d -p 127.0.0.1:33307:22 -p 127.0.0.1:5000:5000 centos-registry

测试:

[root@docker-base docker-temp]# curl 127.0.0.1:5000

出现:”docker-registry server (dev)”  代表 registry 进程自动了

第三步,配置 nginx:
 
yum -y install nginx
service nginx start
 
echo -e “server {\n    listen 80;\n    server_name registry.linuxidc.com;\n    location / {\n        proxy_set_header Host \$host;\n        proxy_pass http://127.0.0.1:5000/;\n}\n}” >>/etc/nginx/conf.d/virtual.conf
 
service nginx reload
 
echo “127.0.0.1 registry.linuxidc.com” >> /etc/hosts

测试:

[root@docker-base docker-temp]# curl registry.linuxidc.com

出现 “docker-registry server (dev)” 代表成功

第四步,使用自己的 registry 服务器

把本机镜像 tag 一下:
 
docker tag centos-newssh registry.linuxidc.com/centos-newssh

把刚才 tag 的镜像 Push 到 registry 服务器上:

docker push registry.linuxidc.com/centos-newssh

第五步,换台服务器配置 hosts 文件,比如我的 registry 服务器 IP 是 192.168.56.101,

在另外一台服务器上 192.168.56.102 上配置 hosts
 
echo “192.168.56.101 registry.linuxidc.com” >> /etc/hosts
 

然后执行:

docker pull registry.linuxidc.com/centos-newssh

创建容器:docker run -d -p 127.0.0.1:33306:22 -m 256M registry.linuxidc.com/centos-newssh

登陆:ssh root@127.0.0.1 -p 33306

密码是:123456

这样我们的私服就利用起来了。

前面我们在 192.168.56.101 上有了 registry.linuxidc.com 私有的镜像源服务器,并且有了 registry.linuxidc.com/CentOS-newssh 镜像。

今天我们在 192.168.56.103 上制作 DNS 服务镜像,当然最好有点 bind9 的经验。

第一步,配置 Hosts,获取 docker 镜像:

echo “192.168.56.101 registry.linuxidc.com” >> /etc/hosts

第二步,这里需要注意 udp 协议,256M 是内存大小:

docker run -d -p 127.0.0.1:33306:22 -p 53:53 -p 53:53/udp 256M registry.linuxidc.com/centos-newssh
 

第三步:登陆容器

ssh 127.0.0.1 -p 33306

第四步:安装 bind9

yum install bind bind-utils -y

第五步:修改配置文件 /etc/named.conf,options 参看下面:

options {
    forwarders {8.8.8.8; 8.8.4.4;};
    listen-on port 53 {any;};
    listen-on-v6 port 53 {::1;};
    directory  “/var/named”;
    dump-file  “/var/named/data/cache_dump.db”;
        statistics-file “/var/named/data/named_stats.txt”;
        memstatistics-file “/var/named/data/named_mem_stats.txt”;
    allow-query    {any;};
    recursion yes;
 
    dnssec-enable yes;
    dnssec-validation yes;
    dnssec-lookaside auto;
 
    /* Path to ISC DLV key */
    bindkeys-file “/etc/named.iscdlv.key”;
 
    managed-keys-directory “/var/named/dynamic”;
};
 

在 /etc/named.conf,假设咱们的域名是 linuxidc.com 文件中添加

zone “linuxidc.com” {
    type master;
    file “db.linuxidc.com”; 
};
 

第六步:进入 /var/named/,添加 db.linuxidc.com,内容如下:

$TTL    604800
@      IN      SOA    linuxidc.com. www.linuxidc.com. (
                              3        ; Serial
                        604800        ; Refresh
                          86400        ; Retry
                        2419200        ; Expire
                        604800 )      ; Negative Cache TTL
;
@      IN      NS      linuxidc.com.
@      IN      A      192.168.56.101
registry    IN      A      192.168.56.101
*      IN      A      192.168.56.101
 

第七步:启动 named 服务:
 
service named start

第八步:由于容器里面不能修改 dns 所在退出容器,修改 /etc/resolv.conf:
 
mv /etc/resolv.conf /etc/resolv.conf.bak
echo “nameserver 192.168.56.103” >/etc/resolv.conf

第九步:测试,安装 bind-utils

yum -y install bind-utils

测试:
 
dig @192.168.56.103 . linuxidc.com

如果有反应就说明成功了。

第十步:去掉 /etc/hosts 里面的 192.168.56.101 registry.linuxidc.com 测试:

curl registry.linuxidc.com

会出现跟配置 Hosts 一样的效果,以后只要把新的机器的 dns 配置成 192.168.56.103 就好了。

后记:

这里只是完成了 dns 服务的安装和使用,写一个 Dockerfile 就可以制作成镜像了,后面有时间再写个脚本。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计9951字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中