Creating the Dockerfile to Automatically Build the Image:
1.启动一个容器:
docker run -i -t -p 7000:80 centos7:cg /bin/bash
注释:centos7:cg这个镜像是在busybox +sshd+systemd+epel还加了监控工具sar+netstat等的镜像
在镜像中的操作:
41 yum update
42 yum -y install tar git curl nano wget dialog net-tools 43 yum -y install build-essential 44 yum install -y python python-dev python-distribute python-pip 45 yum install -y python python-dev python-distribute python-pip 46 yum install -y python python-dev* 47 yum install -y python-pip* 48 yum provides python-pip 49 yum provides pip 50 yum provides pip 51 yum search pip 52 pip 53 yum -y install pip 54 yum -y install pip* 55 yum -y --enablerepo=epel install python-pip 56 pip install flask 57 pip install --upgrade pip 58 pip install flask 59 mkdir my_application 60 cd my_application 61 vi app.pycat app.py
from flask import Flaskapp = Flask(__name__)@app.route("/")def hello(): return "Hello World!"if __name__ == "__main__": app.run()62 vi requirements.txt 63 pip install cherrypy 64 vi server.py
# Import your application as:# from app import application# Example:from app import app# Import CherryPyimport cherrypyif __name__ == '__main__': # Mount the application cherrypy.tree.graft(app, "/") # Unsubscribe the default server cherrypy.server.unsubscribe() # Instantiate a new server object server = cherrypy._cpserver.Server() # Configure the server object server.socket_host = "0.0.0.0" server.socket_port = 80 server.thread_pool = 30 # For SSL Support # server.ssl_module = 'pyopenssl' # server.ssl_certificate = 'ssl/certificate.crt' # server.ssl_private_key = 'ssl/private.key' # server.ssl_certificate_chain = 'ssl/bundle.crt' # Subscribe this server server.subscribe() # Start the server engine (Option 1 *and* 2) cherrypy.engine.start() cherrypy.engine.block()
65 mkdir app
66 python server.py 67 pwd 68 history最后在容器中的结构:
/my_application | |- requirements.txt # File containing list of dependencies |- /app # Application module (which should have your app) |- app.py # WSGI file containing the "app" callable |- server.py # Optional: To run the app servers (CherryPy)
2.现在开始创建Dockerfile:
[root@node ~]# cat Dockerfile
############################################################# Dockerfile to build Python WSGI Application Containers# Based on jt############################################################# Set the base image to Ubuntu
FROM centos7:cg# File Author / Maintainer
MAINTAINER Maintaner Name# Add the application resources URL
#RUN echo "deb http://archive.ubuntu.com/ubuntu/ $(lsb_release -sc) main universe" >> /etc/apt/sources.list# Update the sources list
RUN yum update# Install basic applications
RUN yum install --enablerepo=epel -y tar git curl nano wget dialog net-tools# Install Python and Basic Python Tools
RUN yum install --enablerepo=epel -y python python-dev python-distribute python-pip#genxinRUN pip install --upgrade pip# Copy the application folder inside the containerADD /my_application /my_application# Get pip to download and install requirements:
RUN pip install -r /my_application/requirements.txt# Expose ports
EXPOSE 80# Set the default directory where CMD will execute
WORKDIR /my_application# Set the default command to execute
# when creating a new container# i.e. using CherryPy to serve the applicationCMD python server.py &[root@node ~]#注意这里:ADD /my_application /my_application
我们可以换成:
RUN git clone [application repository URL] 至此我们用dockefile成功构建了一个镜像并访问成功 docker build -t my_application_img .
[root@node ~]# docker run -p 7001:80 -i -t --name jt my_application_img
[02/Jun/2017:20:05:49] ENGINE Bus STARTING[02/Jun/2017:20:05:49] ENGINE Started monitor thread 'Autoreloader'.[02/Jun/2017:20:05:49] ENGINE Started monitor thread '_TimeoutMonitor'.[02/Jun/2017:20:05:49] ENGINE Serving on http://0.0.0.0[02/Jun/2017:20:05:49] ENGINE Bus STARTED
好了,成功用dockerfile发布了一个小代码
私有仓库搭建:
mkdir -p /opt/data/registry
因为是centos7所以都要在所有节点上修改:
vi /usr/lib/systemd/system/docker.serviceExecStart=/usr/bin/dockerd --insecure-registry 192.168.36.151:5000
systemctl daemon-reload
systemctl restart docker
启动一个注册容器:
docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry
为上传镜像打个标签:
docker tag busybox 192.168.36.151:5000/test
另外一个机器上面测试成功:
[root@node ~]# docker pull 192.168.36.151:5000/test
Using default tag: latestTrying to pull repository 192.168.36.151:5000/test ... latest: Pulling from 192.168.36.151:5000/testDigest: sha256:32a0e4c673002bd7d4585587fb06ae9826cda9808317561b790ebef374bbb312