Docker入门学习(一)

Docker入门学习(一)

1.什么是Docker?

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

2.Docker的应用场景

  • Web 应用的自动化打包和发布。
  • 自动化测试和持续集成、发布。
  • 在服务型环境中部署和调整数据库或其他的后台应用。
  • 从头编译或者扩展现有的 OpenShift 或 Cloud Foundry 平台来搭建自己的 PaaS 环境。

3.Docker 的优点

  • 快速,一致地交付您的应用程序
  • 响应式部署和扩展
  • 在同一硬件上运行更多工作负载

总之一句话就是一次构建,到处运行

4.Docker的架构

镜像(Image):Docker 镜像,就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。
容器(Container):镜像和容器的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。可以把容器看做是一个简易版的Linux环境。
仓库(Repository):仓库可看着一个代码控制中心,用来保存镜像。

我们可以拿Java做一个类比

Docker Java
镜像
容器 对象

比如:

Person p1 = new Person();
Person p2 = new Person();
Person p3 = new Person()

Person就是镜像,p1、p2、p3就是容器。

以下是Docker的架构图:
Docker入门学习(一)_第1张图片

概念 说明
Docker 客户端(Client) Docker 客户端通过命令行或者其他工具使用 Docker SDK (https://docs.docker.com/develop/sdk/) 与 Docker 的守护进程通信。
Docker 主机(Host) 一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。
Docker Registry Docker Hub(https://hub.docker.com) 提供了庞大的镜像集合供使用。
Docker Machine Docker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox、 Digital Ocean、Microsoft Azure。

5.Docker的安装

Docker的安装教程网上有很多,在这里就不在赘述。

6.Docker容器与虚拟机的区别

Docker容器 虚拟机
操作系统 与宿主机共享OS 在宿主机运行虚拟机OS
存储大小 镜像小,便于存储与传输 镜像庞大
运行性能 几乎无额外性能损失 操作系统额外的CPU、内存消耗
移植性 轻便、灵活、适应于Linux 笨重,与虚拟机技术耦合度高
硬件亲和力 面向软件开发者 面向硬件运维者
部署速度 快速、秒级 较慢,10s以上

7.Docker帮助命令

(1)查看docker的信息:docker version

Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-109.gitcccb291.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      cccb291/1.13.1
 Built:           Tue Mar  3 17:21:24 2020
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-109.gitcccb291.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      cccb291/1.13.1
 Built:           Tue Mar  3 17:21:24 2020
 OS/Arch:         linux/amd64
 Experimental:    false

(2)查看Docker的更详细信息:docker info

Containers: 3
 Running: 3
 Paused: 0
 Stopped: 0
Images: 3
Server Version: 1.13.1
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: false
Logging Driver: journald
Cgroup Driver: systemd
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version:  (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 66aedde759f33c190954815fb765eedc1d782dd9 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
 seccomp
  WARNING: You're not using the default seccomp profile
  Profile: /etc/docker/seccomp.json
Kernel Version: 3.10.0-514.26.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 1
Total Memory: 1.796 GiB
Name: iZbp1j71l9e8k40vg0cb02Z
ID: QQUC:WWSZ:FOER:ZUNE:OTEP:3EVT:IENO:CDME:JRV7:U7AK:YS5J:S7R7
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Experimental: false
Insecure Registries:
 127.0.0.0/8
Registry Mirrors:
 https://3huzqv5x.mirror.aliyuncs.com
Live Restore Enabled: false
Registries: docker.io (secure)

(3)查看docker的命令:docker help

Usage:	docker COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -D, --debug              Enable debug mode
      --help               Print usage
  -H, --host list          Daemon socket(s) to connect to (default [])
  -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:
  container   Manage containers
  image       Manage images
  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
  volume      Manage volumes

Commands:
  attach      Attach 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
  diff        Inspect changes 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

8.Docker镜像命令

(1)查看本地的docker镜像:docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/mongo     latest              3f3daf863757        4 weeks ago         388 MB
docker.io/redis     latest              4cdbec704e47        7 weeks ago         98.2 MB
docker.io/mysql     5.7                 413be204e9c3        7 weeks ago         456 MB

字段说明:

字段名 说明
REPOSITORY 表示镜像的仓库源
TAG 镜像的标签
IMAGE 镜像ID
CREATED 镜像的创建时间
SIZE 镜像大小

可以使用REPOSITORY:TAG来定义不同的镜像

其他可选命令:

  • 列出全部镜像(包含中间镜像):docker images -a
  • 只显示镜像ID :docker images -q
  • 显示镜像说明:docker images --digests

(2)搜索某个镜像:docker search “xxx”
它是从https://hub.docker.com/去查找xxx镜像,但是拉取的时候是从配置的阿里云镜像加速器来pull的。

[root@iZbp1j71l9e8k40vg0cb02Z ~]# docker search tomcat
INDEX       NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/tomcat                        Apache Tomcat is an open source implementa...   2737      [OK]       
docker.io   docker.io/tomee                         Apache TomEE is an all-Apache Java EE cert...   79        [OK]       
docker.io   docker.io/dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba...   54                   [OK]
docker.io   docker.io/bitnami/tomcat                Bitnami Tomcat Docker Image                     33                   [OK]
docker.io   docker.io/kubeguide/tomcat-app          Tomcat image for Chapter 1                      28                   
docker.io   docker.io/consol/tomcat-7.0             Tomcat 7.0.57, 8080, "admin/admin"              17                   [OK]
docker.io   docker.io/cloudesire/tomcat             Tomcat server, 6/7/8                            15                   [OK]
docker.io   docker.io/aallam/tomcat-mysql           Debian, Oracle JDK, Tomcat & MySQL              12                   [OK]
docker.io   docker.io/arm32v7/tomcat                Apache Tomcat is an open source implementa...   10                   
docker.io   docker.io/rightctrl/tomcat              CentOS , Oracle Java, tomcat application s...   6                    [OK]
docker.io   docker.io/maluuba/tomcat7-java8         Tomcat7 with java8.                             5                    
docker.io   docker.io/unidata/tomcat-docker         Security-hardened Tomcat Docker container.      4                    [OK]
docker.io   docker.io/amd64/tomcat                  Apache Tomcat is an open source implementa...   2                    
docker.io   docker.io/arm64v8/tomcat                Apache Tomcat is an open source implementa...   2                    
docker.io   docker.io/jelastic/tomcat               An image of the Tomcat Java application se...   2                    
docker.io   docker.io/99taxis/tomcat7               Tomcat7                                         1                    [OK]
docker.io   docker.io/camptocamp/tomcat-logback     Docker image for tomcat with logback integ...   1                    [OK]
docker.io   docker.io/i386/tomcat                   Apache Tomcat is an open source implementa...   1                    
docker.io   docker.io/oobsri/tomcat8                Testing CI Jobs with different names.           1                    
docker.io   docker.io/ppc64le/tomcat                Apache Tomcat is an open source implementa...   1                    
docker.io   docker.io/appsvc/tomcat                                                                 0                    
docker.io   docker.io/cfje/tomcat-resource          Tomcat Concourse Resource                       0                    
docker.io   docker.io/picoded/tomcat7               tomcat7 with jre8 and MANAGER_USER / MANAG...   0                    [OK]
docker.io   docker.io/s390x/tomcat                  Apache Tomcat is an open source implementa...   0                    
docker.io   docker.io/secoresearch/tomcat-varnish   Tomcat and Varnish 5.0                          0                    [OK]

只显示STARS多于30的镜像 docker search -s n xxx

[root@iZbp1j71l9e8k40vg0cb02Z ~]# docker search -s 30 tomcat
Flag --stars has been deprecated, use --filter=stars=3 instead
INDEX       NAME                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/tomcat           Apache Tomcat is an open source implementa...   2737      [OK]       
docker.io   docker.io/tomee            Apache TomEE is an all-Apache Java EE cert...   79        [OK]       
docker.io   docker.io/dordoka/tomcat   Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba...   54                   [OK]
docker.io   docker.io/bitnami/tomcat   Bitnami Tomcat Docker Image                     33                   [OK]

(3)拉取某个镜像:docker pull xxx
比如要拉取tomcat下来,可以docker pull tomcat,它是等价于docker pull tomcat:latest的,所以如果想要拉取不同的版本,那就可以用在镜像名后面多添加一个版本号。

[root@iZbp1j71l9e8k40vg0cb02Z ~]# docker pull tomcat
Using default tag: latest
Trying to pull repository docker.io/library/tomcat ... 
latest: Pulling from docker.io/library/tomcat
376057ac6fa1: Pull complete 
5a63a0a859d8: Pull complete 
496548a8c952: Pull complete 
2adae3950d4d: Pull complete 
0a297eafb9ac: Pull complete 
09a4142c5c9d: Pull complete 
9e78d9befa39: Pull complete 
18f492f90b9c: Pull complete 
7834493ec6cd: Pull complete 
216b2be21722: Pull complete 
Digest: sha256:ce753be7b61d86f877fe5065eb20c23491f783f283f25f6914ba769fee57886b
Status: Downloaded newer image for docker.io/tomcat:latest

再用docker images来查看一下本地的镜像

[root@iZbp1j71l9e8k40vg0cb02Z ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/tomcat    latest              1b6b1fe7261e        9 days ago          647 MB
docker.io/mongo     latest              3f3daf863757        4 weeks ago         388 MB
docker.io/redis     latest              4cdbec704e47        7 weeks ago         98.2 MB
docker.io/mysql     5.7                 413be204e9c3        8 weeks ago         456 MB

我们发现,tomcat已经被拉取到本地了,它的大小是647 MB,这是有点奇怪的,这是为什么?后面再解释!

(4)删除掉某个镜像 docker rmi -f 镜像id
删除多个镜像 docker rmi -f 镜像名1:TAG 镜像名2:TAG ......
删除全部镜像,这是一个组合命令 docker rmi -f $(docker images -qa)

你可能感兴趣的:(Docker)