0
点赞
收藏
分享

微信扫一扫

docker基础用法

凛冬已至夏日未远 2022-04-24 阅读 64
linux

文章目录

1.dorcker简介

docker是容器技术的一个前端工具,容器是内核的一项技术,docker只是把这一项技术的使用得以简化,使之普及而已。(dorcker是管理容器的工具)

docker中的容器:

  • lxc --> libcontainer --> runC

2.OCI&OCF

OCI
Open Container-initiative

  • 由Linux基金会主导于2015年6月创立
  • 旨在围绕容器格式和运行时制定一个开放的工业化标准
  • contains two specifications
  • the Runtime Specification(runtime-spec)运行时的规范
  • the Image Specification(image-spec) 镜像的规范

OCF
Open Container Format
runC is a CLI tool for spawning and running containers according to the OCI specification(命令行工具 用于创建运行容器 根据OCI的标准)

  • Containers are started as a child process of runC and can be embedded into various other systems without having to run a daemon(将容器启动为runC的子进程 可以嵌入到其他各种系统中,而无需运行守护进程)

  • runC is built on libcontainer, the same container technology powering millions of Docker Engine installations(runC构建在libcontainer上 提供给很多人使用)
    docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com

3.docker架构

在这里插入图片描述
客户端和主机在同一台主机上 客户机执行命令 向主机发送请求寻找服务 查找本地有无此服务 有的话就启动 没有去镜像中下载启动
docker-ee 付费
docker-ce 开源

4.docker镜像与镜像仓库

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

5.docker对象

When you use docker, you are creating and using images, containers, networks, volumes, pluginns, and other objects.(当你使用docker时,你是在创建和使用图像、容器、网络、卷、插件和其他对象。)

  • IMAGES
    • An image is a read-only template with instructions for creating a docker container.(镜像是一个只读的模板 用于创建dorker容器)
    • Often, an image is based on another image, with some additional customization.(通常,一个镜像基于另一个镜像,并带有一些额外的定制。)
    • You might create your own images or you might only use those created by others and published in a registry.(您可以创建自己的图像,也可以只使用其他人创建并在注册表中发布的图像。)
  • CONTAINERS
    • A conntainer is a runnable instance of an image.(容器是镜像的可运行实例。)
    • You can create, run, stop, move, or delete a container using the docker API or CLI.(您可以使用docker API或CLI创建、运行、停止、移动或删除容器。)
    • You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.(您可以将一个容器连接到一个或多个网络,将存储附加到它,甚至根据它的当前状态创建一个新镜像)

6.安装及使用docker

6.1docker安装

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@localhost yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@localhost yum.repos.d]# yum install -y docker-ce  //正常安装
[root@localhost yum.repos.d]# yum install -y docker-ce --allowerasing  //--allowerasing自动替换冲突包

6.2docker加速

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

docker的加速有多种方式:

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

