1. 把容器中的变更commit到一个新镜像中
我们可以看看现在的容器相对于原来的镜像都有哪些区别:说明:只要容器没有rm,都可以提交,不需要是start状态。
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信息:
运行两个容器:
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映射到的端口,下面要以这个端口打开应用程序界面:
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
然后打开浏览器: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上就能查询到了:在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