12. 镜像创建和共享

1. 把容器中的变更commit到一个新镜像中

图片.png

说明:只要容器没有rm,都可以提交,不需要是start状态。

我们可以看看现在的容器相对于原来的镜像都有哪些区别:
图片.png

2. 把镜像和容器保存为Tar文件以实现共享目的

Save和Export功能基本类似,除去saving an image会保存历史记录, 而exporting a container则不会

2.1 容器的export和tar的import操作

yinbodotcc@yay:~$ docker container ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
ac1cb1ec0457        ubuntu:14.04        "/bin/bash"              About an hour ago   Exited (127) About an hour ago                       happy_wescoff
51b54b87ba23        jenkins             "/bin/tini -- /usr/l…"   45 hours ago        Exited (137) 27 hours ago                            jenkins_master
yinbodotcc@yay:~$ docker export ac1cb > update.tar
yinbodotcc@yay:~$ ls
 dockerfile          file.txt                 mysql                                        software     公共的   图片   音乐
 eclipse-workspace   host.txt                 mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz   test         模板     文档   桌面
 examples.desktop    jenkins_home_directory  '${project.basedir}'                          update.tar   视频     下载

yinbodotcc@yay:~$ docker import - newubuntu < update.tar
sha256:0e1f46cd99b8da4bca08fe8ffe6327d5b526197f807c4a77cb6d0ec94e701f24

yinbodotcc@yay:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
newubuntu           latest              0e1f46cd99b8        25 seconds ago      178MB
...

如果需要共享给别人,可以把这个tar给人家,然后用import命令即可把镜像导入到Docker host上

2.2 镜像的Save和tar的Load操作:

yinbodotcc@yay:~$ docker save -o update1.tar newubuntu
yinbodotcc@yay:~$ docker rmi newubuntu
Untagged: newubuntu:latest
Deleted: sha256:0e1f46cd99b8da4bca08fe8ffe6327d5b526197f807c4a77cb6d0ec94e701f24
Deleted: sha256:ee9eb2f69595e72c544107642ab23ec36b98eab64858b388ac08af5a529e65dd
yinbodotcc@yay:~$ docker load < update1.tar
ee9eb2f69595: Loading layer [==================================================>]  187.4MB/187.4MB
Loaded image: newubuntu:latest
yinbodotcc@yay:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
newubuntu           latest              0e1f46cd99b8        8 minutes ago       178MB
ubuntu              update              f87ea47c7b2f        About an hour ago   201MB
wordpress           latest              074afffecb75        8 days ago          421MB
ubuntu              14.04               390582d83ead        10 days ago         188MB
python              2.7                 3be5dc25d0fa        2 weeks ago         914MB
mysql               5.5                 49a1fbbc9c44        2 weeks ago         205MB
jenkins             latest              cd14cecfdb3a        8 months ago        696MB
yinbodotcc@yay:~$ 

3. 编写Dockfile

在Dockerfile中描述构建步骤

新建两个dockerfile,一个叫做Dockerfile,一个叫做Dockerfile2, 这些dockerfile用到了三个指令:FROM , ENTRYPOINT , CMD

yinbodotcc@yay:~/dockerfiles$ ls
Dockerfile  Dockerfile2
yinbodotcc@yay:~/dockerfiles$ cat Dockerfile
FROM ubuntu:14.04

ENTRYPOINT ["/bin/echo"]
yinbodotcc@yay:~/dockerfiles$ cat Dockerfile2
FROM ubuntu:14.04

CMD ["/bin/echo", "my god!"]
yinbodotcc@yay:~/dockerfiles$ 

构建两个镜像:


yinbodotcc@yay:~/dockerfiles$ docker build .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM ubuntu:14.04
 ---> 390582d83ead
Step 2/2 : ENTRYPOINT ["/bin/echo"]
 ---> Running in 8bf82608366b
Removing intermediate container 8bf82608366b
 ---> 0304833b1ae7
Successfully built 0304833b1ae7

yinbodotcc@yay:~/dockerfiles$ docker build -f Dockerfile2 .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM ubuntu:14.04
 ---> 390582d83ead
Step 2/2 : CMD ["/bin/echo", "my god!"]
 ---> Running in effea094a048
Removing intermediate container effea094a048
 ---> 8544ef082c75
Successfully built 8544ef082c75

注意,构建也可以制定repository和tag信息:
图片.png

运行两个容器:

