Centos7上在线安装docker的方法
来源地址:
https://www.cnblogs.com/yufeng218/p/8370670.html
https://blog.csdn.net/lvhao2813/article/details/80168422
Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE。
社区版是免费提供给个人开发者和小型团体使用的,企业版会提供额外的收费服务,比如经过官方测试认证过的基础设施、容器、插件等。
社区版按照stable和edge两种方式发布,每个季度更新stable版本,如17.06,17.09;每个月份更新edge版本,如17.09,17.10。
一、安装docker
1、Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。
通过 uname -r 命令查看你当前的内核版本
uname -r
3.10.0-514.el7.x86_64
2、使用 root 权限登录 Centos。确保 yum 包更新到最新。
sudo yum update
3、卸载旧版本(如果安装过旧版本的话)
sudo yum remove docker docker-common docker-selinux docker-engine
4、安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
5、设置yum源
//国外yum源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
//国内yum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
6、可以查看所有仓库中所有docker版本,并选择特定版本安装
yum list docker-ce --showduplicates | sort -r
[root@testpc ~]# yum list docker-ce --showduplicates|sort -r
已加载插件:fastestmirror
可安装的软件包
* updates: mirrors.huaweicloud.com
Loading mirror speeds from cached hostfile
* extras: mirrors.huaweicloud.com
docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable
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 docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
* base: mirrors.aliyun.com
最新安装为 docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable
7、安装docker
#由于repo中默认只开启stable仓库,故这里安装的是最新稳定版3:18.09.6-3.el7
sudo yum install docker-ce
8、启动并加入开机启动
sudo systemctl start docker
sudo systemctl enable docker
9、验证安装是否成功(有client和service两部分表示docker安装启动都成功了)
docker version
[root@testpc ~]# docker version
Client:
Version: 18.09.6
API version: 1.39
Go version: go1.10.8
Git commit: 481bc77156
Built: Sat May 4 02:34:58 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.6
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 481bc77
Built: Sat May 4 02:02:43 2019
OS/Arch: linux/amd64
Experimental: false
二、问题
1、因为之前已经安装过旧版本的docker,在安装的时候报错如下:
Transaction check error:
file /usr/bin/docker from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
file /usr/bin/docker-containerd from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
file /usr/bin/docker-containerd-shim from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
file /usr/bin/dockerd from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
2、卸载旧版本的包
sudo yum erase docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
3、再次安装docker
sudo yum install docker-ce
三、docker的使用
client是docker客户端。
images是docker镜像
containers是容器
registry是集中存储分发镜像的服务。最常用的Docker Registry是官方的Docker Hub
通过docker images来查看我们有哪些镜像
docker images
[root@testpc ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
//一个没有。。我们可以去下载http://hub.docker.com(docker hub),或者自己打包
搜索docker 镜像
docker search nginx
docker search supermap
下载docker 镜像
docker pull nginx
docker pull supermap/iserver
docker pull busybox
运行docker 镜像
docker run --name test -d -p 8080:80 nginx
//验证nginx安装的地址:http://192.168.10.200:8080/
docker run --name supermapiserver -d -p 9090:8090 -v /opt/data:/opt/Data supermap/iserver
//验证supermapiserver运行地址:http://192.168.10.200:9090/iserver
docker save -o busybox.tar busybox:latest
//把下载的busybox镜像存储为本地目录中busybox.tar文件
docker load -i busybox.tar
//在内网docker环境下执行以上命令导入镜像包busybox.tar
查看正在运行的docker容器
docker ps
docker ps -a //查看所有正在运行的docker容器
[root@testpc ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
562305838d5c nginx "nginx -g 'daemon of…" 7 minutes ago Up 7 minutes 0.0.0.0:8080->80/tcp test
[root@testpc ~]# docker inspect 5623 //获取容器的元数据信息
获取容器的元数据信息
[root@testpc ~]# docker inspect 5623
[
{
"Id": "562305838d5c13fbbb83749a303a0e0a09386ee1673f004632284785ed3e6b8c",
"Created": "2019-05-25T07:39:30.927845581Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 45707,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-05-25T07:39:31.547590899Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:53f3fd8007f76bd23bf663ad5f5009c8941f63828ae458cef584b5f85dc0a7bf",
"ResolvConfPath": "/var/lib/docker/containers/562305838d5c13fbbb83749a303a0e0a09386ee1673f004632284785ed3e6b8c/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/562305838d5c13fbbb83749a303a0e0a09386ee1673f004632284785ed3e6b8c/hostname",
"HostsPath": "/var/lib/docker/containers/562305838d5c13fbbb83749a303a0e0a09386ee1673f004632284785ed3e6b8c/hosts",
"LogPath": "/var/lib/docker/containers/562305838d5c13fbbb83749a303a0e0a09386ee1673f004632284785ed3e6b8c/562305838d5c13fbbb83749a3a0e0a09386ee1673f004632284785ed3e6b8c-json.log",
"Name": "/test",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "8080"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/8b99e285baee707937a849193438069ec3d56ea3e01c7c5466414b755705ce8a-init/diff:/var/libocker/overlay2/c0b07503681dcf631f39c46c31153282594e5b083cf3c97b28c77c0ec2d90cf4/diff:/var/lib/docker/overlay2/9a54ca3f8af5c31d7bcb729875303bab787028a1cce48be3886291007a8e57/diff:/var/lib/docker/overlay2/46fb3626ae0c9d773e5f6a7eee204ae6860d4e42092b2e14374c2689831386a5/diff",
"MergedDir": "/var/lib/docker/overlay2/8b99e285baee707937a849193438069ec3d56ea3e01c7c5466414b755705ce8a/merged",
"UpperDir": "/var/lib/docker/overlay2/8b99e285baee707937a849193438069ec3d56ea3e01c7c5466414b755705ce8a/diff",
"WorkDir": "/var/lib/docker/overlay2/8b99e285baee707937a849193438069ec3d56ea3e01c7c5466414b755705ce8a/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "562305838d5c",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.15.12-1~stretch",
"NJS_VERSION=1.15.12.0.3.1-1~stretch"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"ArgsEscaped": true,
"Image": "nginx",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers "
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "2effa1cd58de2ed9c84b026538e9d506f50d924deba479409439821896a612fa",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
]
},
"SandboxKey": "/var/run/docker/netns/2effa1cd58de",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "e89c6ebc8d48ba07dfd88481e294aff2e3ecf72c900a0d1ff1405949c0155123",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "a91af9120bb4c9eb58e11a94c88004bff4076afa3680ab2b7dcac6db5cd560ca",
"EndpointID": "e89c6ebc8d48ba07dfd88481e294aff2e3ecf72c900a0d1ff1405949c0155123",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
[root@testpc ~]#
以命令行方式进入到docker容器中
docker exec -it 5623 bash
[root@testpc ~]# docker exec -it 5623 bash
root@562305838d5c:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@562305838d5c:/# exit
exit
[root@testpc ~]#
向docker容器中拷贝文件
docker cp anaconda-ks.cfg 5623:/usr/share/nginx/html/
//拷入文件查看
[root@testpc ~]# docker exec -it 5623 bash
root@562305838d5c:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@562305838d5c:/# cd /usr/share/nginx/html
root@562305838d5c:/usr/share/nginx/html# ls
50x.html anaconda-ks.cfg index.html
root@562305838d5c:/usr/share/nginx/html#
停止正在运行的docker容器
docker stop 5623
//查看结果 STATUS为Exited
[root@testpc ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
562305838d5c nginx "nginx -g 'daemon of…" 22 minutes ago Exited (0) 24 seconds ago test
[root@testpc ~]#
删除正在运行的docker容器
docker rm 5623
//查看结果
[root@testpc ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@testpc ~]#
运行MySQL5.6 docker镜像实例的方法
加载mysql5.6 docker镜像和运行docker mysql容器实例
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 53f3fd8007f7 2 weeks ago 109MB
centos/mysql-56-centos7 latest 8ef375298394 12 months ago 407MB
docker load -i mysql-56-centos7.tar
docker run --name MYSQL56docker -v mysql.cnf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -d 83f3
mysql.cnf文件内容如下:
[client]
default-character-set=utf8
[mysqld]
skip-grant-tables
character-set-server=utf8
default-storage-engine=INNODB
collation-server=utf8_general_ci
[mysql]
default-character-set=utf8
关机后或容器关闭后重启容器的方法:
数据还在原处
通过 docker ps -a 看到关闭的容器
docker start your_container_name
docker attach your_container_name
[root@node114 docker_images]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52ecb0aa3b1b supermap/iportal "/bin/sh -c /opt/run…" 2 days ago Exited (137) 15 minutes ago iportalserver
9f1c40dd51cf supermap/iserver "/bin/sh -c /opt/run…" 3 days ago Exited (137) 15 minutes ago supermapiserver
712a3f0e5035 8ef3 "container-entrypoin…" 4 days ago Exited (0) 15 minutes ago MYSQL56docker
5c58116a7eb7 nginx "nginx -g 'daemon of…" 4 days ago Exited (0) 15 minutes ago test
[root@node114 docker_images]# docker start 712a3f0e5035
712a3f0e5035
[root@node114 docker_images]# docker start 52ecb0aa3b1b
52ecb0aa3b1b
[root@node114 docker_images]# docker start 9f1c40dd51cf
9f1c40dd51cf
[root@node114 docker_images]# docker start 5c58116a7eb7
5c58116a7eb7
[root@node114 docker_images]#
—the—end—