docker

一:docker安装

1,root账户登录,查看内核版本如下

[root@xpq ~]# uname -a
Linux xpq 3.10.0-1127.18.2.el7.x86_64 #1 SMP Sun Jul 26 15:27:06 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@xpq ~]# 

2,(可选)把yum包更新到最新(生产环境慎重!yum update会对软件包和内核升级,此处只是为了排除系统环境的影响,来自笔者的备注)

[root@localhost ~]# yum update
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * updates: centos.ustc.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 bind-libs.x86_64.32.9.9.4-61.el7 将被 升级
---> 软件包 bind-libs.x86_64.32.9.9.4-61.el7_5.1 将被 更新
---> 软件包 bind-libs-lite.x86_64.32.9.9.4-61.el7 将被 升级
---> 软件包 bind-libs-lite.x86_64.32.9.9.4-61.el7_5.1 将被 更新
---> 软件包 bind-license.noarch.32.9.9.4-61.el7 将被 升级
---> 软件包 bind-license.noarch.32.9.9.4-61.el7_5.1 将被 更新
...
...
验证中 : 32:bind-license-9.9.4-61.el7.noarch 8/8

更新完毕:
bind-libs.x86_64 32:9.9.4-61.el7_5.1
bind-libs-lite.x86_64 32:9.9.4-61.el7_5.1
bind-license.noarch 32:9.9.4-61.el7_5.1
bind-utils.x86_64 32:9.9.4-61.el7_5.1

完毕!
[root@localhost ~]#

3,安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的