yinbodotcc@yay:~/dockerfiles$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              8544ef082c75        4 seconds ago       188MB
<none>              <none>              0304833b1ae7        15 seconds ago      188MB
ubuntu              14.04               390582d83ead        10 days ago         188MB
python              2.7                 3be5dc25d0fa        2 weeks ago         914MB
mysql               5.5                 49a1fbbc9c44        2 weeks ago         205MB
jenkins             latest              cd14cecfdb3a        8 months ago        696MB
yinbodotcc@yay:~/dockerfiles$ docker run 85 
my god!
yinbodotcc@yay:~/dockerfiles$ docker run 85 /bin/date #这个有趣
Fri Mar 22 14:10:30 UTC 2019

yinbodotcc@yay:~/dockerfiles$ docker run 03 hello world
hello world
yinbodotcc@yay:~/dockerfiles$ 

对上面运行结果的说明:

Remember that CMD can be overwritten by an argument to docker run , while ENTRY POINT can be overwritten only by using the --entrypoint option of docker run .

4. Flake应用程序镜像制作和容器运行示例

1. 编写python程序(~/dockerfiles/hello.py)

from flask import Flask #在镜像中需要使用pip安装Flask,注意pip本身也要安装
app = Flask(__name__)

@app.route('/hi') #http://localhost:端口号/hi
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000) #在5000端口运行
yinbodotcc@yay:~/dockerfiles$ 

2.编写dockerfile文件(~/dockerfiles/FlaskApplicationDockerfile)

FROM ubuntu:14.04

RUN apt-get update
RUN apt-get install -y python
RUN apt-get install -y python-pip
RUN apt-get clean all

RUN pip install flask

ADD hello.py /tmp/hello.py

EXPOSE 5000

CMD ["python","/tmp/hello.py"]

说明:上面的Dockerfile最好优化成下面这样:

FROM ubuntu:14.04
RUN apt-get update && apt-get install -y \
python
python-pip

RUN pip install flask

COPY hello.py /tmp/hello.py

EXPOSE 5000

CMD ["python","/tmp/hello.py"]

或者如下(亲测的确有效):

FROM ubuntu:14.04
FROM python:2.7.10
RUN pip install flask
COPY hello.py /tmp/hello.py
EXPOSE 5000

CMD ["python","/tmp/hello.py"]

3. 制作镜像

yinbodotcc@yay:~/dockerfiles$ docker build -f FlaskApplicationDockerfile -t flask .

4. 启动容器

yinbodotcc@yay:~$ docker run --name myflake -d -P flask
4635f91f3c58993191050eccbbefdf759dc191108aaace2ffdb645e353739b01

另外,上面-P(P是大写)是随机选择一个Host端口映射到容器里面的端口上,我们也可由-p(p小写)指定Host具体端口:

yinbodotcc@yay:~/dockerfiles$ docker run --name myflake -d -p 8888:5000 flask

5. 打开网页

yinbodotcc@yay:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
4635f91f3c58        flask               "python /tmp/hello.py"   6 seconds ago       Up 4 seconds        0.0.0.0:32768->5000/tcp   myflake

注意到上面的0.0.0.0:32768->5000/tcp,也就是说32768是Docker Host映射到的端口,下面要以这个端口打开应用程序界面:

图片.png

6. 如果容器启动的时候使用非deamon方式,即使用-it, ,则CMD命令失效,你需要在容器里面执行python程序以后打开网页才能看到效果:

yinbodotcc@yay:~/dockerfiles$ docker run -it --name myflake2 -P flask /bin/bash
root@8ed148db8a58:/# cd tmp
root@8ed148db8a58:/tmp# dir
hello.py
root@8ed148db8a58:/tmp# python hello.py
 * Serving Flask app "hello" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.17.0.1 - - [23/Mar/2019 03:54:29] "GET /hi HTTP/1.1" 200 -
172.17.0.1 - - [23/Mar/2019 03:54:29] "GET /favicon.ico HTTP/1.1" 404 -

另外启动一个终端,查看映射到的Docker Host端口:

yinbodotcc@yay:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                     NAMES
8ed148db8a58        flask               "/bin/bash"         About a minute ago   Up About a minute   0.0.0.0:32770->5000/tcp   myflake2

然后打开浏览器:
图片.png

5. 把Docker Host上的镜像push到DockerHub上

