Docker 初级实践

时间:2023-03-09 06:56:32
Docker 初级实践

Docker

  • 应用

  • 优势

    与虚拟相比Docker更加轻量高效,更加方便移植。虚拟机提供的是完整的操作系统环境,包含了大量类似硬件驱动、虚拟处理器、网络接口等等并不需要的信息,也需要比较长时间的启动,同时也会消耗大量的内存、CPU资源,迁移的时候太大了。

  • 历史

    互联网文章 :Docker 集装箱式"运输"在软件上的实现

centos 6.5上 安装Docker

rpm -ivh http://ftp.sjtu.edu.cn/fedora/epel/6/i386/epel-release-6-8.noarch.rpm
yum install docker-io -y
service docker start
chkconfig docker on
#获取最新的centos镜像
docker pull centos:latest
#查看可用镜像
docker images centos
#测试镜像
docker run -i -t centos /bin/bash
  • 关于EPEL

    当 Fedora 项目的维护人员发现可以采用管理 Fedora 项目的方法,来管理针对企业版 Linux 的附加软件包项目时,一个新的伟大的项目诞生了!项目诞生之初只是加入了一些在 RHEL 维护 Fedora 的工具。随着时间的发展,EPEL 仓库越来越丰富,成为一个大型的软件收集仓库。

    关注:https://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F

docker run

#外部调用容器命令
docker run centos /bin/echo 'Hello world'
#容器交互(t分配伪终端i指定容器)
docker run -t -i centos /bin/bash
#退出容器交互终端
bash-4.2# exit
#后台执行-d(输出container ID)
docker run -d centos /bin/sh -c "while true; do echo hello world; sleep 1; done"
894e36b70a671e67252c0034b8eda47059f20dea0edf49700737d6311851a504
#查看容器守护进程(drunk_feynman容器自动分配的标记)
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
894e36b70a67 centos:latest /bin/sh -c 'while tr 2 minutes ago Up 2 minutes drunk_feynman
#查看容器标准输出
docker logs drunk_feynman
#停止容器程序(返回对应的name表示停止成功)
docker stop drunk_feynman
#查看所有容器历史
docker ps -a
#恢复指定容器的最后状态
docker start 942ca54d1f94
#进去一个后台运行的容器
docker attach 942ca54d1f94

Docker Images

获取镜像

#查看本地image
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos latest 1a7dc42f78ba 43 hours ago 236.4 MB
#搜索镜像
docker search centos
#从hub获取cetnos镜像
docker pull centos

创建自己的镜像

  • 在容器里面一旦退出,所有的操作都不被保留
    [root@localhost ~]# docker run -t -i centos /bin/bash
    bash-4.2# touch qiyue
    bash-4.2# exit
    exit
    [root@localhost ~]# docker run -t -i centos /bin/bash
    bash-4.2# ls qiyue
    ls: cannot access qiyue: No such file or directory
  • 通过commit来提交image
    [root@localhost ~]# docker run -t -i centos /bin/bash
    bash-4.2# touch qiyue
    #这里不退出交互终端,另外开一个窗口
    [root@localhost ~]# docker imagesdocker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    6e98177d50f9 centos:latest /bin/bash 15 seconds ago Up 15 seconds cranky_euclid
    #-m 类似git的标记信息, -a 作者 6e98177d50f9为CONTAINER ID,centos:test_1为新的镜像名字
    [root@localhost ~]# docker commit -m "add qiyue" -a "wenhao" 6e98177d50f9 centos:test_1
    9d9d923e47f5189759174481f6543acf08959a1b93993a9246a6652c03603268
    [root@localhost ~]# docker images
    REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
    centos test_1 9d9d923e47f5 18 minutes ago 236.4 MB
    centos latest 1a7dc42f78ba 42 hours ago 236.4 MB
    [root@localhost ~]# docker run -t -i centos:test_1 /bin/bash
    bash-4.2# ls qiyue
    qiyue
  • 通过 Dockerfile 创建镜像
    #创建一个目录,并在目录写 touch Dockerfile
    mkdir /vagrant/wenhao
    cd /vagrant/wenhao/
    touch Dockerfile
    #Dockerfile 配置信息
    FROM centos:latest
    MAINTAINER caiwenhao
    RUN yum install nginx -y
    RUN yum install mysql -y
    #这里必须包含. 表示当前目录下的Dockerfile
    docker build -t="centos:test_2" .
    [root@localhost wenhao]# docker images
    REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
    centos test_2 156d947ce048 22 seconds ago 375.1 MB
    centos test_1 9d9d923e47f5 43 minutes ago 236.4 MB
    centos latest 1a7dc42f78ba 42 hours ago 236.4 MB
    [root@localhost wenhao]# docker run -t -i centos:test_2 /bin/bash
    bash-4.2# mysql -V
    mysql Ver 15.1 Distrib 5.5.37-MariaDB, for Linux (x86_64) using readline 5.1

设置 image tags

[root@localhost wenhao]# docker tag 156d947ce048 centos:test_3
[root@localhost wenhao]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos test_2 156d947ce048 6 minutes ago 375.1 MB
centos test_3 156d947ce048 6 minutes ago 375.1 MB
centos test_1 9d9d923e47f5 49 minutes ago 236.4 MB
centos latest 1a7dc42f78ba 42 hours ago 236.4 MB

删除 image

[root@localhost wenhao]# docker rmi -f centos:test_1
Untagged: centos:test_1
Deleted: 9d9d923e47f5189759174481f6543acf08959a1b93993a9246a6652c03603268
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos test_2 156d947ce048 58 minutes ago 375.1 MB
centos latest 1a7dc42f78ba 43 hours ago 236.4 MB

提交 image到Docker Hub

[root@localhost ~]# docker login
Username: caiwenhao
Password:
Email: caiwenhao186@live.com
[root@localhost ~]# docker push centos:test_2

在Docker中运行python web

初始化python环境

docker run -t -i centos /bin/bash
yum install wget
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
easy_install tornado
mkdir /data
vim /data/web.py

web.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()

创建容器并后台运行

docker commit -m "add python" -a "wenhao" 2b5ae98645cd centos:python
docker run -d -p 3333:8888 centos:python python /data/web.py
curl http://127.0.0.1:3333

Docker Hub