Docker基础

Docker基础

1.什么是docker

docker是一个用Go语言实现的开源项目,可以让我们方便的创建和使用容器,docker将程序以及程序所有的依赖都打包到docker container,这样你的程序可以在任何环境都会有一致的表现,这里程序运行的依赖也就是容器就好比集装箱,容器所处的操作系统环境就好比货船或港口,程序的表现只和集装箱有关系(容器),和集装箱放在哪个货船或者哪个港口(操作系统)没有关系。
此外docker的另一个好处就是快速部署,这是当前互联网公司最常见的一个应用场景,一个原因在于容器启动速度非常快,另一个原因在于只要确保一个容器中的程序正确运行,那么你就能确信无论在生产环境部署多少都能正确运行。

2.docker对象

你使用docker的时候就等于你正在创建和使用镜像、容器、网络、插件功能 和其他对象

  • 镜像
  • 镜像是一个包含创建docker容器的说明只读模板。
  • 正常情况下,一个图像会基于另一个镜像的基础上进行一些额外的配置
  • 你可以创建自己的镜像,也可以使用他人创建并且在镜像仓库中发布的镜像
  • 容器
  • 容器是图像的可运行的实体
  • 你可以使用docker API或CLI创建、运行、停止、移动或删除容器。
  • 您可以将容器连接到一个或多个网络,为其附加存储,甚至基于其当前状态创建新的映像。

3.docker的安装

[root@yyx2 ~]# cd /etc/yum.repos.d/ //进入路径
[root@yyx2 yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo //配置源
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1919  100  1919    0     0  14648      0 --:--:-- --:--:-- --:--:-- 14648
[root@yyx2 yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo 

[root@yyx2 yum.repos.d]#yum -y install docker-ce //安装docker

4.docker加速

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

docker的加速有多种方式:

  • docker cn
  • 中国科技大学加速器
  • 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
[root@yyx2 yum.repos.d]# systemctl start docker

[root@yyx2 yum.repos.d]# cat >/etc/docker/daemon.json <<EOF
> {
> "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
> }
> EOF
[root@yyx2 yum.repos.d]# systemctl restart docker
[root@yyx2 yum.repos.d]# ls /etc/docker
daemon.json  key.json

[root@yyx2 yum.repos.d]# docker version //查看docker的版本
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


5.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 search  在Docker Hub中搜索镜像
[root@yyx2 ~]# docker search mysql
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                            MySQL is a widely used, open-source relation…   12455     [OK]       
mariadb                          MariaDB Server is a high performing open sou…   4800      [OK]       
mysql/mysql-server               Optimized MySQL Server Docker images. Create…   921        [OK]
percona                          Percona Server is a fork of the MySQL relati…   575       [OK]       
phpmyadmin                       phpMyAdmin - A web interface for MySQL and M…   512       [OK]       

//docker pull 从镜像仓库中拉取或更新指定镜像
[root@yyx2 ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

//docker images列出本地所有镜像
[root@yyx2 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
mysql        latest    3218b38490ce   4 months ago   516MB

//docker create创建新的容器 
[root@yyx2 ~]# docker create --name yyx -p 80:80 mysql //-p为不启动
10719e328a30bda5dbfcb18b7706bf896849a207b81b698d91bcbcec5c2d28a3
[root@yyx2 ~]# docker create --name yyxnb -p 80:80 httpd
75c71925221c998f2e30c047ebfce584e1e05b9407af2b2a675cb4130d75c11b


//docker start启动一个或多个停止的容器
[root@yyx2 ~]# docker start yyxnb
yyxnb
[root@yyx2 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS         PORTS                               NAMES
75c71925221c   httpd     "httpd-foreground"   12 seconds ago   Up 2 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   yyxnb

//docker run在新容器中运行命令
[root@yyx2 ~]# docker run -it --name yyx 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  //交互模式,exit为退出并同时停止容器

//docker attach附加到正在运行的容器
[root@yyx2 ~]# docker start yyx
yyx
[root@yyx2 ~]# docker attach yyx 
/ # exit //退出则停止容器


//docker ps列出所有容器 
[root@yyx2 ~]# docker ps -a //-a为所有的容器,包括未启动的
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS    PORTS     NAMES
10719e328a30   mysql     "docker-entrypoint.s…"   About a minute ago   Created             yyx

//docker logs获取容器的日志
[root@yyx2 ~]# docker logs yyxnb
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
[Sun Apr 24 23:57:30.813160 2022] [mpm_event:notice] [pid 1:tid 139830528826688] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Sun Apr 24 23:57:30.814179 2022] [core:notice] [pid 1:tid 139830528826688] AH00094: Command line: 'httpd -D FOREGROUND'
[Sun Apr 24 23:58:24.395197 2022] [mpm_event:notice] [pid 1:tid 139830528826688] 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
[Sun Apr 24 23:58:26.405019 2022] [mpm_event:notice] [pid 1:tid 140377602886976] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Sun Apr 24 23:58:26.405103 2022] [core:notice] [pid 1:tid 140377602886976] AH00094: Command line: 'httpd -D FOREGROUND'
//docker restart重启容器
[root@yyx2 ~]# docker restart yyxnb
yyxnb
[root@yyx2 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS         PORTS                               NAMES
75c71925221c   httpd     "httpd-foreground"   About a minute ago   Up 2 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   yyxnb

//docker stop停止一个或多个正在运行的容器  可以用id或者名字
[root@yyx2 ~]# docker start 10719e328a30
10719e328a30
[root@yyx2 ~]# docker stop yyxnb
yyxnb


//docker kill终止一个或多个正在运行的容器
[root@yyx2 ~]# docker kill yyxnb
yyxnb
[root@yyx2 ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

//docker rm删除一个或多个容器
[root@yyx2 ~]# docker ps -a 
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                     PORTS                               NAMES
75c71925221c   httpd     "httpd-foreground"       3 minutes ago    Up 39 seconds              0.0.0.0:80->80/tcp, :::80->80/tcp   yyxnb
10719e328a30   mysql     "docker-entrypoint.s…"   11 minutes ago   Exited (1) 5 minutes ago                                       yyx
[root@yyx2 ~]# docker rm yyx //删除yyx容器
yyx
[root@yyx2 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS          PORTS                               NAMES
75c71925221c   httpd     "httpd-foreground"   3 minutes ago   Up 54 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   yyxnb

//docker exec在运行容器中运行命令
[root@yyx2 ~]# docker start yyx
yyx
[root@yyx2 ~]# docker exec -it yyx /bin/sh
/ # exit

//docker info显示系统范围的信息
[root@yyx2 yum.repos.d]# 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-365.el8.x86_64
 Operating System: CentOS Stream 8
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 777.4MiB
 Name: yyx2
 ID: G5TZ:CAED:MIWK:V63S:O4YO:MQ27:RRQM:OGFG:I5TV:YSXT:KXZO:RDW3
 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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值