yinbodotcc@yay:~$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: yinbodotcc
Password: 
Error response from daemon: Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password
yinbodotcc@yay:~$ clear
yinbodotcc@yay:~$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: yinbodotcc
Password: 
WARNING! Your password will be stored unencrypted in /home/yinbodotcc/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
yinbodotcc@yay:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
flask               latest              85fe9cebeaef        9 hours ago         938MB
ubuntu              14.04               390582d83ead        11 days ago         188MB
python              2.7                 3be5dc25d0fa        2 weeks ago         914MB
python              latest              32260605cf7a        2 weeks ago         929MB
mysql               5.5                 49a1fbbc9c44        2 weeks ago         205MB
jenkins             latest              cd14cecfdb3a        8 months ago        696MB
yinbodotcc@yay:~$ docker tag ubuntu:14.04 yinbodotcc/ubuntume
yinbodotcc@yay:~$ docker push yinbodotcc/ubuntume
The push refers to repository [docker.io/yinbodotcc/ubuntume]
5f96fa66dc12: Pushed 
dda5ec330bd9: Mounted from library/ubuntu 
11a0c2f551fd: Mounted from library/ubuntu 
eef560b4ec4f: Mounted from library/ubuntu 
latest: digest: sha256:76f6920c51a2e9da1fccca4b81a84af0ff287449282d6a04c33adeaeeb4df60d size: 1152
yinbodotcc@yay:~$ 

然后你在DockerHub上就能查询到了:
图片.png

在DockerHub上查询镜像

yinbodotcc@yay:~$ docker search jenkins
NAME                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
jenkins                                Official Jenkins Docker image                   4173                [OK]                
jenkins/jenkins                        The leading open source automation server       1370                                    
jenkinsci/jenkins                      Jenkins Continuous Integration and Delivery …   356                                     
jenkinsci/blueocean                    https://jenkins.io/projects/blueocean           348                                     
jenkinsci/jnlp-slave                   A Jenkins slave using JNLP to establish conn…   103                                     [OK]
jenkins/jnlp-slave                     a Jenkins agent (FKA "slave") using JNLP to …   73                                      [OK]
jenkinsci/slave                        Base Jenkins slave docker image                 52                                      [OK]
cloudbees/jenkins-enterprise           CloudBees Jenkins Enterprise (Rolling releas…   32                                      [OK]
openshift/jenkins-2-centos7            A Centos7 based Jenkins v2.x image for use w…   20                                      
jenkins/slave                          base image for a Jenkins Agent, which includ…   19                                      [OK]
h1kkan/jenkins-docker                  🤖 Extended Jenkins docker image, bundled wi…    19                                      
bitnami/jenkins                        Bitnami Docker Image for Jenkins                17                                      [OK]
cloudbees/jenkins-operations-center    CloudBees Jenkins Operation Center (Rolling …   13                                      [OK]
blacklabelops/jenkins                  Docker Jenkins Swarm-Ready with HTTPS and Pl…   13                                      [OK]
fabric8/jenkins-docker                 Fabric8 Jenkins Docker Image                    10                                      [OK]
vfarcic/jenkins-swarm-agent            Jenkins agent based on the Swarm plugin         8                                       [OK]
killercentury/jenkins-slave-dind       Generic Jenkins Slave with Docker Engine and…   8                                       [OK]
openshift/jenkins-slave-base-centos7   A Jenkins slave base image. DEPRECATED: see …   5                                       
publicisworldwide/jenkins-slave        Jenkins Slave based on Oracle Linux             5                                       [OK]
openshift/jenkins-1-centos7            DEPRECATED: A Centos7 based Jenkins v1.x ima…   4                                       
trion/jenkins-docker-client            Jenkins CI server with docker client            3                                       [OK]
mashape/jenkins                        Just a jenkins image with the AWS cli added …   0                                       [OK]
ansibleplaybookbundle/jenkins-apb      An APB which deploys Jenkins CI                 0                                       [OK]
jameseckersall/jenkins                 docker-jenkins (based on openshift jenkins 2…   0                                       [OK]
amazeeio/jenkins-slave                 A jenkins slave that connects to a master vi…   0                                       [OK]
yinbodotcc@yay:~$ 

有篇文字可以参考以下,我还没有看:
https://blog.51cto.com/ganbing/2085769

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,558评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,002评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,036评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,024评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,144评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,255评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,295评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,068评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,478评论 1 305
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,789评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,965评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,649评论 4 336
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,267评论 3 318
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,982评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,223评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,800评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,847评论 2 351