1,从仓库中获取到以下原始镜像
django 1.9.8-python2 5237faea5e4b 2 weeks ago 441.3 MB
2,检查当前运行的容器. 目前么有运行
zy@BDC1:~/software/docker_app/pstockAuto$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3,运行原始镜像,进入交互式操作系统,并完成基础环境的初始化
说明:从头写dockerfile,我认为在通用基础环境不具备的条件下,还是比较麻烦的.我的想法是,先手工完成基础环境配置,然后以此为基础,生成一个我的基础镜像.后续的开发都时基于此镜像,并用dockerfile完成代码的构建.这样镜像的层数不会随着版本的发布而增加.
3.1进入容器系统:
zy@BDC1:~/software/docker_app/pstockAuto$ docker run -i -t --name myd198p2 django:1.9.8-python2 /bin/bash root@a94dd8be87da:/#
3.2 测试网络
root@a94dd8be87da:/# ping www.baidu.com PING www.a.shifen.com (14.215.177.38): 56 data bytes 64 bytes from 14.215.177.38: icmp_seq=0 ttl=127 time=43.962 ms
3.3 创建应用用户
root@a94dd8be87da:/# useradd -d "/home/app" -m -s "/bin/bash" app root@a94dd8be87da:/# passwd app Enter new UNIX password: app123 Retype new UNIX password: passwd: password updated successfully root@a94dd8be87da:/# id app uid=1000(app) gid=1000(app) groups=1000(app) root@a94dd8be87da:/# su - app app@a94dd8be87da:~$ pwd /home/app
3.4 设置源,我的镜像时ubuntu的
root@a94dd8be87da:/# uname -a Linux a94dd8be87da 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 GNU/Linux root@a94dd8be87da:/# cat /etc/apt/sources.list deb http://httpredir.debian.org/debian jessie main deb http://httpredir.debian.org/debian jessie-updates main deb http://security.debian.org jessie/updates main 是国外的源,试了一下update成功,现在vim都没有,所以没法编辑,先update root@a94dd8be87da:/# apt-get update root@a94dd8be87da:/# apt-get install vim 我本机的源,复制粘贴进去. zy@BDC1:~/software/docker_app/pstockAuto$ cat /etc/apt/sources.list | grep -v ^# deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted deb http://mirrors.aliyun.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse root@a94dd8be87da:/# vim /etc/apt/sources.list 顺手修改一个root密码,以便将来从app还能切过来. root@a94dd8be87da:/# passwd root Enter new UNIX password: root Retype new UNIX password: passwd: password updated successfully 再update一下 root@a94dd8be87da:~# apt-get update root@a94dd8be87da:~# apt-get install cron
3.5 接下来就正常了,随意配置.最重要的,检查python开发环境
app@a94dd8be87da:~$ which pip /usr/local/bin/pip app@a94dd8be87da:~$ which python /usr/local/bin/python app@a94dd8be87da:~$ python Python 2.7.12 (default, Jul 19 2016, 20:08:29) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import django >>> django.VERSION (1, 9, 8, 'final', 0) >>> 别忘了安装python三大神器. app@a94dd8be87da:~$ pip install virtualenv virtualenvwrapper 报错了,切回root重新执行安装成功.在本地虚拟环境开发,然后部署到容器,最后发布上线 #配置时区. app@735497f41f71:~$ vim .bashrc TZ='Asia/Shanghai'; export TZ
3.6准备python环境项目的pip依赖包
zy@BDC1:~/software/docker_app/pstockAuto$ workon pstockAuto (pstockAuto) zy@BDC1:~/software/docker_app/pstockAuto$ pip freeze > requirements.txt #在镜像容器中安装依赖包 root@a94dd8be87da:/home/app# pip install -r requirements.txt 可能会安装失败,慢慢装吧.最痛苦的是lxml的安装,谷歌的了好多方法都尝试了还是不行,后来update了gcc编译器就好了. 这也是不用dockerfile搞基础环境的原因,情况比较复杂.
3.7 将容器保存为我新的基础镜像.
zy@BDC1:~/software/docker_app/pstockAuto$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a94dd8be87da django:1.9.8-python2 "/bin/bash" 49 minutes ago Up 49 minutes myd198p2 zy@BDC1:~/software/docker_app/pstockAuto$ docker commit a94dd8be87da peter/d198_p2:base sha256:69f120bb4c93a2a6f88f89873b1eebb73a4d089d94bae04d46ce76304be607b6 zy@BDC1:~/software/docker_app/pstockAuto$ docker p_w_picpaths REPOSITORY TAG IMAGE ID CREATED SIZE peter/d198_p2 latest 69f120bb4c93 5 seconds ago 492.6 MB
4.开发环境部署,应用目录设置,代码发布设置
我的基本镜像就构建号了,但每次发步代码,不应该产生新的镜像层.
我采用dockerfile在这个基础镜像上进行构建.
4.1 代码构建,应用部署.在base上构建一层.每次都从base构建.
(pstockAuto) zy@BDC1:~/software/docker_app/pstockAuto_docker$ cat Dockerfile cat Dockerfile FROM peter/d198_p2:base MAINTAINER peter <peterz3g@163.com> WORKDIR /home/app #ADD requirements.txt requirements.txt #RUN pip install -r requirements.txt # ADD app_cronts.txt /var/spool/cron/crontabs/app ADD entrypoint.sh entrypoint.sh RUN chown -R app:crontab /var/spool/cron/crontabs/app \ && chmod 600 /var/spool/cron/crontabs/app \ && touch /var/log/cron.log \ # 用完包管理器后安排打扫卫生可以显著的减少镜像大小 && mkdir -p /home/app/pstockAuto \ && apt-get clean \ && apt-get autoclean \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \ && chmod 777 ./entrypoint.sh ######################################################## # this is my pro dir ADD ./pstockAuto /home/app/pstockAuto RUN chown -R app:app /home/app #USER app ENTRYPOINT ["./entrypoint.sh"] #CMD cron #COPY docker-entrypoint.sh /usr/local/bin/ #EXPOSE 8888 #ENTRYPOINT python manage.py runserver 0.0.0.0:8888 其余文件如下: requirements.txt pip依赖 rm_cont.sh 删除容器脚本 build.sh 从基础镜像构建含代码的镜像 run_cont.sh 运行代码镜像容器 pstockAuto 代码目录,直接在这里写代码 Dockerfile 基础镜像构建代码镜像的描述 entrypoint.sh 运行代码容器的入口脚本 app_cronts.txt 运行代码容器的crontab
构建最新代码的容器dockerfile方式
cat push_2_dao.sh 云上无法自动启动crontab,手工启动即可. /etc/init.d/cron start
或者继续交互式创建:
#update code docker run -u app --name djs2 -p 8888:8888 -d peter/d198_p2:base docker cp /home/zy/software/pstockAuto djs2:/home/app #commit p_w_picpath docker commit -a "peter" -m "init env" djs2 peter/pautostock:v1 #publish to dao docker tag peter/pautostock:v1 daocloud.io/peterz3g/pautostock:v1 docker push daocloud.io/peterz3g/pautostock:v1 #clearn container docker stop djs2 docker rm djs2
转载于:https://blog.51cto.com/peterz3g/1835452