方法一:docker commit
1. 跑一个basic image,docker新建了一个容器
root@Ubuntu:/home/thm/docker/test# docker run -i -t tanghuimin0713/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 updateIgn http://archive.ubuntu.com trusty InRelease Hit http://archive.ubuntu.com trusty Release.gpgHit http://archive.ubuntu.com trusty ReleaseHit http://archive.ubuntu.com trusty/main i386 PackagesGet:1 http://archive.ubuntu.com trusty/main Translation-en [762 kB]Fetched 762 kB in 34s (22.1 kB/s) Reading package lists... Doneroot@5d807872b2ba:/# root@5d807872b2ba:/# apt-get install apache2Reading package lists... DoneBuilding dependency tree... DoneThe following extra packages will be installed:.........
3. 退出容器,将刚才装了apache的容器创建成一个新的image
root@5d807872b2ba:/# exit exitroot@ubuntu:/home/thm/docker/test# root@ubuntu:/home/thm/docker/test# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5d807872b2ba tanghuimin0713/ubuntu_amd64:14.04 /bin/bash 7 minutes ago Exit 0 nostalgic_heisenberg root@ubuntu:/home/thm/docker/test# docker commit 5d807872b2ba tanghuimin0713/ubuntu_amd64_apache2:14.04e7df0b3ab526c83098d54e826b4e8e1f2c6efbe0a2511c3b06b79723bacfff52root@ubuntu:/home/thm/docker/test# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEtanghuimin0713/ubuntu_amd64_apache2 14.04 e7df0b3ab526 3 seconds ago 248.1 MBscratch latest 6e1aa483880d About an hour ago 0 Btanghuimin0713/ubuntu_amd64 14.04 6cfee0d4349b 4 hours ago 216.8 MBtanghuimin0713/ubuntu_amd64 latest 6cfee0d4349b 4 hours ago 216.8 MBtanghuimin0713/ubuntu_i386 14.04 ee57f53fe012 5 hours ago 216.8 MBtanghuimin0713/ubuntu_i386 latest ee57f53fe012 5 hours ago 216.8 MBubuntu 14.04 c4ff7513909d 9 days ago 225.4 MBubuntu trusty c4ff7513909d 9 days ago 225.4 MBubuntu 14.04.1 c4ff7513909d 9 days ago 225.4 MBubuntu latest c4ff7513909d 9 days ago 225.4 MBubuntu 14.10 75204fdb260b 9 days ago 230.1 MBubuntu utopic 75204fdb260b 9 days ago 230.1 MBubuntu 12.04 822a01ae9a15 9 days ago 108.1 MBubuntu 12.04.5 822a01ae9a15 9 days ago 108.1 MBubuntu PRecise 822a01ae9a15 9 days ago 108.1 MBubuntu 12.10 c5881f11ded9 9 weeks ago 172.2 MBubuntu quantal c5881f11ded9 9 weeks ago 172.2 MBubuntu 13.04 463ff6be4238 9 weeks ago 169.4 MBubuntu raring 463ff6be4238 9 weeks ago 169.4 MBubuntu 13.10 195eb90b5349 9 weeks ago 184.7 MBubuntu saucy 195eb90b5349 9 weeks ago 184.7 MBjamtur01/puppetmaster latest 99200e07340a 11 weeks ago 312.4 MBubuntu 10.04 3db9c44f4520 4 months ago 183 MBubuntu lucid 3db9c44f4520 4 months ago 183 MBroot@ubuntu:/home/thm/docker/test#
4. run一下刚刚新创建的image,看看container内是否存在刚刚安装的apache2
root@ubuntu:/home/thm/docker/test# docker run -t -i tanghuimin0713/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:/# apacheapache2 apache2ctl apachectl
5. 将新创建的image同步到docker hub
docker push tanghuimin0713/ubuntu_amd64_apache2
方法二:Dockerfile
1. 在一个空目录下创建Dockerfile,并在Dockerfile中写入如下类似批处理的语句
root@ubuntu:/home/thm/docker/test# touch Dockerfileroot@ubuntu:/home/thm/docker/test# vim Dockerfile
1 FROM tanghuimin0713/ubuntu_amd64:14.04 2 RUN apt-get update 3 RUN apt-get install -y nginx 4 RUN echo "Hi, I am in your container" > /usr/share/nginx/html/index.html 5 EXPOSE 80
Dockerfile中相关命令解释:
FROM: 以"tanghuimin0713/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="tanghuimin0713/ubuntu_amd64_nginx:14.04" .Uploading context 2.56 kBUploading context Step 0 : FROM tanghuimin0713/ubuntu_amd64:14.04 ---> 6cfee0d4349bStep 1 : RUN apt-get update ---> Running in dedead78ac76Ign http://archive.ubuntu.com trusty InReleaseHit http://archive.ubuntu.com trusty Release.gpgHit http://archive.ubuntu.com trusty ReleaseHit http://archive.ubuntu.com trusty/main i386 PackagesGet:1 http://archive.ubuntu.com trusty/main Translation-en [762 kB]Fetched 762 kB in 2min 16s (5571 B/s)Reading package lists... ---> 3af4720f8ec3Step 2 : RUN apt-get install -y nginx ---> Running in e6a4a63286feReading 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) ... ---> 409a0d5cd91bStep 3 : RUN echo "Hi, I am in your container" > /usr/share/nginx/html/index.html ---> Running in 783d8789509c ---> 9a5d96a41a0bStep 4 : EXPOSE 80 ---> Running in a2e24fc2d4b9 ---> 6cd5691b7c8cSuccessfully built 6cd5691b7c8cRemoving intermediate container dedead78ac76Removing intermediate container e6a4a63286feRemoving intermediate container 783d8789509cRemoving intermediate container a2e24fc2d4b9root@ubuntu:/home/thm/docker/test#
3. 查看image是否创建成功
root@ubuntu:/home/thm/docker/test# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEtanghuimin0713/ubuntu_amd64_nginx 14.04 6cd5691b7c8c 30 seconds ago 259.2 MBscratch latest 6e1aa483880d 2 hours ago 0 Btanghuimin0713/ubuntu_amd64 14.04 6cfee0d4349b 5 hours ago 216.8 MBtanghuimin0713/ubuntu_amd64 latest 6cfee0d4349b 5 hours ago 216.8 MBtanghuimin0713/ubuntu_i386 14.04 ee57f53fe012 6 hours ago 216.8 MBtanghuimin0713/ubuntu_i386 latest ee57f53fe012 6 hours ago 216.8 MBubuntu 14.04.1 c4ff7513909d 9 days ago 225.4 MBubuntu 14.04 c4ff7513909d 9 days ago 225.4 MBubuntu latest c4ff7513909d 9 days ago 225.4 MBubuntu trusty c4ff7513909d 9 days ago 225.4 MB
4. run一下新创建的image
root@ubuntu:/home/thm/docker/test# docker run -i -t tanghuimin0713/ubuntu_amd64_nginx:14.04 /bin/bashroot@45494a615738:/# root@45494a615738:/# which nginx/usr/sbin/nginx
nginx命令是存在的,成功。
5. 将新创建的image同步到docker hub
docker push tanghuimin0713/ubuntu_amd64_nginx:14.04
新闻热点
疑难解答