[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * updates: centos.ustc.edu.cn

4,设置yum源

[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
已加载插件:fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

5,可以查看所有仓库中所有docker版本,并选择特定版本安装

[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
已加载插件:fastestmirror, langpacks
可安装的软件包
 * updates: centos.ustc.edu.cn
Loading mirror speeds from cached hostfile
 * extras: mirrors.aliyun.com
docker-ce.x86_64            18.06.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos  

6,安装Docker,命令:yum install docker-ce-版本号,我选的是17.12.1.ce,如下

[root@localhost ~]# yum install docker-ce-17.12.1.ce
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: mirrors.aliyun.com
 * updates: centos.ustc.edu.cn
base                                                   | 3.6 kB     00:00     
docker-ce-stable                                       | 2.9 kB     00:00     
extras                                                 | 3.4 kB     00:00     
updates                                                | 3.4 kB     00:00     
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker-ce.x86_64.0.17.12.1.ce-1.el7.centos 将被 安装
--> 正在处理依赖关系 container-selinux >= 2.9,它被软件包 docker-ce-17.12.1.ce-1.el7.centos.x86_64 需要
...

7, 启动Docker,命令:systemctl start docker,然后加入开机启动,如下

[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

8,验证安装是否成功(有client和service两部分表示docker安装启动都成功了)

[root@localhost ~]# docker version 
Client:
 Version:    17.12.1-ce
 API version:    1.35
 Go version:    go1.9.4
 Git commit:    7390fc6
 Built:    Tue Feb 27 22:15:20 2018
 OS/Arch:    linux/amd64

Server:
 Engine:
  Version:    17.12.1-ce
  API version:    1.35 (minimum version 1.12)
  Go version:    go1.9.4
  Git commit:    7390fc6
  Built:    Tue Feb 27 22:17:54 2018
  OS/Arch:    linux/amd64
  Experimental:    false

9:docker 的安装目录

[root@xpq docker]# pwd 
/etc/docker
[root@xpq docker]# ll
total 4
-rw------- 1 root root 246 Jul 30 23:00 key.json
[root@xpq docker]# 

10:docker的资源目录

[root@xpq docker]# pwd
/var/lib/docker
[root@xpq docker]# ll
total 56
drwx------  2 root root  4096 Aug 15 18:56 builder
drwx--x--x  3 root root  4096 Aug 15 18:56 containerd
drwx------  6 root root  4096 Sep 17 16:04 containers
drwx------  3 root root  4096 Aug 15 18:56 image
drwxr-x---  3 root root  4096 Aug 15 18:56 network
drwx------ 67 root root 12288 Oct 11 20:32 overlay2
drwx------  4 root root  4096 Aug 15 18:56 plugins
drwx------  2 root root  4096 Oct 11 20:32 runtimes
drwx------  2 root root  4096 Aug 15 18:56 swarm
drwx------  2 root root  4096 Oct 11 20:32 tmp
drwx------  2 root root  4096 Aug 15 18:56 trust
drwx------ 17 root root  4096 Sep 17 16:04 volumes
[root@xpq docker]# 

11:加速器的配置

#配置加速器,指定国内的镜像
[root@xpq docker]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://74f21445.m.daocloud.io
docker version >= 1.12
{"registry-mirrors": ["http://74f21445.m.daocloud.io"]}
Success.
You need to restart docker to take effect: sudo systemctl restart docker 

[root@xpq docker]# cd /etc/docker/
#切花配置目录
[root@xpq docker]# ll
total 8
-rw-r--r-- 1 root root  56 Oct 14 15:32 daemon.json
-rw------- 1 root root 246 Jul 30 23:00 key.json
#查看配置文件
[root@xpq docker]# cat daemon.json 
{"registry-mirrors": ["http://74f21445.m.daocloud.io"]}
[root@xpq docker]# 

12:重启

[root@xpq docker]# systemctl restart docker

 

13:卸载:

  1、查询docker安装过的包:

    yum list installed | grep docker

       

  2、删除安装包:

   yum remove docker-ce.x86_64 ddocker-ce-cli.x86_64 -y

  3、删除镜像/容器等

    rm -rf /var/lib/docker

 二:Docker镜像操作

所有镜像的操作

[root@xpq docker]# docker image

Usage:    docker image COMMAND

Manage images

Options:


Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.

搜索镜像资源

[root@xpq docker]# docker search django

 

已存在镜像列表

[root@xpq ~]# docker image ls
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
delron/elasticsearch-ik   2.4.6-1.0           095b6487fb77        2 years ago         689MB
[root@xpq ~]# 

* REPOSITORY:镜像所在的仓库名称 
* TAG:镜像标签 
* IMAGEID:镜像ID 
* CREATED:镜像的创建日期(不是获取该镜像的日期) 
* SIZE:镜像大小

从仓库拉取镜像

[root@xpq ~]# docker  pull morunchang/fastdfs

 docker image pull 镜像名称 或者 sudo docker image pull library/镜像名称

删除镜像

docker image rm 镜像名或镜像ID

镜像资源的重命名

[root@xpq ~]# docker tag ubuntu:latest ubuntu1.1:latest 
[root@xpq ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu1.1           latest              9140108b62dc        2 weeks ago         72.9MB
ubuntu              latest              9140108b62dc        2 weeks ago         72.9MB
mysql               5.7                 ef08065b0a30        4 weeks ago         448MB
elasticsearch       latest              243f46a95d29        7 weeks ago         689MB
elasticsearch_tmp   latest              d423d9677f6d        8 weeks ago         689MB
mysql               5.7.22              0d16d0a97dd1        2 years ago         372MB
delron/fastdfs      latest              8487e86fc6ee        2 years ago         464MB
[root@xpq ~]# 

镜像资源的打包和导入

#打包Ubuntu镜像
[root@xpq ~]# docker save -o ubuntu.tar ubuntu
#查看打包文件
[root@xpq ~]# ll
total 73520
-rw------- 1 root root 75282944 Oct 14 15:53 ubuntu.tar
#删除原来的打包文件
[root@xpq ~]# docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:bc2f7250f69267c9c6b66d7b6a81a54d3878bb85f1ebb5f951c896d13e6ba537
#查看镜像列表
root@xpq ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu1.1           latest              9140108b62dc        2 weeks ago         72.9MB
mysql               5.7                 ef08065b0a30        4 weeks ago         448MB
elasticsearch       latest              243f46a95d29        7 weeks ago         689MB
elasticsearch_tmp   latest              d423d9677f6d        8 weeks ago         689MB
mysql               5.7.22              0d16d0a97dd1        2 years ago         372MB
delron/fastdfs      latest              8487e86fc6ee        2 years ago         464MB
#导入打包好的镜像
[root@xpq ~]# docker load -i ubuntu.tar
Loaded image: ubuntu:latest

[root@xpq ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu1.1           latest              9140108b62dc        2 weeks ago         72.9MB
ubuntu              latest              9140108b62dc        2 weeks ago         72.9MB
mysql               5.7                 ef08065b0a30        4 weeks ago         448MB
elasticsearch       latest              243f46a95d29        7 weeks ago         689MB
elasticsearch_tmp   latest              d423d9677f6d        8 weeks ago         689MB
mysql               5.7.22              0d16d0a97dd1        2 years ago         372MB
delron/fastdfs      latest              8487e86fc6ee        2 years ago         464MB
[root@xpq ~]# 

查看镜像资源的历史操作

[root@xpq ~]# docker image history ubuntu
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
9140108b62dc        2 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           2 weeks ago         /bin/sh -c mkdir -p /run/systemd && echo 'do…   7B                  
<missing>           2 weeks ago         /bin/sh -c [ -z "$(apt-get indextargets)" ]     0B                  
<missing>           2 weeks ago         /bin/sh -c set -xe   && echo '#!/bin/sh' > /…   811B                
<missing>           2 weeks ago         /bin/sh -c #(nop) ADD file:da80f59399481ffc3…   72.9MB              
[root@xpq ~]# 

镜像资源的详情信息

[root@xpq ~]# docker image inspect ubuntu
[
    {
        "Id": "sha256:9140108b62dc87d9b278bb0d4fd6a3e44c2959646eb966b86531306faa81b09b",
        "RepoTags": [
            "ubuntu1.1:latest",
            "ubuntu:latest"
        ],
        "RepoDigests": [],
        "Parent": "",
        "Comment": "",
        "Created": "2020-09-25T22:34:30.295807036Z",
        "Container": "1046a5d685aef5c37d1829040ca8083b94e4c069ca4963f4b16a6ade2e077b06",
        "ContainerConfig": {
            "Hostname": "1046a5d685ae",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:4ff2090064e7e38688bce713d50f3202d227b3c89fecea1434271c912ccd47e0",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "18.09.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:4ff2090064e7e38688bce713d50f3202d227b3c89fecea1434271c912ccd47e0",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 72875723,
        "VirtualSize": 72875723,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/0b3e2be05bc0f8f2b6ed7688fc0d433f08ab74545b52cf5c22a29e9134c163f5/diff:/var/lib/docker/overlay2/c4fa0e259817c395ad260282e4ca40675d93fdc6d937bb8a95fe1d992b37b8fe/diff",
                "MergedDir": "/var/lib/docker/overlay2/9851082dfdaab43d03d744b76bef5d208c2a875d55da9960637e76bac70dfb36/merged",
                "UpperDir": "/var/lib/docker/overlay2/9851082dfdaab43d03d744b76bef5d208c2a875d55da9960637e76bac70dfb36/diff",
                "WorkDir": "/var/lib/docker/overlay2/9851082dfdaab43d03d744b76bef5d208c2a875d55da9960637e76bac70dfb36/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:d42a4fdf4b2ae8662ff2ca1b695eae571c652a62973c1beb81a296a4f4263d92",
                "sha256:90ac32a0d9ab11e7745283f3051e990054616d631812ac63e324c1a36d2677f5",
                "sha256:782f5f011ddaf2a0bfd38cc2ccabd634095d6e35c8034302d788423f486bb177"
            ]
        },
        "Metadata": {
            "LastTagTime": "2020-10-14T15:51:42.698643781+08:00"
        }
    }
]
[root@xpq ~]# 
View Code

 

三:Docker容器操作

0:查看docker容器的所有命令

[root@xpq ~]# docker container 

Usage:    docker container COMMAND

Manage containers

Options:


Commands:
  attach      Attach local standard input, output, and error streams 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 to files or directories 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

Run 'docker container COMMAND --help' for more information on a command.
[root@xpq ~]# 

 

1.容器列表

# 查看正在运行的容器
[root@xpq ~]# docker container ls
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS              PORTS               NAMES
f488cef7af3b        delron/elasticsearch-ik:2.4.6-1.0   "/docker-entrypoint.…"   40 hours ago        Up 40 hours                             elasticsearch
# 查看所有的容器
[root@xpq ~]# docker container ls --all
CONTAINER ID        IMAGE                               COMMAND                  CREATED             STATUS                   PORTS               NAMES
f488cef7af3b        delron/elasticsearch-ik:2.4.6-1.0   "/docker-entrypoint.…"   40 hours ago        Up 40 hours                                  elasticsearch
a51f7dd3d414        bf756fb1ae65                        "/hello"                 2 weeks ago         Exited (0) 2 weeks ago                       exciting_babbage
[root@xpq ~]# 

2.运行容器

docker run [option] 镜像名 [向启动容器中传入的命令]

常用可选参数说明:
* -i 表示以《交互模式》运行容器。
* -t 表示容器启动后会进入其命令行。加入这两个参数后,容器创建就能登录进去。即分配一个伪终端。
* --name 为创建的容器命名。
* -v 表示目录映射关系,即宿主机目录:容器中目录。注意:最好做目录映射,在宿主机上做修改,然后共享到容器上。 
* -d 会创建一个守护式容器在后台运行(这样创建容器后不会自动登录容器)。 
* -p 表示端口映射,即宿主机端口:容器中端口。
* --network=host 表示将主机的网络环境映射到容器中,使容器的网络与主机相同。

3.交互式容器


docker run -it --name=ubuntu1 ubuntu /bin/bash

在容器中可以随意执行linux命令,就是一个ubuntu的环境。
当执行 exit 命令退出时,该容器随之停止。

4.守护式容器

# 开启守护式容器
$ sudo docker run -dit --name=ubuntu2 ubuntu

# 进入到容器内部交互环境
$ sudo docker exec -it 容器名或容器id 进入后执行的第一个命令
$ sudo docker exec -it ubuntu2 /bin/bash

如果对于一个需要长期运行的容器来说,我们可以创建一个守护式容器。
在容器内部执行 exit 命令退出时,该容器也随之停止。

 

5.停止和启动容器

# 停止容器
$ sudo docker container stop 容器名或容器id
# kill掉容器
$ sudo docker container kill 容器名或容器id
# 启动容器
$ sudo docker container start 容器名或容器id

 

6.删除容器

  • 正在运行的容器无法直接删除。
$ sudo docker container rm 容器名或容器id

7.容器制作成镜像

  • 配置完成的环境可以重复利用,我们可以将容器制作成镜像。
# 将容器制作成镜像
$ sudo docker commit 容器名 镜像名
# 镜像打包备份
$ sudo docker save -o 保存的文件名 镜像名

# 镜像解压
$ sudo docker load -i 文件路径/备份文件
 

 

9:进入容器

View Code

 

10:退出(ctrl+d)

四:docker部署流程

1:启动Ubuntu容器(运行一个系统的镜像资源)

2:对从纯净的Ubuntu系统更新软件源(更新系统的软件园)

root@8f1e63e7abfc:/# apt update

3:在系统的容器中配置项目的依赖环境

eg:
1:安装pip
root@8f1e63e7abfc:/# apt install python3-pip

  2:安装django

root@8f1e63e7abfc:/# pip3 install django==1.11.11
3:安装python
root@8f1e63e7abfc:/home/meiduo# apt install python3

4:将项目上传到系统容器中

5:将系统容器压缩打包成新的镜像资源

6:将镜像资源打包成压缩包

7:将压缩包上传到线上服务器

8:线上服务器安装docker

9:将压缩包的资源进行导入启动

五:仓库管理

1:仓库是什么?

  • 仓库就类似于我们在网上搜索操作系统光盘的一个镜像站。
  • 这里的仓库指的是Docker镜像存储的地方。

 

2:Docker的仓库有三大类:

 

  • 公有仓库:Docker hubDocker cloud、等
  • 私有仓库:registryharbor
  • 本地仓库:在当前主机存储镜像的地方。 

3:私有仓库的搭建

下载registry镜像

docker pull registry

配置容器权限

vim /etc/docker/daemon.json

{"registry-mirrors": ["http://74f21445.m.daocloud.io"], "insecure-registries": ["192.168.8.14:5000"]}

注意:

  • 私有仓库的ip地址是宿主机的ip,而且ip两侧有双引号

 

重启docker服务

 

systemctl restart docker
systemctl status docker

启动私有仓库

[root@xpq docker]# docker run -d registry
20d9dcdb53faefe30d4156982ab13072bd60f19e7b8b7ceb6e31974531d68d0b
[root@xpq docker]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
20d9dcdb53fa        registry            "/entrypoint.sh /etc…"   5 seconds ago       Up 4 seconds        5000/tcp            compassionate_joliot
[root@xpq docker]# 

上传私镜像到私有仓库与下载

#标记
[root@xpq docker]# docker tag ubuntu1.1 49.232.56.143:5000/ubuntu
#查看
[root@xpq docker]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
49.232.56.143:5000/ubuntu   latest              9140108b62dc        2 weeks ago         72.9MB
ubuntu1.1                   latest              9140108b62dc        2 weeks ago         72.9MB
ubuntu                      latest              9140108b62dc        2 weeks ago         72.9MB
mysql                       5.7                 ef08065b0a30        5 weeks ago         448MB
elasticsearch               latest              243f46a95d29        7 weeks ago         689MB
elasticsearch_tmp           latest              d423d9677f6d        2 months ago        689MB
registry                    latest              2d4f4b5309b1        3 months ago        26.2MB
mysql                       5.7.22              0d16d0a97dd1        2 years ago         372MB
delron/fastdfs              latest              8487e86fc6ee        2 years ago         464MB
#上传
[root@xpq docker]# docker push 49.232.56.143:5000/ubuntu
The push refers to repository [49.232.56.143:5000/ubuntu]
782f5f011dda: Pushed 
90ac32a0d9ab: Pushed 
d42a4fdf4b2a: Pushed 
latest: digest: sha256:2e70e9c81838224b5311970dbf7ed16802fbfe19e7a70b3cbfa3d7522aa285b4 size: 943


#查看
[root@xpq docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              9140108b62dc        2 weeks ago         72.9MB
ubuntu1.1           latest              9140108b62dc        2 weeks ago         72.9MB
mysql               5.7                 ef08065b0a30        5 weeks ago         448MB
elasticsearch       latest              243f46a95d29        7 weeks ago         689MB
elasticsearch_tmp   latest              d423d9677f6d        2 months ago        689MB
registry            latest              2d4f4b5309b1        3 months ago        26.2MB
mysql               5.7.22              0d16d0a97dd1        2 years ago         372MB
delron/fastdfs      latest              8487e86fc6ee        2 years ago         464MB
#下载

[root@xpq docker]# docker pull 49.232.56.143:5000/ubuntu
Using default tag: latest
latest: Pulling from ubuntu
Digest: sha256:2e70e9c81838224b5311970dbf7ed16802fbfe19e7a70b3cbfa3d7522aa285b4
Status: Downloaded newer image for 49.232.56.143:5000/ubuntu:latest
#查看

[root@xpq docker]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
49.232.56.143:5000/ubuntu   latest              9140108b62dc        2 weeks ago         72.9MB
ubuntu1.1                   latest              9140108b62dc        2 weeks ago         72.9MB
ubuntu                      latest              9140108b62dc        2 weeks ago         72.9MB
mysql                       5.7                 ef08065b0a30        5 weeks ago         448MB
elasticsearch               latest              243f46a95d29        7 weeks ago         689MB
elasticsearch_tmp           latest              d423d9677f6d        2 months ago        689MB
registry                    latest              2d4f4b5309b1        3 months ago        26.2MB
mysql                       5.7.22              0d16d0a97dd1        2 years ago         372MB
delron/fastdfs              latest              8487e86fc6ee        2 years ago         464MB
[root@xpq docker]# 

 六:数据卷的操作

什么是数据卷

就是将宿主机的某个目录,映射到容器中,作为数据存储的目录,我们就可以在宿主机对数据进行存储

1:映射数据卷的命令格式:

docker run -itd --name [容器名字] -v [宿主机目录]:[容器目录] [镜像名称] [命令(可选)]

 

#宿主机中
root@xpq data]# docker run -itd -v /home/data:/home/ ubuntu
bc270c6d1c7f4ad34ad5406a22876b26cbff936619b85a199b34347fbcf50d79
[root@xpq data]# pwd
/home/data
[root@xpq data]# ll
total 0
-rw-r--r-- 1 root root 0 Oct 16 08:33 aa.txt
[root@xpq data]# 
#容器中
[root@xpq ~]# docker exec -it  bc270c6d1c7f  /bin/bash
root@bc270c6d1c7f:/# cd /home/
root@bc270c6d1c7f:/home# ll
total 8
drwxr-xr-x 2 root root 4096 Oct 16 00:33 ./
drwxr-xr-x 1 root root 4096 Oct 16 00:37 ../
-rw-r--r-- 1 root root    0 Oct 16 00:33 aa.txt
root@bc270c6d1c7f:/home# 

 

注意:数据卷是宿主机和容器映射的数据目录,多个容器映射同一个宿主机数据目录,他们之间是共享的。

2:数据卷容器操作

什么是数据卷容器?

将宿主机的某个目录,使用容器的方式来表示,然后其他的应用容器将数据保存在这个容器中,达到大批量应用数据同时存储的目的

1:创建数据卷容器

命令格式:

docker create -v [容器数据卷目录] --name [容器名字] [镜像名称] [命令(可选)]

 

[root@xpq /]# docker create -v /data --name myuuu ubuntu
43740f4addbcee78901bde6914c9e3dfd409940ca4bdc17db96da2b94095ddd8
[root@xpq /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS                                         NAMES
43740f4addbc        ubuntu              "/bin/bash"              3 minutes ago       Created                                                                 myuuu
bc270c6d1c7f        ubuntu              "/bin/bash"              22 minutes ago      Up 22 minutes                                                           thirsty_mccarthy
8cf846e70168        registry            "/entrypoint.sh /etc…"   9 hours ago         Up 9 hours                                                              blissful_lamport
20d9dcdb53fa        registry            "/entrypoint.sh /etc…"   10 hours ago        Exited (2) 9 hours ago 

 

 

2:创建两个容器,把容器挂载到数据卷容器中

 

命令格式:

 

docker run --volumes-from [数据卷容器id/name] -tid --name [容器名字] [镜像名称] [命令(可选)]

 

[root@xpq /]# docker run --volumes-from myuuu -tid  --name myaaa ubuntu /bin/bash
49125f9a153f61e181ff6d15d7baa9d6304916e849a461a610e09bd696b0ec29
[root@xpq /]# docker run --volumes-from myuuu -tid  --name mybbb ubuntu /bin/bash
b8de5ddd9c71708f97bf39f1cae65f4b1dabab2097e23921be20675e31360e68
[root@xpq /]#

 

确保数据共享

#第一个容器
[root@xpq /]# docker exec -it myaaa /bin/bash
root@49125f9a153f:/data# mkdir a
root@49125f9a153f:/data# ll      
total 12
drwxr-xr-x 3 root root 4096 Oct 16 01:13 ./
drwxr-xr-x 1 root root 4096 Oct 16 01:07 ../
drwxr-xr-x 2 root root 4096 Oct 16 01:13 a/
root@49125f9a153f:/data# 
#第二个容器
[root@xpq ~]# docker exec -it  bc270c6d1c7f  /bin/bash
root@b8de5ddd9c71:/# cd data
oot@b8de5ddd9c71:/data# ll
total 12
drwxr-xr-x 3 root root 4096 Oct 16 01:13 ./
drwxr-xr-x 1 root root 4096 Oct 16 01:07 ../
drwxr-xr-x 2 root root 4096 Oct 16 01:13 a/
root@b8de5ddd9c71:/data# 

结果证明:

  • 容器间可以共享数据卷你容器,不过数据是保存在数据卷内,并没有保存到宿主机的文件目录中

 3:数据卷操作

#查看数据卷
[root@xpq _data]# docker volume 

Usage:    docker volume COMMAND

Manage volumes

Options:


Commands:
  create      Create a volume
  inspect     Display detailed information on one or more volumes
  ls          List volumes
  prune       Remove all unused volumes
  rm          Remove one or more volumes

Run 'docker volume COMMAND --help' for more information on a command.
[root@xpq _data]# docker volume ls
DRIVER              VOLUME NAME
local               1781d93cf6f5df41d235f29a258aceb26e2c8afd79dcf6fa450992b6aa8ecce9
local               1be71aab6f14a777abaec4009e19c567c5308ab95dc95c78c59c87d3900df91a
local               3b40be56b4ba338ba96079dfee5ebca56703df024bd16eb8cfcd7dccb4d1eecb
local               3e56058fb9645e2baf9e8a72a8d987d5bde22c7e1529e10fecf5d235ee28dc5b
local               57213a6111bcc33d0adbfef70c5f89635fbcc16b05ec29ddd4c780331d72ad64
local               57895cbe7e8f2eeb58f75c23aaed922e794ba498256379246de0718063313c35
local               5d7cac1b7b56d170228c9b18c5220382c86581779aee7f6e05a08844278b4bc5
local               775bfce7d72a67487d34a4a762d09412e1f36168046dbb7c160465824b281497
local               8212f33b2ce0eb02ad489e10ea51914ff213b2c089dd968f56d20e54ed9c17e8
local               82db895381438ee4ba6ab65ebbfc7084c0bc1d2f0b447cde1fa0ab7c7bb67326
local               8898f6cc3e04b87e4ede5f49a65bd0a8a079371214d6223d4761d6afdd6c4829
local               9070b079073f17165cd0adab163f127dd8ad5c29bdf557a7ac5ac7bb218dcfad
local               a6d9be2644507f98fe338e58f6711abc37f26f1ed97f1024b26b2dc4bd07630c
local               aca7763007799f6295518d2579afabcb034de1638615592ba8e11bf5fa995b04
local               b216e485a835f96cbf01aded8273765533519e062ed7ff6462fcf7842ab38b1f
local               bb164e3d5e59e451675f765f170f7728d1223cfb64aea93b5661e18d9090f268
local               bdec71803b8419560315c7c67b601d02f629e62c6843f12142b5b79228eeb5cf
local               c163afd0a7841a65a6e6ab472c531ab84e589b6cf1c5e36ae7fa35976bd5371b
[root@xpq _data]# 


#删除数据卷
[root@xpq _data]# docker volume  rm 0a405f4f7fae888f95aeedf8571f2495554599d658acb8a47ede67ae2d5157e0


#查看数据卷的详情
[root@xpq _data]# docker volume inspect 1781d93cf6f5df41d235f29a258aceb26e2c8afd79dcf6fa450992b6aa8ecce9
#配置文件中可查看容器映射到宿主机中目录
[root@xpq _data]# pwd
/var/lib/docker/volumes/8898f6cc3e04b87e4ede5f49a65bd0a8a079371214d6223d4761d6afdd6c4829/_data
[root@xpq _data]# ls
client.conf         http.conf   mod_fastdfs.conf  storage.conf.sample  storage_ids.conf.sample  tracker.conf.sample
client.conf.sample  mime.types  storage.conf      storage_ids.conf     tracker.conf
[root@xpq _data]# 
View Code

 

七:网络管理

端口映射简介

默认情况下,容器和宿主机之间网络是隔离的,我们可以通过端口映射的方式,将容器中的端口,映射到宿主机的某个端口上。这样我们就可以通过 宿主机的ip+port的方式来访问容器里的内容

 

端口映射种类

1、随机映射 -P(大写)

2、指定映射     -p 宿主机端口:容器端口

 

指定端口映射

 

命令格式:

 

docker run -d -p [宿主机ip]:[宿主机端口]:[容器端口] --name [容器名字] [镜像名称]

 

注意:

 

如果不指定宿主机ip的话,默认使用 0.0.0.0

 

容器端口必须清楚,而且必须写出来

 

 八:Dockerfile

 

 

什么是Dockerfile

Dockerfile类似于我们学习过的脚本,将我们在上面学到的docker镜像,使用自动化的方式实现出来。

 

Dockerfile的作用

1、找一个镜像:    ubuntu

2、创建一个容器: docker run  ubuntu

3、进入容器: docker exec -it 容器 命令

4、操作: 各种应用配置

....

5、构造新镜像: docker commit   

 

Dockerfile 使用准则

1、大: 首字母必须大写D

2空: 尽量将Dockerfile放在空目录中。

3单: 每个容器尽量只有一个功能。

4少: 执行的命令越少越好。

 

Dockerfile 的使用

1:定制Dockerfile文件

[root@xpq docker]# pwd 
/docker
[root@xpq docker]# ll
total 8
drwxr-xr-x 2 root root 4096 Oct 20 13:45 data
-rw-r--r-- 1 root root  687 Oct 20 14:50 Dockerfile
[root@xpq docker]# 

2:dockerfile 文件中的指令阐述

[root@xpq docker]# cat Dockerfile 
#启动一个镜像资源
FROM ubuntu2.0
#将宿主机中的文件或目录拷贝到容器当中,拷贝压缩包的时候不会解包
#ADD ./data /home
#copy 将拷贝的压缩包不进行解压
#COPY  ./data/packages.tar.gz  /home
#ADD 将拷贝的内容进行解压
#ADD  ./data/packages.tar.gz  /home

#在祁东的容器中执行安装命令
#RUN apt-get update &&  apt-get install python3-pip &&  pip3 install /home/packages.txt
#指定环境变量
#ENV name=python
#指定进入容器的目录
#WORKDIR /home/django
WORKDIR /home
RUN django-admin startproject meiduo
WORKDIR /home/meiduo
#开放端口
EXPOSE 8000
#执行命令
ENTRYPOINT python3 manage.py runserver 0.0.0.0:8000

 

3:构建镜像

命令格式

docker  build -t  镜像名   dockerfile文件目录

[root@xpq docker]# docker build -t xpq .

4:启动容器查看效果

[root@xpq docker]# docker run -it --name ubuntu xpq

 

 

 

 

 

 

 

posted on 2020-10-14 16:04  paike123  阅读(201)  评论(0编辑  收藏  举报

导航