走近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。

 

转载于:https://www.cnblogs.com/xiaochuizi/p/9483630.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值