• 走近docker——ubuntu server 18.04上初体验


    本文为学习CloudMan的《每天5分钟玩转容器技术》的笔记

    一、安装

    在ubuntu 18.04上安装docker非常简单,运行sudo snap install docker即可。

    注意: 实际使用发现用snap安装的docker在通过dockerfile build镜像时总提示权限问题,暂时不知如何解决。

    建议还是用docker官网的方式安装。地址:https://docs.docker.com/install/linux/docker-ce/ubuntu/#prerequisites

    流程如下:

    1. SET UP THE REPOSITORY

    1) Update the apt package index:

    sudo apt-get update

    2) Install packages to allow apt to use a repository over HTTPS:

     sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

    3) Add Docker’s official GPG key:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

    4) Use the following command to set up the stable repository. You always need the stable repository, even if you want to install builds from the edge or test repositories as well. To add the edge or test repository, add the word edge or test (or both) after the word stable in the commands below.

    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

    2. INSTALL DOCKER CE

    1) Update the apt package index

    sudo apt-get update

    2) Install the latest version of Docker CE

    sudo apt-get install docker-ce

    二、核心组件

    1. docker engine: 运行在runtime(runc)之上,包括daemon和cli。

    daemon用systemctl来管理(如: systemctl start docker.service),平时在终端输入的docker指令属于cli。

     由于是用snap安装的,所以需要通过systemctl status snap.docker.dockerd.service来查看daemon的状态。

    cli指令:

    docker pull: 从registry下载镜像(image)。

    docker run: 通过镜像启动容器(container),如果本地没有镜像则先下载。

    docker ps / docker container ls: 两者均是显示正在运行的容器。

    docker images: 显示本地镜像。

    2. docker image: 只读模板。

    3. docker container: image的运行实例。

    4. registry: 存放image的仓库。

     三、理解image镜像

    从以上的信息判断,docker cli指令可以在使用时查看手册学习,先格物docker image对于致知docker有很大的帮助。

    1. 使用最小的hello-world镜像运行容器

    docker run hello-world

    可以看到会输出Hello from Docker!...的信息

    2. 学习dockerfile

    由于image是通过dockerfile创建的,我们先看一下linux中hello-world的dockerfile。(在https://hub.docker.com/_/hello-world/可以找到链接)

    PS: URL地址中的nanoserver是windows上的。

    FROM scratch
    COPY hello /
    CMD ["/hello"]

    1) FROM scratch

    此镜像从零开始构建。

    2) COPY hello /

    将文件hello复制到镜像的根目录。

    3) CMD ["/hello"]

    容器启动时,执行/hello。

    从零开始构建镜像达到发布程序的目的,对于现在的我来说无法想象。

    我希望有一个基本的操作系统环境,在此之上安装配置一些软件,打包一些库和发布程序即可。这种镜像一般被称作base镜像。

    四、base镜像

    1. base镜像含义

    1)不依赖其他镜像,从scratch构建;2)其他镜像可以以之为基础进行扩展。

    一般能称作base镜像的通常都是各种Linux发行版的镜像,如:ubuntu、debian、centos等。

    2. rootfs

    官方发行的base镜像只有几十M到几百M,因为对于base镜像来说底层可以直接用Host的kernel,自己只需提供rootfs即可。

    centos7的dockerfile:

    FROM scratch
    ADD centos-7-docker.tar.xz /
    
    LABEL org.label-schema.schema-version="1.0" 
        org.label-schema.name="CentOS Base Image" 
        org.label-schema.vendor="CentOS" 
        org.label-schema.license="GPLv2" 
        org.label-schema.build-date="20180804"
    
    CMD ["/bin/bash"]

    第二行ADD指令会自动解压。centos-7-docker.tar.xz中包含/dev、/proc、/bin等目录。

    LABEL用于给镜像添加信息,使用docker inspect可以查看这些相关信息。

    3. kernel

    base镜像只是在用户空间与发行版一致,kernel版本与发行版不同。

    通过base镜像启动容器后,容器使用kernel为Host的kernel。所以若对kernel版本有要求,则不建议使用docker。

  • 相关阅读:
    iOS7 iOS8 毛玻璃效果的分别实现
    关于app transfer之后的开发
    微信登录后,请求用户信息时, 返回地址信息是拼音的解决方案
    获得设备型号(含iPhone6 , iPhone 6+)
    iOS 开发者计划申请 2014 年最新心得[转]
    Facebook的Pop动画库相关资料
    iOS添加自定义字体方法
    iOS8新增加的frameworks, 在目前基于7以上开发的情况下, 使用下列sdk要注意设置成optional
    iOS开发 .framework的Optional(弱引用)和Required(强引用)区别, 有错误 Library not found………………
    python接口自动化测试二:常用操作
  • 原文地址:https://www.cnblogs.com/xiaochuizi/p/9483630.html
Copyright © 2020-2023  润新知