docker制作镜像(系统自身命令,docker commit|build)

由于镜像下载都比较慢,而且不好找寻自己需要的版本
这里介绍centos和Ubuntu的镜像制作
1.制作centos镜像
在centos系统中
yum -y install febootstrap
febootstrap -i bash -i lrzsz -i wget -i yum -i iputils -i iproute centos6 centos63 http://mirrors.163.com/centos/6/os/x86_64/ (file:///iso)
最后这里可以是http、ftp、file等等,如果没有本地镜像也可以使用网络yum源
(-i 安装package, centos6 操作系统版本,centos63安装目录,最后是源地址)


2.导入到docker镜像中
在docker服务器中
root@eddy:~/centos6-doc# tar -c .|docker import - centos63-bash
centos63-bash为docker镜像名字
root@eddy:~/centos6-doc# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos63-bash       latest              9a769720073e        53 seconds ago      289.6 MB
ubuntu-base         latest              a92d7a8df1bc        57 minutes ago      172.7 MB

启动一个容器
root@eddy:~/centos6-doc# docker run -t -i centos63-bash /bin/bash

bash-4.1# cat /etc/redhat-release  
Red Hat Enterprise Linux Server release 6.3 (Santiago)
root@eddy:~/centos6-doc# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
c163db7fb946        centos63-bash       "/bin/bash"              41 seconds ago      Exited (0) 5 seconds ago                          trusting_stallman
6ac95abee8be        ubuntu-base         "/bin/bash -c 'while "   35 minutes ago      Exited (137) 23 minutes ago                       dc1
55d94cfcc622        ubuntu-base         "/bin/bash"              40 minutes ago      Exited (0) 38 minutes ago                         adoring_jones
9b85185b9b9e        ubuntu-base         "/bin/bash"              47 minutes ago      Exited (0) 45 minutes ago                         contain01
6c232c270b78        ubuntu-base         "/bin/bash"              51 minutes ago      Exited (0) 51 minutes ago                         trusting_borg
d1f43662f2db        ubuntu-base         "echo 'hello world'"     53 minutes ago      Exited (0) 53 minutes ago                         condescending_swirles

3.制作Ubuntu镜像
与centos镜像制作类似
登陆一台新安装的Ubuntu服务器
sudo apt-get install debootstrap
debootstrap --arch amd64 precise ubuntu1204 http://mirrors.163.com/ubuntu/
cd ubuntu1204/
tar -c .|docker import - ubuntu1204-base
制作ubuntu镜像
root@eddy:~/centos6-doc# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos63-bash       latest              9a769720073e        5 minutes ago       289.6 MB
ubuntu-base         latest              a92d7a8df1bc        About an hour ago   172.7 MB

4.通过容器构建镜像
docker commit
root@eddy:~# docker commit --help

Usage:	docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

  -a, --author=       Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change=[]     Apply Dockerfile instruction to the created image
  --help=false        Print usage
  -m, --message=      Commit message
  -p, --pause=true    Pause container during commit


root@eddy:~# docker run -it -p 80 --name commit-test centos63-bash /bin/bash 
bash-4.1# yum install -y nginx
bash-4.1# exit 
exit
root@eddy:~# 
root@eddy:~# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
6cd16734303d        centos63-bash       "/bin/bash"         2 minutes ago       Exited (0) 8 seconds ago                       commit-test
root@eddy:~# docker commit -a 'eddy' -m 'nginx' commit-test eddy/centos_nginx
72a842011cc198d34748b458d65408d2149c03af22e2e8c8da93e18a74ba29a8
root@eddy:~# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
eddy/centos_nginx               latest              72a842011cc1        19 seconds ago      452.8 MB

构建容器
root@eddy:~# docker run -d --name nginx_web1 -p 80 eddy/centos_nginx nginx -g "daemon off;"
root@eddy:~# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
7a0c8c438db7        eddy/centos_nginx   "nginx -g 'daemon off"   13 seconds ago      Up 12 seconds       0.0.0.0:32770->80/tcp   nginx_web1
root@eddy:~# docker top nginx_web1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                4091                3395                0                   13:13               ?                   00:00:00            nginx: master process nginx -g daemon off;
499                 4101                4091                0                   13:13               ?                   00:00:00            nginx: worker process

5.通过docker file 构建镜像
docker build
dockerfile就是包含一系列docker命令的文本文件
root@eddy:~# docker build --help

Usage:	docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

  --build-arg=[]                  Set build-time variables
  --cpu-shares=0                  CPU shares (relative weight)
  --cgroup-parent=                Optional parent cgroup for the container
  --cpu-period=0                  Limit the CPU CFS (Completely Fair Scheduler) period
  --cpu-quota=0                   Limit the CPU CFS (Completely Fair Scheduler) quota
  --cpuset-cpus=                  CPUs in which to allow execution (0-3, 0,1)
  --cpuset-mems=                  MEMs in which to allow execution (0-3, 0,1)
  --disable-content-trust=true    Skip image verification
  -f, --file=                     Name of the Dockerfile (Default is 'PATH/Dockerfile')
  --force-rm=false                Always remove intermediate containers
  --help=false                    Print usage
  -m, --memory=                   Memory limit
  --memory-swap=                  Total memory (memory + swap), '-1' to disable swap
  --no-cache=false                Do not use cache when building the image
  --pull=false                    Always attempt to pull a newer version of the image
  -q, --quiet=false               Suppress the verbose output generated by the containers
  --rm=true                       Remove intermediate containers after a successful build
  -t, --tag=                      Repository name (and optionally a tag) for the image
  --ulimit=[]                     Ulimit options

dockerfile文件
FROM eddy/centos_nginx:latest
MAINTAINER eddy "eddy@qq.com"
RUN yum install -y vim
EXPOSE 80
root@eddy:~/dockfile/df_test1# docker build -t "eddy/df_test1" .
root@eddy:~/dockfile/df_test1# docker run -d --name nginx_web2 -p 80 eddy/df_test1 nginx -g "daemon off;"
ec56afa0d5f766777f1d8da40e4f6916bb58988c06268163ba2b37a192b81f46
root@eddy:~/dockfile/df_test1# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
ec56afa0d5f7        eddy/df_test1       "nginx -g 'daemon off"   7 seconds ago       Up 6 seconds        0.0.0.0:32771->80/tcp   nginx_web2
7a0c8c438db7        eddy/centos_nginx   "nginx -g 'daemon off"   11 minutes ago      Up 11 minutes       0.0.0.0:32770->80/tcp   nginx_web1


转载于:https://my.oschina.net/eddylinux/blog/607722

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值