Docker容器技术
一、Docker安装
1.1.1 Docker以及环境依赖安装
- 系统环境
[root@docker ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[root@docker ~]# uname -r
3.10.0-1062.18.1.el7.x86_64
- 检查是否安装过Docker
[root@docker ~]# rpm -qa docker docker-common docker-selinux docker-engine
#如果有执行以下命令
yum remove docker docker-common docker-selinux docker-engine
- YUM源准备(清华源)
#官网地址:https://mirrors.tuna.tsinghua.edu.cn
[root@docker ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
--2020-05-27 09:18:35-- https://download.docker.com/linux/centos/docker-ce.repo
Resolving download.docker.com (download.docker.com)... 13.227.60.67, 13.227.60.113, 13.227.60.6, ...
Connecting to download.docker.com (download.docker.com)|13.227.60.67|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2424 (2.4K) [binary/octet-stream]
Saving to: ‘/etc/yum.repos.d/docker-ce.repo’
100%[===========================================================================================>] 2,424 --.-K/s in 0s
2020-05-27 09:18:36 (272 MB/s) - ‘/etc/yum.repos.d/docker-ce.repo’ saved [2424/2424]
#把软件仓库地址替换为如下:
[root@docker ~]# sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
- yum安装依赖环境
[root@docker ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
- Docker安装
[root@docker ~]# yum makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 7.9 kB 00:00:00
* epel: mirrors.bangmodhosting.com
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Metadata Cache Created
[root@docker ~]# yum install docker-ce -y
- 启动Docker服务
[root@docker ~]# systemctl start docker
[root@docker ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2020-05-27 09:29:41 CST; 6s ago
Docs: https://docs.docker.com
Main PID: 1847 (dockerd)
Tasks: 8
Memory: 40.1M
CGroup: /system.slice/docker.service
└─1847 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
May 27 09:29:40 docker dockerd[1847]: time="2020-05-27T09:29:40.573497125+08:00" level=info msg="scheme "unix" not regist...le=grpc
May 27 09:29:40 docker dockerd[1847]: time="2020-05-27T09:29:40.573879050+08:00" level=info msg="ccResolverWrapper: sending...le=grpc
May 27 09:29:40 docker dockerd[1847]: time="2020-05-27T09:29:40.574304979+08:00" level=info msg="ClientConn switching balan...le=grpc
May 27 09:29:40 docker dockerd[1847]: time="2020-05-27T09:29:40.608105515+08:00" level=info msg="Loading containers: start."
May 27 09:29:40 docker dockerd[1847]: time="2020-05-27T09:29:40.928343327+08:00" level=info msg="Default bridge (docker0) i...ddress"
May 27 09:29:41 docker dockerd[1847]: time="2020-05-27T09:29:41.024559448+08:00" level=info msg="Loading containers: done."
May 27 09:29:41 docker dockerd[1847]: time="2020-05-27T09:29:41.069005082+08:00" level=info msg="Docker daemon" commit=9d98...19.03.9
May 27 09:29:41 docker dockerd[1847]: time="2020-05-27T09:29:41.069564924+08:00" level=info msg="Daemon has completed initialization"
May 27 09:29:41 docker systemd[1]: Started Docker Application Container Engine.
May 27 09:29:41 docker dockerd[1847]: time="2020-05-27T09:29:41.088775124+08:00" level=info msg="API listen on /var/run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.
[root@docker ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
- 查看Docker信息
[root@docker ~]# docker version
Client: Docker Engine - Community
Version: 19.03.9
API version: 1.40
Go version: go1.13.10
Git commit: 9d988398e7
Built: Fri May 15 00:25:27 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.9
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 9d988398e7
Built: Fri May 15 00:24:05 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
9.查看Docker系统信息,包括镜像和容器数。
[root@docker ~]# docker info
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 19.03.9
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1062.18.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.934GiB
Name: docker
ID: ZKX4:RF7S:OIH7:5VAW:JPIS:E7E7:TDP5:AFPB:2QG7:TSKR:I4R7:WGDV
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
- 配置镜像加速
[root@docker ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://2giwyi75.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://2giwyi75.mirror.aliyuncs.com"]
}
[root@docker ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://2giwyi75.mirror.aliyuncs.com"]
}
- 重启服务
[root@docker ~]# systemctl daemon-reload
[root@docker ~]# systemctl restart docker
-
Docker的体系结构
-
获取Docker镜像
[root@docker ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 13237 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1809 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 775 [OK]
[root@docker ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
afb6ec6fdc1c: Pull complete
b90c53a0b692: Pull complete
11fa52a0fdc0: Pull complete
Digest: sha256:30dfa439718a17baafefadf16c5e7c9d0a1cde97b4fd84f63b69e13513be7097
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 9beeba249f3e 11 days ago 127MB
- 启动Nginx并映射宿主机80端口
[root@docker ~]# docker run -it -d --name mynginx -p 80:80 nginx
2674b9b9d93c848aa4dd8a525049ecbeed7e17e2ee43c82cfe33f79580e52045
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2674b9b9d93c nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp mynginx
- 访问Nginx
搭建成功,请大佬多多指教.....。