docker开发_在basic image的基础上创建自定义的image

方法一:docker commit

1. 跑一个basic image,docker新建了一个容器

root@ubuntu:/home/thm/docker/test# docker run -i -t tang*0713/ubuntu_amd64:14.04 /bin/bash
WARNING: Local (127.0.0.1) DNS resolver found in resolv.conf and containers can't use it. Using default external servers : [8.8.8.8 8.8.4.4]
root@5d807872b2ba:/#

2. 在容器内安装apache2

root@5d807872b2ba:/# apt-get update
Ign http://archive.ubuntu.com trusty InRelease
Hit http://archive.ubuntu.com trusty Release.gpg
Hit http://archive.ubuntu.com trusty Release
Hit http://archive.ubuntu.com trusty/main i386 Packages
Get: http://archive.ubuntu.com trusty/main Translation-en [762 kB]
Fetched kB in 34s (22.1 kB/s)
Reading package lists... Done
root@5d807872b2ba:/#
root@5d807872b2ba:/# apt-get install apache2
Reading package lists... Done
Building dependency tree... Done
The following extra packages will be installed:
...
...
...

3. 退出容器,将刚才装了apache的容器创建成一个新的image

root@5d807872b2ba:/# exit
exit
root@ubuntu:/home/thm/docker/test#
root@ubuntu:/home/thm/docker/test# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d807872b2ba tang*0713/ubuntu_amd64:14.04 /bin/bash minutes ago Exit nostalgic_heisenberg
root@ubuntu:/home/thm/docker/test# docker commit 5d807872b2ba tang*0713/ubuntu_amd64_apache2:14.04
e7df0b3ab526c83098d54e826b4e8e1f2c6efbe0a2511c3b06b79723bacfff52
root@ubuntu:/home/thm/docker/test# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
tang*0713/ubuntu_amd64_apache2 14.04 e7df0b3ab526 seconds ago 248.1 MB
scratch latest 6e1aa483880d About an hour ago B
tang*0713/ubuntu_amd64 14.04 6cfee0d4349b hours ago 216.8 MB
tang*0713/ubuntu_amd64 latest 6cfee0d4349b hours ago 216.8 MB
tang*0713/ubuntu_i386 14.04 ee57f53fe012 hours ago 216.8 MB
tang*0713/ubuntu_i386 latest ee57f53fe012 hours ago 216.8 MB
ubuntu 14.04 c4ff7513909d days ago 225.4 MB
ubuntu trusty c4ff7513909d days ago 225.4 MB
ubuntu 14.04. c4ff7513909d days ago 225.4 MB
ubuntu latest c4ff7513909d days ago 225.4 MB
ubuntu 14.10 75204fdb260b days ago 230.1 MB
ubuntu utopic 75204fdb260b days ago 230.1 MB
ubuntu 12.04 822a01ae9a15 days ago 108.1 MB
ubuntu 12.04. 822a01ae9a15 days ago 108.1 MB
ubuntu precise 822a01ae9a15 days ago 108.1 MB
ubuntu 12.10 c5881f11ded9 weeks ago 172.2 MB
ubuntu quantal c5881f11ded9 weeks ago 172.2 MB
ubuntu 13.04 463ff6be4238 weeks ago 169.4 MB
ubuntu raring 463ff6be4238 weeks ago 169.4 MB
ubuntu 13.10 195eb90b5349 weeks ago 184.7 MB
ubuntu saucy 195eb90b5349 weeks ago 184.7 MB
jamtur01/puppetmaster latest 99200e07340a weeks ago 312.4 MB
ubuntu 10.04 3db9c44f4520 months ago MB
ubuntu lucid 3db9c44f4520 months ago MB
root@ubuntu:/home/thm/docker/test#

4. run一下刚刚新创建的image,看看container内是否存在刚刚安装的apache2

root@ubuntu:/home/thm/docker/test# docker run -t -i tang*0713/ubuntu_amd64_apache2:14.04 /bin/bash
WARNING: Local (127.0.0.1) DNS resolver found in resolv.conf and containers can't use it. Using default external servers : [8.8.8.8 8.8.4.4]
root@081572d422c7:/#
root@081572d422c7:/#
root@081572d422c7:/# apache
apache2 apache2ctl apachectl

