docker基础用法

目录
  • 1.什么是docker
  • 2.OCI&OCF
    • 2.1OCI
    • 2.2OCF
  • 3.docker架构
  • 4.docker镜像与镜像仓库
  • 5.docker对象
  • 6.安装及使用docker
  • 7.docker加速
  • 8.docker常用操作
  • 9.docker event state

1.什么是docker

docker中的容器:
lxc --> libcontainer --> runC
docker基础用法_第1张图片

2.OCI&OCF

2.1OCI

Open Container-initiative

由Linux基金会主导于2015年6月创立
旨在围绕容器格式和运行时制定一个开放的工业化标准
contains two specifications

  • the Runtime Specification(runtime-spec)
  • the Image Specification(image-spec)

2.2OCF

Open Container Format

runC is a CLI tool for spawning and running containers according to the OCI specification

  • Containers are started as a child process of runC and can be embedded into various other systems without having to run a daemon
  • runC is built on libcontainer, the same container technology powering millions of Docker Engine installations
    docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com

3.docker架构

4.docker镜像与镜像仓库

为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
docker基础用法_第2张图片
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。

5.docker对象

当您使用docker时,您正在创建和使用镜像、容器、网络、卷、插件和其他对象。

  • IMAGES
    • 镜像是一个只读模板,上面有创建docker容器的说明。
    • 通常,镜像是基于另一个镜像,并有一些额外的定制。
    • 您可以创建自己的镜像,或者只使用其他人创建并发布在仓库中的镜像。
  • CONTAINERS
    • 容器是镜像的可运行实例。
    • 您可以使用dockerAPI或CLI创建、运行、停止、移动或删除容器。
    • 您可以将容器连接到一个或多个网络,将存储附加到它,甚至根据其当前状态创建新镜像。

6.安装及使用docker

