docker使用笔记(1):docker介绍与安装

引言

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源。

Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。和其它软件一样,docker也分为企业版和社区版,一般管理一些小应用,比如说我要将我的项目制成镜像,一般用社区版就行了。

docker介绍

docker是有三部分组成,分别为镜像(Image)、容器(Container)和仓库(Repository),下面就分别介绍一下这三个部分:

镜像(Image):
在 Docker 的术语里,一个只读层被称为镜像,一个镜像是永久不会变的,它可以类比于python中的元组,相当于不可变类型,显示为无状态。镜像构建时,会一层层构建,前一层是后一层的基础。每一层构建完就不会再发生改变,后一层上的任何改变只发生在自己这一层。所以当我们在构建镜像的时候,需要额外小心,每一层尽量只包含该层需要添加的东西,任何额外的东西应该在该层构建结束前清理掉。另外关于镜像ID是由 64 位十六进制字符串来标识的,为了简化使用,前12个字符可以构成短ID,但因为这样可能造成重名的冲突,所以显示依然是64位全显示。

当一个镜像创建成功后,一般内部的信息就已经固定了,之后的版本迭代只是在原基础上构建一个新的层,因此,不用替换整个原先的镜像或者重新建立,只是一个新的层被添加或升级了,这无疑更加高效。但如果我们想要改变应该怎么做呢?那么就是下面容器的内容了。

容器(container):
镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的 类 和 实例 一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。

容器的实质是进程,但与直接在宿主执行的进程不同,容器进程运行于属于自己的独立的 命名空间。因此容器可以拥有自己的 root 文件系统、自己的网络配置、自己的进程空间,甚至自己的用户 ID 空间。容器内的进程是运行在一个隔离的环境里,使用起来,就好像是在一个独立于宿主的系统下操作一样。所以我们可以将docker容器看成是docker的一个独立与隔离的应用平台,

仓库(repository):
Docker 仓库用来保存镜像,可以理解为代码控制中的代码仓库。同样的,Docker 仓库也有公有和私有的概念。公有的 Docker 仓库名字是 Docker Hub。Docker Hub 提供了庞大的镜像集合供使用。这些镜像可以是自己创建,或者在别人的镜像基础上创建。Docker 仓库是 Docker 的分发部分。

当然我们也可以自己申请一个账号管理docker镜像,和GitHub操作一样,我们可以自己本地创建好镜像然后push到dockerhub上的公共仓库或者私人仓库,但如果没有翻墙工具,就比较难注册账号了。下图为这三部分交互的可视化

在这里插入图片描述

docker安装

docker安装是很简单的,只要跟着官网的步骤,一般不会出现问题,下面我以ubuntu为例,虽然工作中用的是CentOS,但安装过程基本是一致的,官网网址为:https://docs.docker.com/install/linux/docker-ce/ubuntu/

开头有介绍过docker自17.03版本之后就分出了社区版CE,和企业版EE,一般用社区版是已经够用了,而如果是Ubuntu安装,最好满足版本是16.04以上,然后Xenial 16.04(LTS),下面是步骤:

设置存储库:

# 1. 更新apt包索引
sudo apt-get update

# 2. 安装包以允许apt通过HTTPS使用存储库
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

# 3. 添加Docker的官方GPG密钥:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# 4. 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88通过搜索指纹的最后8个字符,验证您现在拥有带指纹的密钥 。一般都是一样的秘钥
sudo apt-key fingerprint 0EBFCD88
"""
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <[email protected]>
sub   rsa4096 2017-02-22 [S]
"""

# 5. 设置稳定存储库
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

安装DOCKER CE:

# 1. 更新apt包索引
sudo apt-get update

# 2. 安装最新版本的Docker CE和containerd,或者转到下一步安装特定版本
sudo apt-get install docker-ce docker-ce-cli containerd.io

# 3. 列出当前仓库可用版本
apt-cache madison docker-ce

# 4. 根据显示的版本选择其中一个进行安装,比如目前我写这篇最新的为5:18.09.1~3-0~ubuntu-xenial
sudo apt-get install docker-ce=5:18.09.1~3-0~ubuntu-xenial docker-ce-cli=5:18.09.1~3-0~ubuntu-xenial containerd.io

当安装成功后,我们可以利用docker -version进行查看当前情况:

$ docker -version

unknown shorthand flag: 'e' in -ersion
See 'docker --help'.

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")
                           (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default
                           "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  checkpoint  Manage checkpoints
  config      Manage Docker configs
  container   Manage containers
  engine      Manage the docker engine
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  deploy      Deploy a new stack or update an existing stack
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

另外还有很多镜像的命令以及登录等操作,在下一篇中会进行详细分析。对于上面的命令,可以看下图来进一步分析:

在这里插入图片描述
摘自Visualizing Docker Containers and Images (需要翻墙,另外还有大量的对于底层的图片)



参考与推荐:
[1]. 没什么难的:Docker入门与开发实战 (熊昌隆 著)
[2]. https://docs.docker.com/install/linux/docker-ce/ubuntu/
[3]. docker入门教程
[4]. Docker —— 从入门到实践

猜你喜欢

转载自blog.csdn.net/submarineas/article/details/92642851
今日推荐