Docker入门学习(一)

简介: 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的架构图:

20200526102036294.png

image.png


5.Docker的安装

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

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


image.png


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


字段说明:


image.png

可以使用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)


相关文章
|
12天前
|
存储 虚拟化 数据中心
|
1天前
|
Ubuntu 应用服务中间件 Shell
Docker入门
Docker入门
8 0
|
3天前
|
存储 Ubuntu Linux
Docker 从入门到实践:Docker介绍
Docker 从入门到实践:Docker介绍
|
4天前
|
应用服务中间件 持续交付 nginx
【Docker专栏】Docker入门指南:快速构建你的第一个容器
【5月更文挑战第7天】Docker 入门指南:容器化应用利器。了解 Docker 核心概念——镜像、容器和仓库。安装 Docker 后,运行官方 `hello-world` 验证安装,再尝试运行 `nginx` Web 服务器。通过端口映射访问容器内服务,学习管理容器命令。创建自定义镜像,编写 Dockerfile,实现 Python Web 应用容器化。Docker 助力高效开发与运维,探索更多自动化部署与微服务场景。
【Docker专栏】Docker入门指南:快速构建你的第一个容器
|
12天前
|
安全 Linux 数据库
Docker 入门详解
通过遵循这些步骤,你将能够快速入门 Docker 并开始探索容器化的世界,关注 V 哥,技术之路一起成长。
|
16天前
|
存储 Apache Swift
无限套娃_docker入门(镜像、容器、仓库)
无限套娃_docker入门(镜像、容器、仓库)
31 0
|
16天前
|
Linux 虚拟化 Docker
docker入门
docker入门
23 2
|
17天前
|
存储 运维 Docker
Docker从入门到精通:Docker 容器数据卷详解
Docker 数据卷是持久化容器数据的关键机制,允许跨容器或主机共享,即使容器删除数据仍保留。创建数据卷可通过命令行或容器启动时指定,挂载到容器目录以读写。使用 `docker volume` 命令可管理数据卷,适用于持久化存储、数据共享及备份场景。了解和善用数据卷能优化 Docker 应用程序的运维。
23 0
|
18天前
|
监控 Docker 容器
Docker从入门到精通:Docker log 命令学习
了解 Docker 日志管理对容器监控至关重要。`docker logs` 命令用于查看和管理容器日志,例如,`docker logs <container_name>` 显示容器日志,`-f` 或 `--follow` 实时跟踪日志,`--tail` 显示指定行数,`--timestamps` 添加时间戳,`--since` 按日期筛选。Docker 支持多种日志驱动,如 `syslog`,可通过 `--log-driver` 配置。有效管理日志能提升应用程序的稳定性和可维护性。
17 0
|
22天前
|
机器学习/深度学习 数据挖掘 Docker
《Docker 简易速速上手小册》第1章 Docker 基础入门(2024 最新版)
《Docker 简易速速上手小册》第1章 Docker 基础入门(2024 最新版)
27 1