[root@192 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@192 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@192 ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo  docker-ce.repo
[root@192 ~]# yum makecache fast
[root@192 ~]# yum -y install docker-ce
[root@192 ~]# systemctl enable --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@192 ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2020-03-31 09:30:06 CST; 26s ago
     Docs: https://docs.docker.com
 Main PID: 2813 (dockerd)
    Tasks: 8
   Memory: 42.1M
   CGroup: /system.slice/docker.service
           └─2813 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

//安装校验
[root@192 ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:27:04 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:25:42 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

7.docker加速

用阿里云加速器步骤

登录阿里云官网https://www.aliyun.com/
创建一个阿里云账号
docker基础用法_第3张图片

docker基础用法_第4张图片

docker基础用法_第5张图片

[root@192 ~]# vim /etc/docker/daemon.json

{
  "registry-mirrors": ["https://k94psehn.mirror.aliyuncs.com"]
}
[root@192 ~]# systemctl daemon-reload
[root@192 ~]# systemctl restart docker

8.docker常用操作

命令 功能
docker search 在Docker Hub中搜索镜像
docker pull 从仓库中提取镜像或存储库
docker images 列出镜像
docker create 创建一个新的容器
docker start 启动一个或多个已停止的容器
docker run 在新容器中运行命令
docker attach 附加到运行容器
docker ps 列出容器
docker logs 提取容器的日志
docker restart 重新启动容器
docker stop 停止一个或多个运行中的容器
docker kill 杀死一个或多个正在运行的容器
docker rm 删除一个或更多的容器
docker exec 在正在运行的容器中运行命令
docker info 显示系统范围的信息
docker inspect 返回有关Docker对象的低级信息
//在Docker Hub中搜索镜像
[root@192 ~]# docker search tomcat
NAME                          DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
tomcat                        Apache Tomcat is an open source implementati…   2680                [OK]                
tomee                         Apache TomEE is an all-Apache Java EE certif…   76                  [OK]                
dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base…   53                  

//从仓库中提取镜像或存储库
[root@192 ~]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
50e431f79093: Pull complete 
dd8c6d374ea5: Pull complete 
c85513200d84: Pull complete 
55769680e827: Pull complete 
e27ce2095ec2: Pull complete 
5943eea6cb7c: Pull complete 
3ed8ceae72a6: Pull complete 
91d1e510d72b: Pull complete 
415cc4506e71: Pull complete 
a79d88064227: Pull complete 
Digest: sha256:b707d3b8b4f40951ca2f387c24ab9f78800c69c90740f0cca937a1b95204b3a4
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest

//列出镜像
[root@192 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              latest              a7fa4ac97be4        13 days ago         528MB

//显示系统范围的信息
[root@192 ~]# docker info
Client:
 Debug Mode: false

Server:
 Containers: 2
  Running: 0
  Paused: 0
  Stopped: 2
 Images: 1
 Server Version: 19.03.8
 Storage Driver: overlay2
  Backing Filesystem: 
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-862.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 1.779GiB
 Name: 192.168.249.131
 ID: VM7H:N3DL:QYCN:EFWA:IBPN:3CBO:F2GY:UN3P:33GB:DGDW:PKQP:NBPW
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://k94psehn.mirror.aliyuncs.com/
 Live Restore Enabled: false

//创建一个新的容器
[root@192 ~]# docker create -it tomcat /bin/bash
4fa4d1a800d2a8a0a2600409e12c78acf626008c54ce3bcd3e58652ad52cac21

//启动一个或多个已停止的容器
[root@192 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
4fa4d1a800d2        tomcat              "/bin/bash"         59 seconds ago      Created                                         mystifying_rosalind
fdea496cc2f5        tomcat              "/bin/bash"         13 minutes ago      Exited (0) 11 minutes ago                       upbeat_hamilton
798adb9f3186        tomcat              "/bin/bash"         19 minutes ago      Exited (0) 17 minutes ago                       sleepy_neumann
[root@192 ~]# docker start 4fa4d1a800d2
4fa4d1a800d2

//列出容器
[root@192 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
4fa4d1a800d2        tomcat              "/bin/bash"         59 seconds ago      Created                                         mystifying_rosalind
fdea496cc2f5        tomcat              "/bin/bash"         13 minutes ago      Exited (0) 11 minutes ago                       upbeat_hamilton
798adb9f3186        tomcat              "/bin/bash"         19 minutes ago      Exited (0) 17 minutes ago                       sleepy_neumann
[root@192 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4fa4d1a800d2        tomcat              "/bin/bash"         4 minutes ago       Up 7 seconds        8080/tcp            mystifying_rosalind

//在新容器中运行命令
[root@192 ~]# docker run -it nginx /bin/bash
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
68ced04f60ab: Pull complete 
28252775b295: Pull complete 
a616aa3b0bf2: Pull complete 
Digest: sha256:5dc91f5ad4941e09860f02fce957718093419a13fe9cc53db65a658cb6e6eaf5
Status: Downloaded newer image for nginx:latest
root@dcccf0f24f7e:/#
[root@192 ~]# docker run -d nginx
90c020105f038a736e377cb0798bd12c95634aa5dc1c9ae96d146d279b0543bf
[root@192 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
90c020105f03        nginx               "nginx -g 'daemon of…"   49 seconds ago      Up 48 seconds       80/tcp              confident_feistel

//重新启动容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
90c020105f03        nginx               "nginx -g 'daemon of…"   17 minutes ago      Up 17 minutes       80/tcp              confident_feistel
[root@192 ~]# docker restart 90c020105f03 
90c020105f03
[root@192 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
90c020105f03        nginx               "nginx -g 'daemon of…"   17 minutes ago      Up 4 seconds        80/tcp              confident_feistel

//停止一个或多个运行中的容器
[root@192 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
90c020105f03        nginx               "nginx -g 'daemon of…"   17 minutes ago      Up 4 seconds        80/tcp              confident_feistel
[root@192 ~]# 
[root@192 ~]# docker stop 90c020105f03
90c020105f03
[root@192 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

//杀死一个或多个正在运行的容器
[root@192 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
90c020105f03        nginx               "nginx -g 'daemon of…"   21 minutes ago      Up 16 seconds       80/tcp              confident_feistel
[root@192 ~]# docker kill 90c020105f03
90c020105f03
[root@192 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

//删除一个或更多的容器
[root@192 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES
90c020105f03        nginx               "nginx -g 'daemon of…"   23 minutes ago      Exited (137) About a minute ago                       confident_feistel
dcccf0f24f7e        nginx               "/bin/bash"              27 minutes ago      Exited (0) 25 minutes ago                             jolly_moore
4fa4d1a800d2        tomcat              "/bin/bash"              48 minutes ago      Exited (255) 29 minutes ago       8080/tcp            mystifying_rosalind
fdea496cc2f5        tomcat              "/bin/bash"              About an hour ago   Exited (0) 59 minutes ago                             upbeat_hamilton
798adb9f3186        tomcat              "/bin/bash"              About an hour ago   Exited (0) About an hour ago                          sleepy_neumann
[root@192 ~]# docker rm 798adb9f3186
798adb9f3186
[root@192 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
90c020105f03        nginx               "nginx -g 'daemon of…"   24 minutes ago      Exited (137) 2 minutes ago                         confident_feistel
dcccf0f24f7e        nginx               "/bin/bash"              28 minutes ago      Exited (0) 26 minutes ago                          jolly_moore
4fa4d1a800d2        tomcat              "/bin/bash"              49 minutes ago      Exited (255) 29 minutes ago    8080/tcp            mystifying_rosalind
fdea496cc2f5        tomcat              "/bin/bash"              About an hour ago   Exited (0) About an hour ago                       upbeat_hamilton

//附加到运行容器
[root@192 ~]# docker run -it -d nginx /bin/bash
b73a949f6814db401f4c0c2516758771d00a36cae6fa2f612deab92714c532ac
[root@192 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b73a949f6814        nginx               "/bin/bash"         7 seconds ago       Up 5 seconds        80/tcp              quirky_murdock
[root@192 ~]# docker attach b73a949f6814
root@b73a949f6814:/# pwd
/

//在正在运行的容器中运行命令
[root@192 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d5c4aee2f928        nginx               "/bin/bash"         44 seconds ago      Up 43 seconds       80/tcp              wonderful_tereshkova
[root@192 ~]# docker exec d5c4aee2f928 pwd
/

9.docker event state

docker基础用法_第6张图片

你可能感兴趣的:(docker基础用法)