环境信息

CentOS Linux release 7.8.2003 (Core)

链接:https://pan.baidu.com/s/1EzNSL4YpiHc4Yz80yDwuVA
提取码:r222

$ cat /etc/redhat-release
$ cat /proc/version

Docker version 1.13.1, build 64e9980/1.13.1

$ docker --verison

配置网络

查看网卡配置

$ ip addr

配置网卡自启动

$ vi /etc/sysconfig/network-scripts/ifcfg-ens33

修改ONBOOT=yes

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=45166c14-a7e4-477a-b3dc-931692773e0f
DEVICE=ens33
ONBOOT=yes # 修改此处为yes

输入:wq保存并退出

重启网络服务

$ service network restart

ping百度验证网络连通性

$ ping www.baidu.com
PING www.a.shifen.com (14.215.177.39) 56(84) bytes of data.
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=1 ttl=47 time=22.7 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=2 ttl=47 time=22.4 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=3 ttl=47 time=22.1 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2007ms
rtt min/avg/max/mdev = 22.177/22.491/22.798/0.253 ms

配置yum

使用yum下载wget

$ yum install wget

删除yum默认源

$ rm -rf /etc/yum.repos.d/*

为yum配置阿里云源

$ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

刷新yum缓存使之生效

$ yum clean all     # 清除系统所有的yum缓存
$ yum makecache     # 生成yum缓存

Screen的配置与使用

使用yum安装screen

$ yum install screen

创建名为docker的会话,创建后自动进入该会话

$ screen -S docker

使用ctrl+a+d来暂离(detached)当前进入的会话

[detached from 2145.docker]

查看所有的screen会话状态

$ screen -ls
There are screens on:
    2231.nginx    (Attached)
    2145.docker    (Detached)
2 Sockets in /var/run/screen/S-root.

共享模式进入已经被Attached的会话

$ screen -x docker

从会话内部退出并关闭会话

exit

强制从主机关闭名为docker的会话,将会强行将所有连接的用户弹出

screen -S docker -X quit

Docker的安装与配置

查看是否已经安装docker

$ yum list installed | grep docker

卸载docker及其相关拓展

$ yum remove docker* # 其中*号为通配符

使用yum安装docker

$ yum install docker

查看docker服务状态

$ service docker status

启动docker服务

$ service docker start

设置docker服务开机自启动

$ systemctl enable docker

查看docker是否为开机自启动

$ systemctl list-unit-files | grep docker

配置docker源

$ vi /etc/docker/daemon.json

阿里云容器镜像服务页面中登陆并获取适配个人所在网络的加速地址,在文件中写入地址

{
  "registry-mirrors": ["https://81rkondt.mirror.aliyuncs.com"]
}

:wq保存并退出

重新生成系统依赖树并重启docker服务

$ systemctl daemon-reload
$ service docker restart

检查docerk配置

$ docker info

Play with Docker

https://labs.play-with-docker.com/

在docker源中搜索镜像

$ docker search httpd

下载docker镜像文件

$ docker pull httpd:latest

查看已下载的docker镜像文件

$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/httpd     latest              c39a868aad02        8 hours ago         133 MB

创建docker容器

$ docker run -itd --name httpd -p 81:80 docker.io/httpd

run命令详解

https://www.runoob.com/docker/docker-run-command.html

查看已创建的docker容器

$ docker ps -a

停止容器

$ docker stop httpd

删除容器

$ docker rm my_nginx

重启容器

$ docker restart httpd

使用yum安装telnet

$ yum install telnet

测试本机81端口

$ telnet localhost 81

远程测试81端口

http://10.101.53.65:81/

如若本机测试端口成功,远程测试失败,则可能为火墙导致,需在火墙开启对应端口

https://www.ihawo.com/archives/214.html

使用exec进入container可查看文件目录

$ docker exec -it httpd /bin/bash

Container容器与host宿主间拷贝文件

使用docker cp命令拷贝

自动覆盖同名文件、可重命名考出的文件

$ docker cp apache:/usr/local/apache2/htdocs/index.html /home/index.html

Container的备份与迁移

使用commit命令将container容器提交并合并到image镜像中

$ docker commit --author shxuai --message "backup for apache by shxuai in 20201110" apache apache_backup_20201110

查看生成的image镜像

$ docker images

将新生成的image镜像保存为tar文件

$ docker save -o /home/apache_backup_20201110.tar apache_backup_20201110

查看备份文件

$ cd /home; ll

导入备份文件

$ docker load -i /home/apache_backup_20201110.tar 

创建container容器

$ docker run -itd --name apache -p 80:80 apache_backup_20201110

浏览器访问验证

http://10.101.53.65:80

修改已经创建的container的运行参数

将上述nginx container的端口映射从81改为82

查看需要修改的container ID

$ docker ps -a

停止docker服务

$ service docker stop

使用yum安装vim

$ yum install vim

进入对应container的配置文件修改配置

$ vim /var/lib/docker/containers/{container ID}/hostconfig.json

PortBindings中的HostPort的值从81改为82,:wq保存并退出

{
    "Binds":null,
    "ContainerIDFile":"",
    "LogConfig":{
        "Type":"journald",
        "Config":{

        }
    },
    "NetworkMode":"default",
    "PortBindings":{
        "80/tcp":[
            {
                "HostIp":"",
                "HostPort":"81"
            }
        ]
    },
    "RestartPolicy":{
        "Name":"no",
        "MaximumRetryCount":0
    },
    "AutoRemove":false,
    "VolumeDriver":"",
    "VolumesFrom":null,
    "CapAdd":null,
    "CapDrop":null,
    "Dns":[

    ],
    "DnsOptions":[

    ],
    "DnsSearch":[

    ],
    "ExtraHosts":null,
    "GroupAdd":null,
    "IpcMode":"",
    "Cgroup":"",
    "Links":[

    ],
    "OomScoreAdj":0,
    "PidMode":"",
    "Privileged":false,
    "PublishAllPorts":false,
    "ReadonlyRootfs":false,
    "SecurityOpt":null,
    "UTSMode":"",
    "UsernsMode":"",
    "ShmSize":67108864,
    "Runtime":"docker-runc",
    "ConsoleSize":[
        0,
        0
    ],
    "Isolation":"",
    "CpuShares":0,
    "Memory":0,
    "NanoCpus":0,
    "CgroupParent":"",
    "BlkioWeight":0,
    "BlkioWeightDevice":null,
    "BlkioDeviceReadBps":null,
    "BlkioDeviceWriteBps":null,
    "BlkioDeviceReadIOps":null,
    "BlkioDeviceWriteIOps":null,
    "CpuPeriod":0,
    "CpuQuota":0,
    "CpuRealtimePeriod":0,
    "CpuRealtimeRuntime":0,
    "CpusetCpus":"",
    "CpusetMems":"",
    "Devices":[

    ],
    "DiskQuota":0,
    "KernelMemory":0,
    "MemoryReservation":0,
    "MemorySwap":0,
    "MemorySwappiness":-1,
    "OomKillDisable":false,
    "PidsLimit":0,
    "Ulimits":null,
    "CpuCount":0,
    "CpuPercent":0,
    "IOMaximumIOps":0,
    "IOMaximumBandwidth":0
}

重启docker服务

$ service docker restart

重启container

$ docker start my_nginx

查看端口映射

$ docker ps -a
最后修改:2020 年 12 月 23 日
如果觉得我的文章对你有用,请随意赞赏