[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.14
 API version:       1.41
 Go version:        go1.16.15
 Git commit:        a224086
 Built:             Thu Mar 24 01:47:44 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.14
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.15
  Git commit:       87a90dc
  Built:            Thu Mar 24 01:46:10 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.5.11
  GitCommit:        3df54a852345ae127d1fa3092b95168e4a88e2f8
 runc:
  Version:          1.0.3
  GitCommit:        v1.0.3-0-gf46b6ba
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
  
[root@localhost ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.8.1-docker)
  scan: Docker Scan (Docker Inc., v0.17.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.14
 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: 3df54a852345ae127d1fa3092b95168e4a88e2f8
 runc version: v1.0.3-0-gf46b6ba
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.18.0-193.el8.x86_64
 Operating System: Red Hat Enterprise Linux 8.2 (Ootpa)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 1.758GiB
 Name: localhost.localdomain
 ID: MRDF:Q5XI:ZGZK:7UNA:NGDM:WG4V:GCOB:HSWS:LWSU:EBOB:37OV:G5UY
 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://docker.mirrors.ustc.edu.cn/
 Live Restore Enabled: false

6.3docker常用命令

#docker search 从Docker Hub查找镜像
[root@localhost ~]# docker search httpd   //查找
NAME                                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
httpd                                The Apache HTTP Server Project                  3976      [OK]       
centos/httpd-24-centos7              Platform for running Apache httpd 2.4 or bui…   44                   
centos/httpd                                                                         35                   [OK]

#docker pull 从镜像仓库中拉取或者更新指定镜像
[root@localhost ~]# docker pull httpd   //从镜像仓库中拉取httpd   可以接版本 不接为最新版本
Using default tag: latest
latest: Pulling from library/httpd
a2abf6c4d29d: Pull complete 
dcc4698797c8: Pull complete 
41c22baa66ec: Pull complete 
67283bbdd4a0: Pull complete 
d982c879c57e: Pull complete 
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

#docker images  列出本地镜像
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
httpd        latest    dabbfbe0c57b   4 months ago   144MB

#docker create 创建一个新的容器但不启动它
[root@localhost ~]# docker create --name web -p 80:80 httpd
48421722f053a5b6181517a5deb574eba3514685d74ea09df81284412623c920

#docker ps 列出容器  
[root@localhost ~]# docker ps -a  //-a 列出所有 包括未启动的
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS    PORTS     NAMES
48421722f053   httpd     "httpd-foreground"   About a minute ago   Created             web

#docker start 启动容器 可使用名字 id
[root@localhost ~]# docker start web
web
[root@localhost ~]# docker ps  //查看已经启动 并且有端口号
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS                               NAMES
48421722f053   httpd     "httpd-foreground"   2 minutes ago   Up 3 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web

#docker stop  关闭容器 可以使用名字 id
[root@localhost ~]# docker stop 48421722f053
48421722f053
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES


# docker restart 重启容器
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS                               NAMES
48421722f053   httpd     "httpd-foreground"   6 minutes ago   Up 5 seconds 50.0.0.0:80->80/tcp, :::80->80/tcp   web
[root@localhost ~]# docker restart web
web
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS        PORTS                               NAMES
48421722f053   httpd     "httpd-foreground"   7 minutes ago   Up 1 second 10.0.0.0:80->80/tcp, :::80->80/tcp   web

#docker kill 杀死运行中的容器  stop正常退出 kill强制关闭
[root@localhost ~]# docker kill web
web
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS                       PORTS     NAMES
48421722f053   httpd     "httpd-foreground"   8 minutes ago   Exited (137) 7 seconds ago             web

#docker logs 查看日志
[root@localhost ~]# docker logs web

#docker rm  删除一个或多个容器 运行时不可以删除
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                      PORTS                               NAMES
69f8a9712431   busybox   "/bin/sh"            11 minutes ago   Up 3 minutes                                                    test
b597ea864dda   httpd     "ls"                 14 minutes ago   Exited (0) 13 minutes ago                                       inspiring_borg
48421722f053   httpd     "httpd-foreground"   26 minutes ago   Up 16 minutes               0.0.0.0:80->80/tcp, :::80->80/tcp   web
[root@localhost ~]# docker stop test   //停止
test
[root@localhost ~]# docker rm test   //删除test容器
test
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                      PORTS                               NAMES
b597ea864dda   httpd     "ls"                 15 minutes ago   Exited (0) 15 minutes ago                                       inspiring_borg
48421722f053   httpd     "httpd-foreground"   27 minutes ago   Up 17 minutes               0.0.0.0:80->80/tcp, :::80->80/tcp   web



#docker run 创建一个新的容器并运行一个命令
[root@localhost ~]# docker run -it --name test busybox /bin/sh
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete 
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest
/ # 
/ # exit   //交互模式内 如果退出 就会停止容器

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS                      PORTS                               NAMES
69f8a9712431   busybox   "/bin/sh"            About a minute ago   Exited (0) 57 seconds ago                                       test


#docker attach  连接到正在运行中的容器
[root@localhost ~]# docker start test
test
[root@localhost ~]# docker attach test
/ # exit   //此方式 进入退出后也会自动关闭容器
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                     PORTS                               NAMES
69f8a9712431   busybox   "/bin/sh"            6 minutes ago    Exited (0) 9 seconds ago                                       test


#docker exec 在运行的容器中执行命令  
[root@localhost ~]# docker start test
test
[root@localhost ~]# docker exec -it test /bin/sh
/ # exit   //此方式退出 容器还在运行
[root@localhost ~]# docker ps -a 
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                      PORTS                               NAMES
69f8a9712431   busybox   "/bin/sh"            9 minutes ago    Up About a minute   

docker event state
在这里插入图片描述

举报

相关推荐

0 条评论