docker的基础用法

什么是docker

docker中的容器:

  • lxc --> libcontainer --> runC

 OCI&OCF

OCI

开放容器倡议

  • 由Linux基金会主导于2015年6月创刊
  • 注重围绕容器格式和运行时制定开放的工业化标准
  • 包含两个规格
    • 运行时规范(runtime-spec)
    • 图像规范(image-spec)

OCF

开放式容器格式

runC 是一个 CLI 工具,用于根据 OCI 规范生成和运行容器

  • 容器作为 runC 的子进程启动,可以嵌入到各种其他系统中,而无需运行守护进程
  • runC 建立在 libcontainer 之上,同样的容器技术为数百万个 Docker Engine 安装提供支持

docker提供了一个内置容器的站点:https ://hub.docker.com

镜像与镜像仓库的关系

容器和镜像和仓库的关系:

 docker ps 会列出所有运行中的容器; docker ps -a 列出运行中和未运行的容器; docker images -a 列出所用的镜像,也可以说列出所用的可读层

docker架构

Docker 包括三个基本概念:

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

Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。

Docker 容器通过 Docker 镜像来创建。

容器与镜像的关系类似于面向对象编程中的对象与类。

docker对象

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

  • 镜像

    • 镜像是一个只读模板,其中包含创建 docker 容器的说明。
    • 通常,一个镜像基于另一个镜像,并带有一些额外的自定义
    • 您可以创建自己的镜像,也可以仅使用其他人创建并在注册表中发布的镜像。
  • 容器

    • 容器是镜像的可运行实例。
    • 您可以使用 docker API 或 CLI 创建、运行、停止、移动或删除容器。
    • 您可以将容器连接到一个或多个网络,为其附加存储,甚至可以根据其当前状态创建新镜像。

docker工作原理

Docker在Linux系统镜像上创建容器。就像其他半虚拟化工具如Virtuozzo等,所有的实例基本上运行在主机系统的内核之上,但锁定在自己的运行时间环境中,与主机环境是隔离的。

当你启动或创建一个Docker容器时,只有容器内的激活进程运行时才能开始活动。如果开启一个daemonized进程,容器会立刻结束运行,因为该进程在前端不是激活状态。如果你在前端开启一个进程,容器能正常运行,直到该进程结束。这和其他的半虚拟化工具不同,其他的半虚拟化工具基本上在相同的主机上建立“正常的”虚拟服务器实例。即使前台进程未激活,这些实例也能正常运行。

docker的安装

获取yum源

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# vim docker-ce.repo
[Docker-ce]
name=Docker-ce
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[root@localhost yum.repos.d]# dnf -y install docker-ce

启动docker

[root@localhost ~]# systemctl start docker

docker加速

docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。

docker的加速有多种方式:

  • docker cn
  • 中国科技大学加速器
  • 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
[root@localhost ~]# cat > /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}
EOF


