• docker容器安装与操作


     
    Docker与Kvm的对比
     
    kvm:
    1. 虚拟机
    2. 使用复杂
    3. 启动过程相对慢(分钟)
    4. 模版文件较大
    5. 和物理完全隔离
    6. 模拟的是一个完整系统,可以登录并实现管理
     
    docker:
    1. 直接虚拟出来一个用户空间
    2. 使用简单
    3. 启动非常块(秒级)
    4. 模版文件很小
    5. 在一定程度上和物理机隔离
    6. 仅仅是模拟一部分用户空间,不方便管理
     
    docker部署安装
    第一步:需要下载阿里云较新版的docker源文件
    [root@zxw99 ~]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

    需要的源 
    [extra]
    name=centos extra
    enabled=1
    gpgcheck=0
    baseurl=https://mirrors.aliyun.com/centos/7/extras/x86_64/
    [epel]
    name=epel
    enabled=1
    gpgcheck=0
    baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/

    [docker]
    name = dock
    enabled = 1
    gpgcheck = 0
    baseurl = https://mirrors.aliyun.com/centos/7/os/x86_64/


    本地源

    第二步:移动文件到yum.repos.d下
    [root@zxw99 ~]#mv docker-ce.repo /etc/yum.repos.d/
     
    第三步:下载docker-ce
    docker-ce 社区免费版
    docker-ee 企业收费版
    [root@zxw99 ~]# yum install docker-ce -y
     
    第四步:直接启动docker并设置开机自启
    [root@zxw99 ~]# systemctl restart docker
    [root@zxw99 ~]## systemctl enable docker
     
     
    注:
    如果不启动docker服务会报如下的错误:
    [root@zxw99 ~]# docker image ls
    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
     
    第五步:验证docker服务是否可以正常运行 
    [root@zxw99 ~]# docker image ls
    REPOSITORY TAG IMAGE ID CREATED SIZE
    busybox 1.31.0-glibc 32b9e6a4f1f1 3 weeks ago 5.2MB
    busybox latest db8ee88ad75f 3 weeks ago 1.22MB

    镜像加速
     
    为什么镜像加速?
    因为docker在拉取镜像的时候是默认使用dockerhub上面的镜像即国外镜像,所以拉取速度是很慢的,
    为了解决这个问题,我们就可以使用阿里云镜像加速
     
     
    阿里云镜像加速
     
    第一步:浏览器输入阿里云进入官网并登陆

     
    第二步:进入控制台–》产品与服务–》容器镜像服务–》镜像加速器
     
    第三步:创建一个脚本写入如下内容

     
     
    docker镜像操作详解
     
    docker镜像操作相关的指令
     
    docker使用技巧:
    docker –help  #可以查看docker所有使用方法
    docker image –help   #可以查看docker镜像操作的所有方法
    docker image ls –help  #可以查看docker镜像操作中ls指令的用法
     
    镜像操作指令:
    build  从dockerfiler中创建镜像
    history 可以显示镜像的构建历史 #docker镜像分层构建,每一层都有一个指令
    import 从一个压缩包创建镜像
    save 创建镜像压缩包
    load 从压缩包导入镜像
    ls 列出来镜像
    prune 清除不使用的镜像
    pull 拉取镜像
    push 推送镜像
    rm 删除镜像
    tag 改名
     
     
    docker镜像操作演示
     
    root@zxw99 ~]# docker image history busybox --no-trunc
    IMAGE CREATED CREATED BY SIZE COMMENT
    sha256:db8ee88ad75f6bdc74663f4992a185e2722fa29573abcc1a19186cc5ec09dceb 3 weeks ago /bin/sh -c #(nop) CMD ["sh"] 0B
    <missing> 3 weeks ago /bin/sh -c #(nop) ADD file:9ceca008111a4ddff7a68f2c3b645ff51fd6d70ef79b0a60cd0b006aa7033698 in / 1.22MB
    [root@zxw99 ~]# docker image history busybox
    IMAGE CREATED CREATED BY SIZE COMMENT
    db8ee88ad75f 3 weeks ago /bin/sh -c #(nop) CMD ["sh"] 0B
    <missing> 3 weeks ago /bin/sh -c #(nop) ADD file:9ceca008111a4ddff… 1.22MB


    例子1:拉取镜像pull
    [root@zxw99 ~]# docker image pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    f5d23c7fed46: Pull complete
     
    格式:docker image pull REPOSITORY:TAG
    镜像拉去官方
    https://hub.docker.com/
    注意:
    如果在拉取镜像的时候只指定了REPOSITORY的话,默认拉取的是tag为latest的版本
     
    例子2:查看当前有哪些镜像 ls
    [root@zxw99 ~]# docker image ls
    REPOSITORY TAG IMAGE ID CREATED SIZE
    nginx latest e445ab08b2be 2 weeks ago 126MB
    busybox 1.31.0-glibc 32b9e6a4f1f1 3 weeks ago 5.2MB
    busybox latest db8ee88ad75f 3 weeks ago 1.22MB


    例子3:查看镜像构建历史
    [root@zxw99 ~]# docker image history busybox:1.31.0-glibc
    IMAGE CREATED CREATED BY SIZE COMMENT
    32b9e6a4f1f1 3 weeks ago /bin/sh -c #(nop) CMD ["sh"] 0B
    <missing> 3 weeks ago /bin/sh -c #(nop) ADD file:aadf874455828a173… 5.2MB
     
    例子4:制作镜像压缩包
    方法一:使用-o选项,output
     
    [root@zxw99 ~]# docker image save busybox:1.31.0-glibc -o busybox1.31.0-glibc.tar
    [root@zxw99 ~]# ls
    anaconda-ks.cfg busybox1.31.0-glibc.tar docker.sh

    方法二:使用标准输出>

    [root@zxw99 ~]# docker image save busybox:1.31.0-glibc > busybox1.31.tar
    [root@zxw99 ~]# ls
    anaconda-ks.cfg busybox1.31.tar docker.sh

    例子5:使用镜像压缩包
     
    方法一:使用-i,input
     
    [root@zxw99 ~]# docker image load -i busybox1.31.tar
    fdfb6e9b8538: Loading layer 5.424MB/5.424MB
    Loaded image: busybox:1.31.0-glibc


    方法二:<
     [root@zxw99 ~]# docker image load < busybox1.31.tar
    例子6:删除镜像
    [root@zxw99 ~]# docker image rm busybox:1.31.0-glibc
    Untagged: busybox:1.31.0-glibc
    [root@zxw99 ~]# docker image ls
    REPOSITORY TAG IMAGE ID CREATED SIZE
    nginx latest e445ab08b2be 2 weeks ago 126MB
    busybox v1 32b9e6a4f1f1 3 weeks ago 5.2MB
    busybox latest db8ee88ad75f 3 weeks ago 1.22MB 


    例子7:镜像改名tag
    [root@zxw99 ~]# docker image tag busybox:1.31.0-glibc busybox:v1
    [root@zxw99 ~]# docker image ls
    REPOSITORY TAG IMAGE ID CREATED SIZE
    nginx latest e445ab08b2be 2 weeks ago 126MB
    busybox 1.31.0-glibc 32b9e6a4f1f1 3 weeks ago 5.2MB
    busybox v1 32b9e6a4f1f1 3 weeks

     

    例子8:清除不经常使用的镜像
    [root@zxw99 ~]# docker image prune -f
    Total reclaimed space: 0B
     
     
    docker容器操作详解
     
    run 运行容器
    commit 使用当前运行的容器制作镜像
    exec 可以在容器中执行命令或者进入容器
    inspect 查看容器的详细信息,也可以查看镜像的信息
    kill 强制杀掉容器
    logs 查看容器产生的日志信息
    ls 查看当前运行的容器
    ps 查看当前运行的容器
    pause 暂停容器
    unpause 开启容器
    port 查看容器端口映射信息
    rename 对容器改名
    restart 重启容器
    start 启动容器
    stop 停掉容器
    rm 删除容器,默认删除不了正在运行的容器,-f
    stats 查看容器的运行状态
    top 查看容器的系统内存,磁盘使用信息
     
     
    例子1:启动容器
    前台运行容器
    -i: 交互模式
    -t: 分派终端
    [root@zxw99 ~]# docker run -ti busybox /bin/sh
    / #

    在容器外执行命令
    [root@zxw99 ~]# docker run -ti busybox ls
    bin dev etc home proc root sys tmp usr var

    [root@zxw99 ~]# docker exec -it 41d2872d3e6a ls
    bin dev home lib64 mnt proc run srv tmp var
    boot etc lib media opt root sbin sys usr

    查看当前运行命令
    [root@zxw99 ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    2300907aaad9 nginx "nginx -g 'daemon of…" 19 seconds ago Up 18 seconds 80/tcp hungry_kilby

    查看所有已经运行的或退出的
    [root@zxw99 ~]# docker ps -a

    后台运行容器
    开启容器的服务
    -d: 守护进程
    [root@zxw99 ~]# docker run -d -p 80:80 nginx

    停止容器服务
    [root@zxw99 ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    41d2872d3e6a nginx "nginx -g 'daemon of…" 15 seconds ago Up 14 seconds 0.0.0.0:88->80/tcp sharp_lalande
    6cf30c091e8c nginx "nginx -g 'daemon of…" 9 minutes ago Up 9 minutes 80/tcp trusting_nash
    2300907aaad9 nginx "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes 80/tcp hungry_kilby
    [root@zxw99 ~]# docker stop 6cf30c091e8c

     


    制作镜像的方法
    方法一:commit

    方式二:dockerfile

    使用commit制作镜像
    基于现在的镜像里制作

    第一步:启动容器
    [root@zxw99 ~]# docker run -d -p 88:80 httpd
    92671e9ac110704afa365eb0b9605579ad362222940773b41ea8fd4c3186ca0e


    第二步:修改配置文件
    [root@zxw99 ~]# docker exec -it a3fd10c82512 bash
    root@a3fd10c82512:/usr/local/apache2#
    [root@zxw99 ~]# curl 192.168.126.99:88
    <html><body><h1>It works!</h1></body></html>
    [root@zxw99 ~]# docker exec -it a3fd10c82512 bash
    root@a3fd10c82512:/usr/local/apache2#
    root@92671e9ac110:/usr/local/apache2# cd htdocs/
    root@92671e9ac110:/usr/local/apache2/htdocs# ls
    index.html
    root@92671e9ac110:/usr/local/apache2/htdocs# echo -e "hello world this is ip:192.168.126.99" > index.html
    root@a3fd10c82512:/usr/local/apache2# pwd
    /usr/local/apache2


    第三步:进行提交
    [root@zxw99 ~]# docker commit -p 92671e9ac110 httpd:v1
    sha256:20140cbbd111cbcb9b7f71d400dd0c143e9f2d7ac969cfa5247614376b9137e2

     

    第四步:把镜像制作成压缩包
    [root@zxw99 ~]# docker image save httpd:v1 -o httpd-com.tar
    [root@zxw99 ~]# ls
    anaconda-ks.cfg busybox1.31.tar docker.sh httpd-com.tar


    第五步:创建镜像仓库

     


    [root@zxw99 ~]# docker login --username=zxwgq0930 registry.cn-huhehaote.aliyuncs.com
    Password: #阿里登录密码
    WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
    Configure a credential helper to remove this warning. See
    https://docs.docker.com/engine/reference/commandline/login/#credentials-store

    Login Succeeded

    制作出镜像
    [root@zxw99 ~]# docker tag httpd:v1 registry.cn-huhehaote.aliyuncs.com/zxwgq/httpd-zxw:v1

     

    镜像推送到仓库
    docker push registry.cn-huhehaote.aliyuncs.com/zxwgq/httpd-zxw:v1

     

    拉取镜像
    docker pull registry.cn-huhehaote.aliyuncs.com/zxwgq/httpd-zxw:v1

  • 相关阅读:
    js下拉列表效果
    js格式化数字/日期
    通用的网页上的播放本地视频文件
    综合
    componentartclientmodeediting
    ESC键
    asp.net/ajax
    转载MS AJAX
    不允许进行远程连接可能会导致此失败 error: 40
    2007.9.17
  • 原文地址:https://www.cnblogs.com/itzhao/p/11341075.html
Copyright © 2020-2023  润新知