Docker入门
Docker安装
官方帮助文档:
# 1、卸载旧的版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2、需要的安装包
sudo yum install -y yum-utils
#国外镜像
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
#阿里云镜像(推荐)
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#更新yum缓存
sudo yum makecache fast
# 3、安装docker
sudo yum install docker-ce docker-ce-cli containerd.io
# 4、启动docker
systemctl start docker
#查看是否启动成功
docker version
# 5、启动hello-world
sudo docker run hello-world
# 6、查看hello-world镜像
docker images
卸载docker:
#如果需要卸载docker
sudo yum remove docker-ce docker-ce-cli containerd.io
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
阿里云服务器镜像加速器:
登录阿里云,进入控制台,找到容器镜像服务,选择对应系统版本,得到操作步骤
#免费阿里云镜像加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://79oqmvbm.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Docker命令文档:
https://docs.docker.com/reference/
查看镜像
[root@localhost ~]# docker images
#镜像的仓库源 标签 镜像ID 镜像创建时间 镜像大小
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 2 months ago 13.3kB
搜索镜像
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10903 [OK]
mariadb MariaDB Server is a high performing open sou… 4115 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 809 [OK]
#过滤收藏量大于3000的
[root@localhost ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10903 [OK]
mariadb MariaDB Server is a high performing open sou… 4115 [OK]
拉取镜像
#下载docker镜像 docker pull 镜像名:tag
[root@localhost ~]# docker pull mysql
Using default tag: latest #如果不写tag,默认最新版
latest: Pulling from library/mysql
69692152171a: Pull complete #分层下载,docker images的核心 联合文件系统
1651b0be3df3: Pull complete
951da7386bc8: Pull complete
0f86c95aa242: Pull complete
37ba2d8bd4fe: Pull complete
6d278bb05e94: Pull complete
497efbd93a3e: Pull complete
f7fddf10c2c2: Pull complete
16415d159dfb: Pull complete
0e530ffc6b73: Pull complete
b0a4a1a77178: Pull complete
cd90f92aa9ef: Pull complete
Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #镜像真实地址
删除镜像
#删除多个镜像
docker rmi -f 镜像ID 镜像ID 镜像ID 镜像ID
#遍历删除所有的镜像
docker rmi -f $(docker images -aq)
启动容器
docker run [可选参数] image
# 参数说明
--name="name" 容器名字 tomcat01 tomcat02 用来区分容器
-d 后台方式运行
-it 使用交互式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-p 随机容器端口
#下载一个centos容器
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
#查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest c0cdc95609f1 10 days ago 556MB
hello-world latest d1165f221234 2 months ago 13.3kB
centos latest 300e315adb2f 5 months ago 209MB
#启动并进入centos容器
[root@localhost ~]# docker run -it centos /bin/bash
#查看centos容器内容
[root@8aed09c8e6ea /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#退出并继续运行容器 ctrl + p + q 组合键
#退出并停止容器
[root@8aed09c8e6ea /]# exit
exit
#容器与宿主机不相干
[root@localhost ~]# ls
anaconda-ks.cfg
列出容器
docker ps [可选参数]
#参数说明
无参数 列出当前正在运行的容器
-a 列出当正在运行的容器+历史运行过的容器
-n=? 显示最近创建的容器
-q 只显示容器编号
删除容器
docker rm 容器ID #不能删除正在运行的容器
docker rm -f 强制删除 #可以删除正在运行的容器
docker rm -f $(docker ps -aq) #删除所有容器
docker ps -a -q |xargs docker rm #删除所有容器
启动和停止容器
docker start 容器ID #启动容器
docker restart 容器ID #重启容器
docker stop 容器ID #停止容器
docker kill 容器ID #强制停止容器
常用其他命令
后台启动容器
# 命令 docker run -d 镜像名
# docker ps 发现 centos 自动停止了
# 常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
查看容器日志
#启动centos容器 启动带入脚本循环输出字符串wang
[root@localhost ~]# docker run -it -d centos /bin/sh -c "while true;do echo wang;sleep 1;done"
01dc904710fba9797b58993c572f63dd9aefd1c529411d4beb01fb15918a4388
#查看容器ID
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01dc904710fb centos "/bin/sh -c 'while t…" 5 seconds ago Up 4 seconds blissful_maxwell
#显示容器日志最新的10条
[root@localhost ~]# docker logs -tf --tail 10 01dc904710fb
2021-05-22T12:37:55.496002403Z wang
2021-05-22T12:37:56.500844096Z wang
2021-05-22T12:37:57.505126265Z wang
2021-05-22T12:37:58.507671551Z wang
2021-05-22T12:37:59.512895085Z wang
2021-05-22T12:38:00.518663182Z wang
2021-05-22T12:38:01.521399072Z wang
2021-05-22T12:38:02.525642986Z wang
2021-05-22T12:38:03.527662099Z wang
2021-05-22T12:38:04.530641057Z wang
查看容器进程信息
[root@localhost ~]# docker top 01dc904710fb
UID PID PPID C STIME TTY TIME CMD
root 3341 3321 0 20:37 pts/0 00:00:00 /bin/sh -c while true;do echo wang;sleep 1;done
root 3547 3341 0 20:40 pts/0 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看容器信息
docker inspect 01dc904710fb
[
{
"Id": "01dc904710fba9797b58993c572f63dd9aefd1c529411d4beb01fb15918a4388",
"Created": "2021-05-22T12:37:36.193250304Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo wang;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3341,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-05-22T12:37:36.445167075Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/01dc904710fba9797b58993c572f63dd9aefd1c529411d4beb01fb15918a4388/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/01dc904710fba9797b58993c572f63dd9aefd1c529411d4beb01fb15918a4388/hostname",
"HostsPath": "/var/lib/docker/containers/01dc904710fba9797b58993c572f63dd9aefd1c529411d4beb01fb15918a4388/hosts",
"LogPath": "/var/lib/docker/containers/01dc904710fba9797b58993c572f63dd9aefd1c529411d4beb01fb15918a4388/01dc904710fba9797b58993c572f63dd9aefd1c529411d4beb01fb15918a4388-json.log",
"Name": "/blissful_maxwell",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"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,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"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/6f7849f77fce0e28ba178460b6bd75ec4025496b84d95a4916a18aba6e914761-init/diff:/var/lib/docker/overlay2/ef531d3a99c73c2ff624480b4afdbb3144067894db8b10edb931c546dbb41c9e/diff",
"MergedDir": "/var/lib/docker/overlay2/6f7849f77fce0e28ba178460b6bd75ec4025496b84d95a4916a18aba6e914761/merged",
"UpperDir": "/var/lib/docker/overlay2/6f7849f77fce0e28ba178460b6bd75ec4025496b84d95a4916a18aba6e914761/diff",
"WorkDir": "/var/lib/docker/overlay2/6f7849f77fce0e28ba178460b6bd75ec4025496b84d95a4916a18aba6e914761/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "01dc904710fb",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": true,
"OpenStdin": true,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"while true;do echo wang;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "410d3912d9d46fe73a861f3d0097726ff0cd8407ea673b580c7a546245f3ba5b",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/410d3912d9d4",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "daba68eecde9f110f7906f26cb9313fa3ff36cf8986eaa20489458cec3b3d2bf",
"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": "52621d7ba91422aa7ac238cc9e886e53e6ab40abc1011f685e27fe6c22a1a25f",
"EndpointID": "daba68eecde9f110f7906f26cb9313fa3ff36cf8986eaa20489458cec3b3d2bf",
"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@localhost ~]# docker exec -it 01dc904710fb /bin/bash
#进入容器正在执行的终端
[root@localhost ~]# docker attach 01dc904710fb
从容器文件拷入拷出
[root@localhost home]# docker run -it -d centos /bin/bash
1a358b82b139c1e74d361d01eef6a0ddb7190faa6e59e801917cfc493b002391
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a358b82b139 centos "/bin/bash" 1 second ago Up 1 second priceless_curran
[root@localhost home]# docker exec -it 1a358b82b139 /bin/bash
[root@1a358b82b139 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@1a358b82b139 /]# cd /home/
[root@1a358b82b139 home]# ls
[root@1a358b82b139 home]# touch wang.java
[root@1a358b82b139 home]# exit
exit
[root@localhost home]# ls
esuser wy
[root@localhost home]# docker cp 1a358b82b139:/home/wang.java /home
[root@localhost home]# ls
esuser wang.java wy
[root@localhost home]# touch yu.java
[root@localhost home]# ls
esuser wang.java wy yu.java
[root@localhost home]# docker cp yu.java 1a358b82b139:/home
[root@localhost home]# docker exec -it 1a358b82b139 /bin/bash
[root@1a358b82b139 /]# ls /home/
wang.java yu.java