测试工程师Docker基础

一、docker概述

1、docker为什么会出现

本质:所有技术的出现都是因为出现了一些问题,我们需要去解决,才去研究和学习;

问题:

​ 开发环境、测试环境、仿真环境、正式环境等诸多环境都需要进行频繁的服务更新、环境维护等;

​ 不同的环境由于成本的问题很难做到完全一致和统一,导致同样的服务在不同环境出现服务不可用等问题;

docker给以上问题提出的解决方案:带上运行环境一起打包、更新、上线即可。docker的核心思想就是打包装箱,每个箱子互相隔离,互不影响。

2、docker为什么火

核心原因:docker十分的轻巧。因为在容器技术出现之前我们使用的都是虚拟技术。

虚拟机:在windows上面安装一个vmware虚拟机软件,通过这个软件我们可以虚拟出多台机器;其实虚拟机属于虚拟化技术,docker容器技术其实也属于虚拟化技术。

虚拟机和docker对比:

传统虚拟机技术:虚拟机相关硬件资源,运行一个完整的操作系统,然后在该操作系统上面安装和运行相关需要的软件;
容器技术:容器公用宿主机相关硬件资源,容器没有自己的内核也没有虚拟任何硬件,这样就轻便了许多。每个容器都相互隔离,并且拥有自己的文件系统,互不影响;

3、docker优势

  • 应用更加快速的交付和部署

传统:一堆帮助文档和安装程序;

docker:打包镜像、发布测试一件运行;

  • 更便捷的升级和扩容

使用docker之后,我们部署应用就跟搭积木一样,项目打包为一个镜像,可以很方便的扩展容器1、容器2等

  • 更简单的系统维护

docker之后,我们的开发环境、测试环境、线上环境是高度一致的;

  • 更高效的计算资源利用

docker是一个内核级别的虚拟化技术,可以在一台物理机上运行多个容器实例。服务器的性能可以被压榨到极致;

二、docker安装

1、docker基本组成

image.png

  • 镜像(image)

docker镜像就好比是一个目标,可以通过这个目标来创建容器服务,tomcat镜像==>run==>容器(提

供服务器),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)。

  • 容器(container)

Docker利用容器技术,独立运行一个或者一组应用,通过镜像来创建的.

启动,停止,删除,基本命令

目前就可以把这个容器理解为就是一个简易的 Linux系统。

  • 仓库(repository)

仓库就是存放镜像的地方!

仓库分为公有仓库和私有仓库。(很类似git)

Docker Hub是国外的。

2、docker安装

直接yum安装即可:

#查看linux内核版本
[root@ecs-x-large-2-linux-20200305213344 ~]# uname -r
3.10.0-1160.6.1.el7.x86_64

