docker入门(创建一个简单的tornado项目)

基础条件: ubuntu14.04, docker for ubuntu14.04安装成功

  1. 在本地创建一个简单的tornado项目

    在/Public/testApp创建app.py

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

 application = tornado.web.Application([

     (r"/", MainHandler),

])

if __name__ == "__main__":

    application.listen(8888)

    tornado.ioloop.IOLoop.instance().start()

2.继续创建我们要使用的文件, requirements.txt

    tornado

3.创建一个sources.list用来代替ubuntu原有的数据源

deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse

deb-src http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse

3.创建Dockerfile文件构建一个镜像, 我们以ubuntu14.04作为基础镜像, 我们使用网易蜂巢的docker镜像仓库, https://c.163.com/hub#/m/repository/?repoId=3186, 首先拷贝14.04-tools的Dockerfile然后做一些修改


FROM hub.c.163.com/library/ubuntu:14.04
MAINTAINER lpj24

WORKDIR /myapp    #工作目录

COPY ./app.py /myapp/        #拷贝项目文件到/myapp目录下
COPY ./Dockerfile /myapp/

COPY ./requirements.txt /myapp/
RUN mkdir -p /var/run/sshd

COPY sources.list /etc/apt/
RUN apt-get update \
    && apt-get update && apt-get install -y openssh-server vim tar wget curl rsync bzip2 iptables tcpdump less telnet net-tools lsof sysstat cron supervisor python2.7 python-pip\ 
    && rm -rf /var/lib/apt/lists/*
RUN sed -i s/"PermitRootLogin without-password"/"PermitRootLogin yes"/g /etc/ssh/sshd_config
RUN cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 22 8888
COPY sshd.conf /etc/supervisor/conf.d/sshd.conf
COPY myapp.conf /etc/supervisor/conf.d/myapp.conf   #supervisor的项目管理

RUN pip install -r requirements.txt
CMD ["/usr/bin/supervisord"]

4.创建myapp.conf, 从网易蜂巢拷贝sshd.conf到/Public/testApp/下

myapp.conf

[program:myapp]
command=python app.py
directory=/myapp
autostart=true
autorestart=true

sshd.conf

[supervisord]
nodaemon=true
[program:sshd]
command=/usr/sbin/sshd -D

5.有了需要的文件之后开始构建一个名字为pythonapp的镜像

 docker build -t pythonapp:v1 .
 docker images

可以看到一个名字为pythonapp的镜像

  1. 运行一个名字为appcontainer的容器, 使用宿主机4000与8888映射
docker run -d --name appcontainer1 -p 4000:8888 pythonapp:v1
#进入容器查看app进程是否已经启动
docker exec -it appcontainer /bin/bash
supervisorctl
#查看是否有myapp的进程 或者直接在宿主机浏览器127.0.0.1:4000访问是否可以浏览

你可能感兴趣的:(docker入门(创建一个简单的tornado项目))