506629361 发表于 2015-4-18 06:03:41

Docker学习笔记1 :镜像制作

  参考资源:
  http://blog.iyunv.com/kongxx?viewmode=contents
  http://my.oschina.net/feedao/blog
  ==============================


[*]运行环境
  win8.1 + virtual box, 运行 centos6.4 64bit, 内网通过代理上网。
  如下操作基本都在root下进行。


[*]目的
  尝试自己建立Docker镜像


[*]基础工作
  1,阿里云镜像
  执行如下脚本,将资源镜像执行阿里云



#!/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
  在windows下编辑后,回车换行符需要替换
  使用dos2unix命令



$ dos2unix -k sample.txt
  2,wget上网代理
  root用户
  增加/root/.wgetrc 文件,定义代理服务器及用户名密码



http-proxy=proxy_ip:port
https-proxy=proxy_ip:port
ftp-proxy=proxy_ip:port
proxy-user=username
proxy-passwd=password
  重新登录一下
  联网不使用代理时使用 --no-proxy 参数
  


[*]Docker ubuntu 镜像制作
  1,安装docker
  对于CentOS6,可以使用EPEL库安装Docker,命令如下



# yum install http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm
# yum install docker-io
  安装之后启动Docker服务,并让它随系统启动自动启动。



# service docker start
# chkconfig docker on
  2,安装工具debbootstrap



yum -y install debootstrap
  3,编写自己的Dockerfile,以ubuntu 14.04 ssh为例: *在docker build 时使用



# 选择一个已有的os镜像作为基础
FROM ubuntu
# 镜像的作者
MAINTAINER Leonard Tian "leonard.tianlb@gmail.com"
# 安装openssh-server和sudo软件包,并且将sshd的UsePAM参数设置成no
RUN apt-get install -y openssh-server sudo
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
# 添加测试用户tianlb,密码123456,并且将此用户添加到sudoers里
RUN useradd tianlb
RUN echo "tianlb:123456" | chpasswd
RUN echo "tianlb   ALL=(ALL)       ALL" >> /etc/sudoers
# 下面这两句比较特殊,在centos6上必须要有,否则创建出来的容器sshd不能登录
# RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
# RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
# 启动sshd服务并且暴露22端口
RUN mkdir /var/run/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
  注意Dockerfile文件的首字母大写
  将镜像源转换为阿里云的,参考1.阿里云镜像
  4, 创建image



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 .
  * docker build 目前有问题
  查看docker image



# docker images
  REPOSITORY     TAG   IMAGE ID   CREATED         VIRTUAL SIZE
  ubuntu1404-base  latest            6457f5dac973      About an hour ago   287.5 MB
  5, 创建容器



docker run -t -i ubuntu1404-base /bin/bash
  直接进入容器,可以进行基本操作,如ls等。
  6,异常处理
  Docker在CentoOS6运行一个容器的时候出现下面的错误



# docker run -i -t ubuntu1404-base /bin/bash
unable to remount sys readonly: unable to mount sys as readonly max retries reached
  碰到这个问题需要修改Docker的配置参数把/etc/sysconfig/docker文件中的other-args更改为:



other_args="--exec-driver=lxc --selinux-enabled"
  然后重新启动Docker服务



sudo /etc/init.d/docker restart
  7, 其他命令



#查看docker容器,及状态
docker ps -a
#启动容器
docker start ID
#停止容器
docker stop ID
#删除容器
docker rm ID
  --End--
页: [1]
查看完整版本: Docker学习笔记1 :镜像制作