Docker

Posted 罗穆瑞,卡子火,crabluo,luomurui,kazih

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker相关的知识,希望对你有一定的参考价值。

目录

1.1????容器引擎核心技术????1

1.2????特性????1

1.3????说明????1

1.4????部署????2

1.5????操作????2

1.6????Docker Image制作????5

1.7????Docker Image发布????7

1.8????容器端口映射????8

?

  1. 容器引擎核心技术

    Namespace:实现Container的进程、网络、消息、文件系统和主机名的隔离;

    Cgroup:实现对资源的配额和度量;

  2. 特性

    文件系统隔离:每个进程容器运行在一个完全独立的根文件系统里;

    资源隔离:系统资源根据需求分配到不同的容器中;

    网络隔离:么米格容器运行在自己的网络空间,虚拟接口和IP地址;

    日志记录:Docker会 手机并记录每个进程容器的标准流(stdout/stderr/stdin),用于实时检索或批量检索;

    变更管理:容器文件系统的变更可提交到新的镜像中,并可重复使用以创建更多的容器,无需使用模板或手动配置;

    交互式Shell:Docker可分配一个虚拟终端并关联到任何容器的标准输入上。

  3. 说明

    Docker镜像及数据存放于/var/lib/docker目录

    [[email protected] ~]# du -sh /var/lib/docker/*

    24K????/var/lib/docker/containers

    406M????/var/lib/docker/devicemapper

    664K????/var/lib/docker/image

    44K????/var/lib/docker/network

    0????/var/lib/docker/swarm

    0????/var/lib/docker/tmp

    0????/var/lib/docker/trust

    24K????/var/lib/docker/volumes

  4. 部署

    [[email protected] ~]# mv local.repo local.repo.bak

    [[email protected] ~]# wget -O /etc/yum.repos.d/Centos_Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

    [[email protected] ~]# yum -y install docker-io

    [[email protected] ~]# systemctl start docker

    [[email protected] ~]# docker search centos

    #从Docker Hub中搜索符合条件的镜像

    [[email protected] ~]# docker pull centos

    #从Docker Hub中拉取或更新指定镜像

    or

    本地导入指定image

    [[email protected] ~]# docker load -i centos-latest-docker-image.tar

    # -i:指定载入的镜像归档

  5. 操作

    查看所有本地镜像:

    [[email protected] ~]# docker images

    ?

    运行容器:

    [[email protected] ~]# cat /etc/redhat-release

    CentOS Linux release 7.2.1511 (Core)

    [[email protected] ~]# docker run -it --name=test-centos docker.io/centos:latest /bin/bash

    #-i:以交互模式运行容器;-t:为容器重新分配一个伪输入终端;-d 表示守护模式, 容器在后台运行;--name 为容器命名;docker.io/centos:latest 格式为镜像名:版本;/bin/bash 进入bash命令行

    [[email protected] /]# cat /etc/redhat-release

    CentOS Linux release 7.4.1708 (Core)

    ?

    长久运行:

    [[email protected] ~]# JOB=$(docker run -d centos /bin/bash -c "while true;do echo hello world;sleep 1;done")

    # -d:后台运行容器,并返回容器ID;-c:待完成

    [[email protected] ~]# echo $JOB

    64f37b2fb689076e9b3c169ab9ab4505448c394388c312643a6d54f05fda3756

    ?

    容器日志读取:

    [[email protected] ~]# docker logs $JOB

    hello world

    ···

    hello world

    hello world

    ?

    查看运行中容器:

    [[email protected] ~]# docker ps

    ?

    查看所有容器:

    [[email protected] ~]# docker ps -a

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

    64f37b2fb689 centos "/bin/sh -c ‘while tr" 13 minutes ago Up 13 minutes desperate_albattani

    a94e89783f11 centos "/bin/bash" 4 hours ago Exited (137) 18 minutes ago elegant_kare

    12fe2d95c2a2 centos "/bin/bash" 4 hours ago Exited (127) 4 hours ago sleepy_banach

    ?

    杀死容器:

    [[email protected] ~]# docker kill 64f37b2fb689

    ?

    启动、停止、重启:

    [[email protected] ~]# docker start 64f37b2fb689

    [[email protected] ~]# docker stop 64f37b2fb689

    [[email protected] ~]# docker restart 64f37b2fb689

    ?

    删除容器:

    [[email protected] ~]# docker rm 64f37b2fb689

    注:正在运行的容器在删除前先将容器关闭,或者加-f强制删除

    ?

    查看容器网络:

    [[email protected] ~]# ifconfig

    docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

    inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0

    inet6 fe80::42:8fff:fed0:be33 prefixlen 64 scopeid 0x20<link>

    ether 02:42:8f:d0:be:33 txqueuelen 0 (Ethernet)

    RX packets 28641 bytes 1172564 (1.1 MiB)

    RX errors 0 dropped 0 overruns 0 frame 0

    TX packets 30689 bytes 105377292 (100.4 MiB)

    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    ?

    配置容器root密码:

    [[email protected] ~]# echo 000000|passwd --stdin root

    注:默认无密码

  6. Docker Image制作

    法一:

    docker commit

    #保存container的当前状态到image后,生成对应的image

    法二:

    docker build

    #使用Dockerfile自动化制作image

    ?

    法一实操:

    [[email protected] ~]# docker run -ti centos /bin/bash

    [[email protected] ~]# yum -y install net-tools

    [[email protected] ~]# exit

    [[email protected] ~]# docker images

    REPOSITORY TAG IMAGE ID CREATED SIZE

    docker.io/centos latest 3fa822599e10 5 weeks ago 203.5 MB

    [[email protected] ~]# docker ps -a

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

    c4e377bf5c01 centos "/bin/bash" 18 minutes ago Exited (0) About a minute ago reverent_kare

    [[email protected] ~]# docker commit c4e377bf5c01 centos:net-tool

    sha256:c45c0a7b6a1a2d0db6c94d5d932b741f408507b86da50ab41280421258d86e68

    [[email protected] ~]# docker images

    REPOSITORY TAG IMAGE ID CREATED SIZE

    centos net-tool c45c0a7b6a1a 15 seconds ago 350.3 MB

    docker.io/centos latest 3fa822599e10 5 weeks ago 203.5 MB

    [[email protected] ~]# docker run -ti centos:net-tool /bin/bash

    [[email protected] /]# rpm -qa net-tools

    net-tools-2.0-0.22.20131004git.el7.x86_64

    ?

    法二实操:

    [[email protected] ~]# mkdir /docker-build

    [[email protected] ~]# cd /docker-build/

    [[email protected] /docker-build]# cat Dockerfile

    FROM centos

    MAINTAINER kazihuo <[email protected]>

    RUN yum -y install httpd

    ADD start.sh /usr/local/bin/start.sh

    ADD index.html /var/www/html/index.html

    ?

    注释:

    FROM #基于指定镜像

    MAINTANER #镜像创建者

    RUN #安装软件

    ADD #将文件<src>拷贝到container的文件系统对应的路径<dest>,所有拷贝到container中的文件和文件夹权限是07555,uid和gid是0

    ?

    [[email protected] /docker-build]# echo "/usr/sbin/httpd -DFOREGROUND"> start.sh

    注:以上相当于执行systemctl start httpd

    [[email protected] /docker-build]# chmod a+x start.sh

    [[email protected] /docker-build]# echo "docker image build test" > index.html

    ?

    build创建新的image语法:

    # docker build -t 父镜像名:自己定义的镜像名 Dockerfile文件所在路径

    [[email protected] /docker-build]# docker bulid -t centos:httpd .

    [[email protected] /docker-build]# docker images

    REPOSITORY TAG IMAGE ID CREATED SIZE

    centos httpd 40aba19c5ebb 20 seconds ago 327 MB

    centos net-tool c45c0a7b6a1a 35 minutes ago 350.3 MB

    docker.io/centos latest 3fa822599e10 5 weeks ago 203.5 MB

  7. Docker Image发布

    法一:

    Save Image To TarBall

    法二:

    Push Image To Docker Hub

    ?

    法一实操:

    保存image到tar包

    语法:docker save -o 导出的镜像名.tar 本地镜像名

    [[email protected] /docker-build]# docker save -o centos-httpd-docker-image.tar centos:httpd

    [[email protected] /docker-build]# ls

    centos-httpd-docker-image.tar Dockerfile index.html start.sh

  8. 容器端口映射

    使用centos:httpd镜像启动一个容器,将容器中的80端口映射到docker物理上的9000端口

    [[email protected] ~]# docker run -d -p 9000:80 centos:httpd /bin/bash -c /usr/local/bin/start.sh

    01b16f44a3f96814c5368e417c07ef48fad2fe7013ff78718293ff012e97421b

    [[email protected] ~]# docker ps

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

    01b16f44a3f9 centos:httpd "/bin/bash -c /usr/lo" 23 seconds ago Up 22 seconds 0.0.0.0:9000->80/tcp elegant_hopper

    [[email protected] ~]# curl http://127.0.0.1:9000

    docker image build test

    [[email protected] ~]# docker exec -ti 01b16f44a3f9 /bin/bash

    [[email protected] ~]# echo kazihuo > /var/www/html/test.html

    [[email protected] ~]# curl http://127.0.0.1:9000/test.html

    kazihuo

以上是关于Docker的主要内容,如果未能解决你的问题,请参考以下文章

DockerDocker常用命令

DockerDocker配置

dockerdocker镜像和镜像发布方法

dockerdocker

dockerdocker安装rabbitmq

dockerdocker下安装mysql