把 Docker 容器当做虚拟机使用

一、安装和卸载 Docker

前提条件,保证 /var/ 所在的分区空闲容量大于 40 G, 因为默认情况下, /var/lib/docker/ 目录存放了 Docker 的镜像,容器,卷,网络等文件。
当使用的镜像越来越多的情况下,这个目录的容量也会变的很大。

1 下载 Docker 仓库

wget https://download.docker.com/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

2 安装 Docker 引擎

安装最新版本的Docker CE

yum install -y docker-ce docker-ce-cli containerd.io

3 启动Docker

systemctl start docker && systemctl enable docker

4 通过运行hello-world 映像来验证是否正确安装了Docker Engine

[root@VM-0-11-centos ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:4cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc
Status: Downloaded newer image for 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/

这时,Docker 会主动去下载这个镜像,并用这个镜像启动一个容器;当容器运行时,它打印 Hello from Docker! 并退出。

5 卸载

5.1 卸载Docker Engine,CLI和Containerd软件包

yum remove docker-ce docker-ce-cli containerd.io

5.2 删除所有的镜像、容器和卷

rm -rf /var/lib/docker

6 使用非 root 用户管理 docker

Docker守护进程绑定到Unix套接字而不是TCP端口。
默认情况下,Unix套接字是归 root 用户所有,其他用户只能使用 sudo 的方式访问它。

Docker守护进程始终以 root 用户身份运行。

如果您不想在 docker 命令前面加 sudo,请创建一个名为 docker 的组(centos 中 这个组已经存在)并向其中添加用户。

当Docker守护进程启动时,它会创建一个Unix套接字,供Docker组的成员访问。


[root@qq ~]# useradd -G docker xiguatian    # 创建一个新用户,把他添加到 docker 组中
[root@qq ~]# id xiguatian
uid=1005(xiguatian) gid=1005(xiguatian)=1005(xiguatian),990(docker)

[root@qq ~]# su - xiguatian         # 切换到普通用户

[xiguatian@qq ~]$ docker image ls   # 执行 Docker 命令

二、启动一个centos7容器

1 使用阿里云的镜像源,加速镜像下载速度

注册阿里云账户并登录

之后点击控制台
在这里插入图片描述

再点击容器镜像服务

在这里插入图片描述

点击镜像加速

在这里插入图片描述

点击 Centos 后按照提示完成配置

在这里插入图片描述

2 运行 Centos7容器

语法:

docker  run    -it       centos:7 
      子命令     选项     镜像名称:标签

假如运行的镜像不在本地,会自动从 Docker hub 下载到本地,之后再运行。

[xiguatian@VM-0-11-centos ~]$ docker run -it centos:7
Unable to find image 'centos:7' locally
7: Pulling from library/centos
75f829a71a1c: Pull complete
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:7
[root@bcbf2db9ae6a /]# cd
[root@bcbf2db9ae6a ~]# ls
anaconda-ks.cfg
[root@bcbf2db9ae6a ~]# exit  # 退出容器
exit
  • -t 分配一个伪TTY,以便和容器进行命令的交互
  • -i 表示持续和 容器交互,防止断开

三、查看本地镜像

使用命令 docker images 或者 docker image ls

[xiguatian@VM-0-11-centos ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   7e6257c9f8d8        6 weeks ago         203MB
hello-world         latest              bf756fb1ae65        8 months ago        13.3kB

或者

[xiguatian@VM-0-11-centos ~]$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   7e6257c9f8d8        6 weeks ago         203MB
hello-world         latest              bf756fb1ae65        8 months ago        13.3kB
[xiguatian@VM-0-11-centos ~]$

四、容器进阶操作

1 查看容器

查看所以的容器

[xiguatian@VM-0-11-centos ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
bcbf2db9ae6a        centos:7            "/bin/bash"         9 minutes ago       Exited (0) 8 minutes ago                        hardcore_dewdney
e57b04317fad        hello-world         "/hello"            18 minutes ago      Exited (0) 18 minutes ago                       inspiring_sanderson
[xiguatian@VM-0-11-centos ~]$

各列的含义

字段名 含义
CONTAINER ID 容器 ID, 具有唯一性
IMAGE 镜像名称, 就是说这个容器是用这个镜像创建的
COMMAND 运行这个容器时,在容器内执行的命令,一般都有一些默认的命令
CREATED 此容器何时创建的
STATUS 此容器的状态
PORTS 宿主机和容器之间的端口映射
NAMES 此容器的名称

2 启动一个已经停止的容器

docker start bcbf2db9ae6a
             容器 ID

在这里插入图片描述

2 进入/退出容器

docker exec -it  bcbf2       bash
                容器 ID    容器内的命令

容器 ID 不必写全,只要保证可以识别到唯一的容器即可


[xiguatian@VM-0-11-centos ~]$ docker exec -it bcbf2 bash
[root@bcbf2db9ae6a /]# cd
[root@bcbf2db9ae6a ~]# ls
anaconda-ks.cfg
[root@bcbf2db9ae6a ~]# exit
exit
[xiguatian@VM-0-11-centos ~]$

3 停止/重启容器


docker   stop  bcbf
               容器 ID

在这里插入图片描述

4 本地文件和容器文件快速交换

docker cp   源   目标

源和目标可以是本地文件的路径,也可以是容器内的文件路径

将本地的文件 a.txt 拷贝到容器的 /root/ 目录下

docker  cp   a.txt  bcbf:/root/

在这里插入图片描述

5 挂载本地文件/目录到容器

[xiguatian@VM-0-11-centos ~]$ docker run  -it -v /home/xiguatian/a.txt:/tmp/a.txt centos:7
[root@9b3b9968b639 /]# cat /tmp/a.txt
hello shark
[root@9b3b9968b639 /]#

  • -v 本地文件路径,这个路径必须是绝对路径

映射目录是一样的操作

[xiguatian@prod-server ~]$ mkdir javaapp
[xiguatian@prod-server ~]$ ls
a.txt  b.txt  javaapp
[xiguatian@prod-server ~]$ mv b.txt javaapp/
[xiguatian@prod-server ~]$ ls javaapp/
b.txt
[xiguatian@prod-server ~]$ ls
a.txt  javaapp
[xiguatian@prod-server ~]$ docker run -it -v /home/xiguatian/javaapp/:/tmp/ centos:7
[root@16ce1cfacae1 /]# ls /tmp/
b.txt
[root@16ce1cfacae1 /]# exit
exit
[xiguatian@prod-server ~]$ docker run -it -v /home/xiguatian/javaapp/:/tmp/javaapp centos:7
[root@df1162a57244 /]# ls /tmp/
javaapp  ks-script-V_wEqJ  yum.log
[root@df1162a57244 /]# ls /tmp/javaapp/
b.txt

6 后台运行和映射容器端口到本地端口

docker run  -d        -p      8000:80          nginx
       运行在后台    映射端口  本地端口:容器端口    镜像名称

示例:

[xiguatian@VM-0-11-centos ~]$ docker run -d -p 8000:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
d121f8d1c412: Pull complete
ebd81fc8c071: Pull complete
655316c160af: Pull complete
d15953c0e0f8: Pull complete
2ee525c5c3cc: Pull complete
Digest: sha256:c628b67d21744fce822d22fdcc0389f6bd763daac23a6b77147d0712ea7102d0
Status: Downloaded newer image for nginx:latest
4e13ff50aca75db6a7e554ecb609091b2da65df5ac7cc1bb64e21928d727a1c0

访问本地的 8000 端口

[xiguatian@VM-0-11-centos ~]$ curl -I 127.0.0.1:8000
HTTP/1.1 200 OK
Server: nginx/1.19.2
Date: Thu, 24 Sep 2020 13:05:27 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Aug 2020 14:50:35 GMT
Connection: keep-alive
ETag: "5f32b03b-264"
Accept-Ranges: bytes

7 启动容器时候给容器一个名字

docker run -itd    --name h1    centos:7
                     容器名称    镜像名称

在这里插入图片描述
之后再访问使用容器,就可以把 容器 ID更换成 容器名称了

[xiguatian@VM-0-11-centos ~]$ docker exec -it h1 bash
[root@9329acd400e3 /]#

8 删除已经停止的容器

docker  rm  容器 ID/容器名称

在这里插入图片描述

五、镜像操作

1 搜索镜像

[xiguatian@VM-0-11-centos ~]$ docker search redis --limit 2
NAME                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
redis               Redis is an open source key-value store that…   8592                [OK]
bitnami/redis       Bitnami Redis Docker Image                      161                                     [OK]
[xiguatian@VM-0-11-centos ~]$

2 下载镜像到本地

[xiguatian@VM-0-11-centos ~]$ docker pull redis
Using default tag: latest
latest: Pulling from library/redis
d121f8d1c412: Already exists
2f9874741855: Pull complete
d92da09ebfd4: Pull complete
bdfa64b72752: Pull complete
e748e6f663b9: Pull complete
eb1c8b66e2a1: Pull complete
Digest: sha256:1cfb205a988a9dae5f025c57b92e9643ec0e7ccff6e66bc639d8a5f95bba928c
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[xiguatian@VM-0-11-centos ~]$

3 删除本地镜像

先查看

[xiguatian@VM-0-11-centos ~]$ docker image  ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              84c5f6e03bf0        13 days ago         104MB
nginx               latest              7e4d58f0e5f3        2 weeks ago         133MB
centos              7                   7e6257c9f8d8        6 weeks ago         203MB
hello-world         latest              bf756fb1ae65        8 months ago        13.3kB

再删除

[xiguatian@VM-0-11-centos ~]$ docker image rm hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:4cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63

4 Dockerfile 制作镜像

定义 Dockerfile

FROM centos:7
RUN yum install -y \
    vim bash-com* openssh-clients openssh-server iproute cronie;\
    yum group install -y "Development Tools";yum clean all;\
    localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV LANG=zh_CN.UTF-8

构建Docker 镜像

[xiguatian@VM-0-11-centos ~]$ ls Dockerfile
Dockerfile

[xiguatian@VM-0-11-centos ~]$ docker build . -t centos7-sshd

  • -t 指定了构建后的镜像名称

六、docker-compose

Compose是用于定义和运行多容器Docker应用程序的工具。通过Compose,您可以使用YAML文件来配置应用程序的服务。然后,使用一个命令,就可以从配置中创建并启动所有服务。要了解有关Compose的所有功能的更多信息,请参阅功能列表。

Compose可在所有环境中工作:生产,登台,开发,测试以及CI工作流。您可以在“ 通用用例”中了解有关每种用例的更多信息。

使用Compose基本上是一个三步过程:

使用定义您的应用环境,Dockerfile以便可以在任何地方复制。

定义组成应用程序的服务,docker-compose.yml 以便它们可以在隔离的环境中一起运行。

Run docker-compose upand Compose启动并运行您的整个应用程序。

Compose 使用一个具有 YAML风格的文件来实现,这个文件一般叫做 docker-compose.yml 。

通过编写这个文件来定义一组相互之间有关联的应用容器。

概念
Compose 中有两个比较重要的概念: 服务service 和 项目 project。

服务 service
就是一个应用容器,实际上可以包含多个使用相同镜像运行的容器实例。

项目
就是包含了多个 service 的一个 docker-compose.yml 文件

1 安装 docker-compose

1.1 下载

 curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

1.2 修改为可执行权限

chmod +x /usr/local/bin/docker-compose

1.3 验证安装

[xiguatian@VM-0-11-centos ~]$ docker-compose version
docker-compose version 1.27.4, build 40524192
docker-py version: 4.3.1
CPython version: 3.7.7
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

1.4 子命令补全

先安装 BASH 自身的子命令补全软件包

yum install bash-completion
curl -L https://raw.githubusercontent.com/docker/compose/1.27.4/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose

测试是否下载成功

echo $?

之后退出并重新登录即可

[xiguatian@qq ~]$ docker-compose p  # 连续敲两次 Tab 键
pause  port   ps     pull   push
[xiguatian@qq ~]$ docker-compose p

2 简单使用

2.1 首先在一个指定的目录中编辑一个 docker-compose.yml 文件,

version: "3.8"                     # 指定要使用 docker-compose 的版本
services:                          # 声明服务
  host1:                           # 第一个服务的名称,可以设置多个服务
    image: "centos:7"              # 从这个镜像启动一个容器
    container_name: "host1"        # 容器启动后的名称
    stdin_open: true               # 相当于 docker run 的 -i    
    tty: true                      # 相当于 docker run 的 -t
    #command: /bin/bash            # 一开始执行的命令 
    networks:                      # 设置这个服务的网络信息
      centos_net:                  # 网络名称,需要在下面顶级的 networks 中声明
        ipv4_address: 172.16.1.10  # 指定一个静态的 IP 地址

networks:                          # 顶级 networks ,设置整个 docker-compose 使用的网络设备
  centos_net:                      # 网络设备名称
    driver: bridge                 # 网络设备是网桥,可以理解为是一个交换机设备
    ipam:                          # 顶层网络部分中相应的网络配置必须具有ipam块
      driver: default              # 采用的默认的网络模式
      config:                      # 下面是一个配置列表
        - subnet: 172.16.1.0/24    # 子网网络
          gateway: 172.16.1.1      # 网关

2.2 以后台的方式运行: up -d

注意:默认情况下,所有的 docker-compose 命令都必须在含有 docker-compose.yml 文件的目录下执行。
换句话说,执行 docker-compose 命令的时候,需要保证当前目录下有 docker-compose.yml 文件。

docker-compose up -d

2.3 列出当前 docker-compose 管理的所有的容器: ps

docker-compose ps

[xiguatian@qq centos7]$ docker-compose ps
Name     Command    State   Ports
---------------------------------
host1   /bin/bash   Up
[xiguatian@qq centos7]$

2.4 列出当前 docker-compose 管理的所有的服务: ps --services

docker-compose ps --services

[xiguatian@qq centos7]$ docker-compose ps --services
host1
[xiguatian@qq centos7]$

2.5 执行容器内的命令:exec

docker-compose exec 服务名 命令 [选项]

[xiguatian@qq centos7]$ docker-compose exec host1 hostname -i
172.16.1.10

执行容器内的 bash 命令,就会进入容器

[xiguatian@qq centos7]$ docker-compose exec host1 bash
[root@befd9a2727ed /]# cd
[root@befd9a2727ed ~]# hostname -i
172.16.1.10

执行容器内的 exit 命令,就会退出容器

[root@befd9a2727ed ~]# exit
exit
[xiguatian@qq centos7]$

2.6 停止/启动容器: stop

在不移除容器的情况下停止运行容器。
之后,可以使用“docker compose start”重新启动它们。

stop [options] [--] [SERVICE...]

[xiguatian@qq centos7]$ docker-compose stop host1
Stopping host1 ... done
[xiguatian@qq centos7]$
[xiguatian@qq centos7]$ docker-compose ps
Name     Command     State     Ports
------------------------------------
host1   /bin/bash   Exit 137
[xiguatian@qq centos7]$
[xiguatian@qq centos7]$ docker-compose start host1
Starting host1 ... done
[xiguatian@qq centos7]$
[xiguatian@qq centos7]$ docker-compose ps
Name     Command    State   Ports
---------------------------------
host1   /bin/bash   Up

3.4 移除/删除/销毁容器:down

down 子命令,用于处于 Up 状态的容器停止,并删除容器、网络、卷和映像。

默认情况下删除如下内容:

  • compose 文件中为服务定义的容器
  • compose 文件中顶级的 networks 定义的网络设备
  • 默认网络,如果使用了

放心,它不会删除 external 使用的外部网络和卷。

选项 -v 可以一同删除 compose 文件中定义的卷,默认是不删除的。

[xiguatian@qq centos7]$ docker-compose down
Stopping host1 ... done
Removing host1 ... done
Removing network centos7_centos_net

七、 docker-compose的容器编排

1 含有多台主机的 docker-compose.yml

1.1 定义一个 Dockerfile

先定义一个 Dockerfile,我们可以从这个 Dockerfile 中构建一个自定义的镜像,从而获取到一个我们自定义的容器。

FROM centos:7
RUN yum install -y \
    vim bash-com* openssh-clients openssh-server iproute cronie;\
    yum group install -y "Development Tools";yum clean all;\
    localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV LANG=zh_CN.UTF-8

在上面的 Dockerfile 中,安装的基本的软件,和 sshdcrond 服务。
会支持 ssh 远程连接、计划任务、ip 命令 ,ss 命令, 还有开发工具。

1.2 定义 docker-compose 文件

version: '3.8'
services:
  h1:
    build: .
    image: centos7-sshd
    privileged: true
    command: /usr/sbin/init
    hostname: h1.sharkyun.com
    networks:
      xiuyun_net:

  h2:
    image: centos7-sshd
    privileged: true
    hostname: h2.sharkyun.com
    command: /usr/sbin/init
    networks:
      xiuyun_net:
  h3:
    image: centos7-sshd
    hostname: h3.sharkyun.com
    privileged: true
    command: /usr/sbin/init
    networks:
      xiuyun_net:
networks:
  xiuyun_net:

2 构建并启动 docker-compose.yml 中的容器

docker-compose up -d

3 添加一个新的容器进入已有的 compose 项目中

3.1 编辑 compose 文件,并添加新的容器的声明

  h4:
    build: .
    image: centos7-sshd
    container_name: h4
    hostname: h4.sharkyun.com
    privileged: true
    command: /usr/sbin/init
    networks:
      xiuyun_net:
        ipv4_address: 172.16.2.40

下面是添加后的完整 docker-compose.yml 文件内容

version: '3.8'
services:
  h1:
    build: .
    image: centos7-sshd
    container_name: h1
    privileged: true
    command: /usr/sbin/init
    hostname: h1.sharkyun.com
    networks:
      xiuyun_net:
        ipv4_address: 172.16.2.10

  h2:
    build: .
    image: centos7-sshd
    container_name: h2
    privileged: true
    hostname: h2.sharkyun.com
    command: /usr/sbin/init
    networks:
      xiuyun_net:
        ipv4_address: 172.16.2.20
  h3:
    build: .
    image: centos7-sshd
    container_name: h3
    hostname: h3.sharkyun.com
    privileged: true
    command: /usr/sbin/init
    networks:
      xiuyun_net:
        ipv4_address: 172.16.2.30
  h4:
    build: .
    image: centos7-sshd
    container_name: h4
    hostname: h4.sharkyun.com
    privileged: true
    command: /usr/sbin/init
    networks:
      xiuyun_net:
        ipv4_address: 172.16.2.40
networks:
  xiuyun_net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.16.2.0/24
          gateway: 172.16.2.1

3.2 更新 compose 项目

每次修改完 docker-compose.yml 文件的内容后,只需要重新执行
docker-compose up -d 命令即可立即生效。

[xiguatian@qq centos7]$ docker-compose up -d
h3 is up-to-date
h2 is up-to-date
h1 is up-to-date
Creating h4 ... done

3.3 假如想再加入一个 nginx

version: '3.8'
services:
  h1:
    build: .
    image: centos7-sshd
    container_name: h1
    privileged: true
    command: /usr/sbin/init
    hostname: h1.sharkyun.com
    networks:
      xiuyun_net:
        ipv4_address: 172.16.2.10

  h2:
    build: .
    image: centos7-sshd
    container_name: h2
    privileged: true
    hostname: h2.sharkyun.com
    command: /usr/sbin/init
    networks:
      xiuyun_net:
        ipv4_address: 172.16.2.20
  h3:
    build: .
    image: centos7-sshd
    container_name: h3
    hostname: h3.sharkyun.com
    privileged: true
    command: /usr/sbin/init
    networks:
      xiuyun_net:
        ipv4_address: 172.16.2.30
  h4:
    build: .
    image: centos7-sshd
    container_name: h4
    hostname: h4.sharkyun.com
    privileged: true
    command: /usr/sbin/init
    networks:
      xiuyun_net:
        ipv4_address: 172.16.2.40
  web1:
    image: nginx
    container_name: web1
    hostname: web1
    networks:
      xiuyun_net:
        ipv4_address: 172.16.2.80

networks:
  xiuyun_net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.16.2.0/24
          gateway: 172.16.2.1

更新 compose 项目

[xiguatian@qq centos7]$ docker-compose up -d
h2 is up-to-date
h1 is up-to-date
h4 is up-to-date
h3 is up-to-date
Creating web1 ... done
[xiguatian@qq centos7]$

使用其中的一个容器访问 nginx 服务

[xiguatian@qq centos7]$ docker-compose exec h1 curl -I web1
HTTP/1.1 200 OK
Server: nginx/1.19.2
Date: Sat, 26 Sep 2020 06:40:04 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Aug 2020 14:50:35 GMT
Connection: keep-alive
ETag: "5f32b03b-264"
Accept-Ranges: bytes

猜你喜欢

转载自blog.csdn.net/qq_22648091/article/details/108781098
今日推荐