#查看系统版本详情
[root@ecs-x-large-2-linux-20200305213344 ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

#查看docker是否安装成功
[root@ecs-x-large-2-linux-20200305213344 ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-203.git0be3e21.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      0be3e21/1.13.1
 Built:           Thu Nov 12 15:11:46 2020
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-203.git0be3e21.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      0be3e21/1.13.1
 Built:           Thu Nov 12 15:11:46 2020
 OS/Arch:         linux/amd64
 Experimental:    false
 
 #测试docker
 [root@ecs-x-large-2-linux-20200305213344 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:89b647c604b2a436fc3aa56ab1ec515c26b085ac0c15b0d105bc475be15738fb
Status: Downloaded newer image for docker.io/hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
 
 #查看下载后的镜像
 [root@ecs-x-large-2-linux-20200305213344 ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              d1165f221234        7 hours ago         13.3 kB
docker.io/nginx         latest              35c43ace9216        2 weeks ago         133 MB

3、docker运行原理

docker是一个client-server结构的系统,docker的守护进程运行在主机上,通过socker从客户端访问。server接收到client指令,就只执行这个命令。

未命名表单.png

三、docker常用命令

1、帮助命令

docker version #显示dokcer版本信息
docker info #显示docker的系统信息,包括镜像和容器数量
docker 命令 --help #帮助命令

2、镜像命令

docker images #查看所有本地主机上的镜像
docker search #搜索某个镜像
docker pull #下载镜像
docker rmi #删除镜像

docker images #查看所有本地主机上的镜像

[root@ecs-x-large-2-linux-20200305213344 ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              d1165f221234        7 hours ago         13.3 kB
docker.io/nginx         latest              35c43ace9216        2 weeks ago         133 MB
#解释

#REPOSITORY  镜像的仓库源
#TAG         镜像标签
#IMAGE ID    镜像id
#CREATED     镜像创建时间
#SIZE        镜像大小

#可选项
Options:
  -a, --all             Show all images (default hides intermediate images)#查看所有镜像
  -q, --quiet           Only show numeric IDs #只显示镜像id
  
[root@ecs-x-large-2-linux-20200305213344 ~]# docker images -aq  #显示所有镜像id
d1165f221234
35c43ace9216

docker pull #下载镜像

#下载镜像 docker pull 镜像名[:tag]  如果不写tag默认下载最新
[root@ecs-x-large-2-linux-20200305213344 ~]# docker pull redis
Using default tag: latest
Trying to pull repository docker.io/library/redis ...
latest: Pulling from docker.io/library/redis
45b42c59be33: Already exists   #分层下载
5ce2e937bf62: Pull complete
2a031498ff58: Pull complete
2f3d47096658: Pull complete
04f5cb8ac4c0: Pull complete
9ed141398658: Pull complete
Digest: sha256:9a1a2bb9fd2bd8b2c15aaca44d8e6ba8bc448df9b7b8d7d24ba4b472e0da1b8a
Status: Downloaded newer image for docker.io/redis:latest #镜像的真实地址

docker rmi #删除镜像

docker rmi -f 镜像id #删除指定的镜像 
docker rmi -f 镜像id 镜像id #删除指定的镜像 
docker rmi -f $(docker images -aq) #删除全部的镜像

3、容器命令

备注:我们有了镜像才能创建容器

docker run 镜像id #新建容器并启动 
docker ps #列出所有运行的容器 
docker rm 容器id #删除指定容器 
docker start 容器id #启动容器 
docker restart容器id #重启容器 
docker stop 容器id #停止当前正在运行的容器 
docker kill 容器id #强制停止当前容器
#查看容器所有命令
[root@ecs-x-large-2-linux-20200305213344 ~]# docker container

Usage:	docker container COMMAND

Manage containers

Options:
      --help   Print usage

Commands:
  attach      Attach to a running container
  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
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  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
  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
  wait        Block until one or more containers stop, then print their exit codes

新建容器并启动

docker runb [可选参数] image
#参数说明
--name =”name“ #容器名字,用于区分不同的的容器
-d #后台运行
-it #使用交互模式运行,进入容器查看相关内容
-p #指定容器端口
	-p 主机端口:容器端口
	
#测试-it
[root@ecs-x-large-2-linux-20200305213344 ~]# docker run -it 300e315adb2f /bin/bash
[root@9f5b41ce646c /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@9f5b41ce646c /]# exit  #退出容器
exit

#测试 --name -p -d
[root@ecs-x-large-2-linux-20200305213344 ~]# docker run --name nginx01 -d -p 3334:80 35c43ace9216
cacf7a960d29dc24755f6b9896aaa85f2e9f902ff7f620470c907acf5e17bd7a
[root@ecs-x-large-2-linux-20200305213344 ~]# curl localhost:3334
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title> #成功访问3334端口
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

#测试是否后台运行
[root@ecs-x-large-2-linux-20200305213344 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                  NAMES
cacf7a960d29        35c43ace9216        "/docker-entrypoin..."   About a minute ago   Up About a minute   0.0.0.0:3334->80/tcp   nginx01

列出所有运行的容器

#docker ps #列出当前正在运行的容器
Options:
  -a, --all             Show all containers (default shows just running)#列出目前所有容器
  -q, --quiet           Only display numeric IDs #只显示容器id
  
[root@ecs-x-large-2-linux-20200305213344 ~]# docker ps -aq    #显示所有容器id
9f5b41ce646c
7aab85f14183

退出容器

exit #退出容器

删除容器

docker rm 容器id #删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -rf docker 
rm -f $(docker ps -aq) #删除指定的容器 
docker ps -a -q|xargs docker rm #删除所有的容器

启动和停止容器

docker start 容器id #启动容器 
docker restart 容器id #重启容器 
docker stop 容器id #停止当前正在运行的容器 
docker kill 容器id #强制停止当前容器

查看日志

[root@ecs-x-large-2-linux-20200305213344 ~]# docker logs --help

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --help           Print usage
      --since string   Show logs since timestamp
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
  
#显示日志
-tf #显示日志信息(一直更新)
--tail number #需要显示日志条数
docker logs -t --tail n 容器id #查看n行日志 docker logs -ft 容器id #跟着日志

查看容器中的进程信息

#docker top 容器id 
[root@ecs-x-large-2-linux-20200305213344 ~]# docker top cacf7a960d29
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                27859               27841               0                   16:26               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 27908               27859               0                   16:26               ?                   00:00:00            nginx: worker process

查看容器元数据

#docker inspect 容器id
[root@ecs-x-large-2-linux-20200305213344 ~]# docker inspect cacf7a960d29
[
    {
        "Id": "cacf7a960d29dc24755f6b9896aaa85f2e9f902ff7f620470c907acf5e17bd7a",
        "Created": "2021-03-06T08:26:19.015315592Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 27859,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-03-06T08:26:19.32665951Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:35c43ace9216212c0f0e546a65eec93fa9fc8e96b25880ee222b7ed2ca1d2151",
        "ResolvConfPath": "/var/lib/docker/containers/cacf7a960d29dc24755f6b9896aaa85f2e9f902ff7f620470c907acf5e17bd7a/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/cacf7a960d29dc24755f6b9896aaa85f2e9f902ff7f620470c907acf5e17bd7a/hostname",
        "HostsPath": "/var/lib/docker/containers/cacf7a960d29dc24755f6b9896aaa85f2e9f902ff7f620470c907acf5e17bd7a/hosts",
        "LogPath": "",
        "Name": "/nginx01",
        "RestartCount": 0,
        "Driver": "overlay2",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {

进入当前正在运行的容器

#我们通常容器都是在后台运行方式,如果需要修改一些配置或其他操作,那么我们需要进入运行的容器
#方式一:docker exec - it容器id /bin/bash
[root@ecs-x-large-2-linux-20200305213344 ~]# docker exec -it cacf7a960d29 /bin/bash
root@cacf7a960d29:/# ls
bin   dev		   docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc			 lib   media  opt  root  sbin  sys  usr
#方式二:docker attach 容器id
[root@ecs-x-large-2-linux-20200305213344 ~]# docker attach cacf7a960d29
正在执行的代码******
[root@ecs-x-large-2-linux-20200305213344 ~]#  #ctrl+c 退出
#区别
#docker exec #进入当前容器后开启一个新的终端,可以在里面操作。(常用)
#docker attach # 进入容器正在执行的终端

从容器内拷贝到主机

#docker cp 容器id:容器内路径 主机目的路径

#测试
#进入容器内部
[root@ecs-x-large-2-linux-20200305213344 ~]# docker exec -it cacf7a960d29 /bin/bash
root@cacf7a960d29:/# ls
bin   dev		   docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc			 lib   media  opt  root  sbin  sys  usr
#新建一个文件
root@cacf7a960d29:/# echo "hello" >test.java
root@cacf7a960d29:/# ls
bin   dev		   docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  test.java  usr
boot  docker-entrypoint.d  etc			 lib   media  opt  root  sbin  sys  tmp        var
#退出容器
exit
#拷贝
docker cp cacf7a960d29:test.java /home/
#验证是否copy到主机
[root@ecs-x-large-2-linux-20200305213344 ~]# ls /home/
test.java

4、命令总结

WX20210306-170549@2x.png