[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 1
 Server Version: 20.10.7
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 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: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7eba5930496d9bbe375fdf71603e610ad737d2b2
 runc version: v1.0.0-0-g84113ee
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.18.0-257.el8.x86_64
 Operating System: CentOS Stream 8
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 780.3MiB
 Name: localhost.localdomain
 ID: OKXW:4S7Y:VELV:POWG:XL4H:MXSL:TN7M:MMOA:GAWL:IGWL:I7XK:WC7V
 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://kmny1apu.mirror.aliyuncs.com/    #看到这里就说明加速添加成功了
 Live Restore Enabled: false

docker常用操作

命令功能
docker search用来在docker hub上查找镜像
docker pull用来在docker上拉取指定镜像
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工具的详细信息
docker inspect用来查看指定对象的详细信息

docker search

[root@localhost ~]# docker search httpd
NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
httpd                                   The Apache HTTP Server Project                  3598      [OK]       
centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or bui…   40                   
centos/httpd                                                                            34                   [OK]
arm64v8/httpd                           The Apache HTTP Server Project                  7                    
polinux/httpd-php                       Apache with PHP in Docker (Supervisor, CentO…   5                    [OK]
solsson/httpd-openidc                   mod_auth_openidc on official httpd image, ve…   2                    [OK]
hypoport/httpd-cgi                      httpd-cgi                                       2                    [OK]
centos/httpd-24-centos8                                                                 1                    
clearlinux/httpd                        httpd HyperText Transfer Protocol (HTTP) ser…   1                    
jonathanheilmann/httpd-alpine-rewrite   httpd:alpine with enabled mod_rewrite           1                    [OK]
dockerpinata/httpd                                                                      1                    
dariko/httpd-rproxy-ldap                Apache httpd reverse proxy with LDAP authent…   1                    [OK]
manageiq/httpd                          Container with httpd, built on CentOS for Ma…   1                    [OK]
inanimate/httpd-ssl                     A play container with httpd, ssl enabled, an…   1                    [OK]
publici/httpd                           httpd:latest                                    1                    [OK]
lead4good/httpd-fpm                     httpd server which connects via fcgi proxy h…   1                    [OK]
appertly/httpd                          Customized Apache HTTPD that uses a PHP-FPM …   0                    [OK]
interlutions/httpd                      httpd docker image with debian-based config …   0                    [OK]
amd64/httpd                             The Apache HTTP Server Project                  0                    
manageiq/httpd_configmap_generator      Httpd Configmap Generator                       0                    [OK]
manasip/httpd                                                                           0                    
itsziget/httpd24                        Extended HTTPD Docker image based on the off…   0                    [OK]
trollin/httpd                                                                           0                    
webratio/httpd-with-curl                Apache HTTP Server (https://httpd.apache.org…   0                    [OK]
e2eteam/httpd             

docker pull

[root@localhost ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
33847f680f63: Already exists 
d74938eee980: Pull complete 
963cfdce5a0c: Pull complete 
8d5a3cca778c: Pull complete 
e06a573b193b: Pull complete 
Digest: sha256:61e49dd08a51d6fc421ed257bd8eb461cf2d48269d9ab2b4ff5d4c69826c3c9c
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

docker images

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
nginx        latest    08b152afcfae   5 days ago   133MB
httpd        latest    73b8cfec1155   5 days ago   138MB

docker create

[root@localhost ~]# docker create httpd
13b583433cb16ba40e9490a6eeb604ba921b8fda44fc33b6a30ec3987d20ea68
如果本地没有镜像会自动拉镜像

docker ps

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS                    PORTS     NAMES
13b583433cb1   httpd     "httpd-foreground"       About a minute ago   Created                             frosty_tesla
239e24d436d2   nginx     "/docker-entrypoint.…"   17 hours ago         Exited (0) 15 hours ago             musing_jemison

docker start

[root@localhost ~]# docker start 13b583433cb1
13b583433cb1

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS     NAMES
13b583433cb1   httpd     "httpd-foreground"   2 minutes ago   Up 3 seconds   80/tcp

docker run

[root@localhost ~]# docker run httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Wed Jul 28 01:51:17.944803 2021] [mpm_event:notice] [pid 1:tid 139625322529920] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 01:51:17.945877 2021] [core:notice] [pid 1:tid 139625322529920] AH00094: Command line: 'httpd -D FOREGROUND'
#在前台运行,然后换一个终端ps查看
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS     NAMES
3620151fcbdc   httpd     "httpd-foreground"   12 seconds ago   Up 11 seconds   80/tcp    stoic_shamir
13b583433cb1   httpd     "httpd-foreground"   8 minutes ago    Up 5 minutes    80/tcp    frosty_tesla
#ctrl+c终止,容器也会停止
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS         PORTS     NAMES
13b583433cb1   httpd     "httpd-foreground"   11 minutes ago   Up 8 minutes   80/tcp    frosty_tesla
#再次运行,就是在后台运行
[root@localhost ~]# docker start 3620151fcbdc
3620151fcbdc

docker attach

#进入容器,退出后容器也会停止
[root@localhost ~]# docker attach 13b583433cb1
^C[Wed Jul 28 01:57:36.141510 2021] [mpm_event:notice] [pid 1:tid 140663158252672] AH00491: caught SIGTERM, shutting down
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker logs

[root@localhost ~]# docker start 13b583433cb1
13b583433cb1
[root@localhost ~]# docker logs 13b583433cb1
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Wed Jul 28 01:44:45.576237 2021] [mpm_event:notice] [pid 1:tid 140579490059392] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 01:44:45.677088 2021] [core:notice] [pid 1:tid 140579490059392] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Jul 28 01:45:14.233602 2021] [mpm_event:notice] [pid 1:tid 140579490059392] AH00492: caught SIGWINCH, shutting down gracefully
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Wed Jul 28 01:45:42.452783 2021] [mpm_event:notice] [pid 1:tid 140663158252672] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 01:45:42.455763 2021] [core:notice] [pid 1:tid 140663158252672] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Jul 28 01:57:36.141510 2021] [mpm_event:notice] [pid 1:tid 140663158252672] AH00491: caught SIGTERM, shutting down
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Wed Jul 28 01:59:12.930300 2021] [mpm_event:notice] [pid 1:tid 140141014824064] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 01:59:12.930973 2021] [core:notice] [pid 1:tid 140141014824064] AH00094: Command line: 'httpd -D FOREGROUND'

docker rm

#建议先停掉再删
[root@localhost ~]# docker stop 13b583433cb1
13b583433cb1
[root@localhost ~]# docker rm 13b583433cb1
13b583433cb1
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS     NAMES
3620151fcbdc   httpd     "httpd-foreground"       11 minutes ago   Exited (0) 5 minutes ago              stoic_shamir
a5f7c098a196   httpd     "httpd-foreground"       11 minutes ago   Exited (0) 11 minutes ago             reverent_tesla
239e24d436d2   nginx     "/docker-entrypoint.…"   17 hours ago     Exited (0) 16 hours ago               musing_jemison

docker exec

[root@localhost ~]# docker exec -it 3620151fcbdc /bin/bash
root@3620151fcbdc:/usr/local/apache2# pwd
/usr/local/apache2

docker event state

img

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值