5. 将新创建的image同步到docker hub

docker push tang*0713/ubuntu_amd64_apache2

方法二:Dockerfile

1. 在一个空目录下创建Dockerfile,并在Dockerfile中写入如下类似批处理的语句

root@ubuntu:/home/thm/docker/test# touch Dockerfile
root@ubuntu:/home/thm/docker/test# vim Dockerfile
   FROM tang*0713/ubuntu_amd64:14.04
RUN apt-get update
RUN apt-get install -y nginx
RUN echo "Hi, I am in your container" > /usr/share/nginx/html/index.html
EXPOSE

Dockerfile中相关命令解释:

FROM: 以"tang*0713/ubuntu_amd64:14.04"为basic image运行一个container;

RUN: 在container中执行命令;

EXPOSE: 将镜像的80端口暴露在外,这样容器外可以看到这个端口并与其通信;

2. docker build创建image

root@ubuntu:/home/thm/docker/test#
root@ubuntu:/home/thm/docker/test# docker build -t="tang*0713/ubuntu_amd64_nginx:14.04" .
Uploading context 2.56 kB
Uploading context
Step : FROM tang*0713/ubuntu_amd64:14.04
---> 6cfee0d4349b
Step : RUN apt-get update
---> Running in dedead78ac76
Ign http://archive.ubuntu.com trusty InRelease
Hit http://archive.ubuntu.com trusty Release.gpg
Hit http://archive.ubuntu.com trusty Release
Hit http://archive.ubuntu.com trusty/main i386 Packages
Get: http://archive.ubuntu.com trusty/main Translation-en [762 kB]
Fetched kB in 2min 16s ( B/s)
Reading package lists...
---> 3af4720f8ec3
Step : RUN apt-get install -y nginx
---> Running in e6a4a63286fe
Reading package lists...
Building dependency tree...
The following extra packages will be installed:
...
...
...
Processing triggers for libc-bin (2.19-0ubuntu6) ...
Processing triggers for sgml-base (1.26+nmu4ubuntu1) ...
---> 409a0d5cd91b
Step : RUN echo "Hi, I am in your container" > /usr/share/nginx/html/index.html
---> Running in 783d8789509c
---> 9a5d96a41a0b
Step : EXPOSE
---> Running in a2e24fc2d4b9
---> 6cd5691b7c8c
Successfully built 6cd5691b7c8c
Removing intermediate container dedead78ac76
Removing intermediate container e6a4a63286fe
Removing intermediate container 783d8789509c
Removing intermediate container a2e24fc2d4b9
root@ubuntu:/home/thm/docker/test#

3.  查看image是否创建成功

root@ubuntu:/home/thm/docker/test# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
tang*0713/ubuntu_amd64_nginx 14.04 6cd5691b7c8c 30 seconds ago 259.2 MB
scratch latest 6e1aa483880d hours ago B
tang*0713/ubuntu_amd64 14.04 6cfee0d4349b hours ago 216.8 MB
tang*0713/ubuntu_amd64 latest 6cfee0d4349b hours ago 216.8 MB
tang*0713/ubuntu_i386 14.04 ee57f53fe012 hours ago 216.8 MB
tang*0713/ubuntu_i386 latest ee57f53fe012 hours ago 216.8 MB
ubuntu 14.04. c4ff7513909d days ago 225.4 MB
ubuntu 14.04 c4ff7513909d days ago 225.4 MB
ubuntu latest c4ff7513909d days ago 225.4 MB
ubuntu trusty c4ff7513909d days ago 225.4 MB

4. run一下新创建的image

root@ubuntu:/home/thm/docker/test# docker run -i -t tang*0713/ubuntu_amd64_nginx:14.04 /bin/bash
root@45494a615738:/#
root@45494a615738:/# which nginx
/usr/sbin/nginx

nginx命令是存在的,成功。

5. 将新创建的image同步到docker hub

docker push tang*0713/ubuntu_amd64_nginx:14.04

上一篇:Unix中库的使用


下一篇:matlab 乱七八糟求最大值~