需要正确的理解仓储/镜像/容器这几个概念 :
Docker 本身是一个容器运行载体或称之为管理引擎。我们把应用程序和配置依赖打包好形成一个 可交付的运行环境,这个打包好的运行环境就似乎
image镜像文件。只有通过这个镜像文件才能生 成 Docker 容器。image 文件可以看作是容器的模板。Docker 根据
image 文件生成容器的实例。 同一个 image 文件,可以生成多个同时运行的容器实例。
image 文件生成的容器实例,本身也是一个文件,称为镜像文件。
一个容器运行一种服务,当我们需要的时候,就可以通过docker客户端创建一个对应的运行实例, 也就是我们的容器
至于仓库,就是放了一堆镜像的地方,我们可以把镜像发布到仓库中,需要的时候从仓库中拉下来 就可以了。
yum-yinstallgcc
yum-yinstallgcc-c++
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
yuminstall-yyum-utils
# 错误
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
## 报错
[Errno 14] curl #35 - TCP connection reset by peer
[Errno 12] curl #35 - Timeout
# 正确推荐使用国内的
yum-config-manager --add-repo http://mirrors.aliyun.com/docker- ce/linux/centos/docker-ce.repo
yummakecachefast
yuminstalldocker-cedocker-ce-clicontainerd.io`
systemctlstartdocker
docker version
docker run hello-world
docker images
#停止docker服务
systemctl stop docker
yum -y remove docker-ce docker-ce-cli containerd.io
#删除docker目录
rm -rf /var/lib/docker
1、介绍:https://www.aliyun.com/product/acr
2、注册一个属于自己的阿里云账户(可复用淘宝账号)
3、进入管理控制台设置密码,开通
4、查看镜像加速器自己的
dockerrunhello-world
Docker是一个Client-Server结构的系统,Docker守护进程运行在主机上, 然后通过Socket连接从客户 端访问,守护进程从客户端接受命令并管理运行在主机上的容器。 容器,是一个运行时环境,就是我们 前面说到的集装箱。
1、docker有着比虚拟机更少的抽象层。由亍docker不需要Hypervisor实现硬件资源虚拟化,运行在 docker容器上的程序直接使用的都是实际物理机的硬件资源。因此在CPU、内存利用率上docker将会在 效率上有明显优势。
2、docker利用的是宿主机的内核,而不需要Guest OS。因此,当新建一个容器时,docker不需要和虚拟机 一样重新加载一个操作系统内核。仍而避免引寻、加载操作系统内核返个比较费时费资源的过程,当新建 一个虚拟机时,虚拟机软件需要加载Guest OS,返个新建过程是分钟级别的。而docker由于直接利用宿主 机的操作系统,则省略了返个过程,因此新建一个docker容器只需要几秒钟。
docker version # 显示 Docker 版本信息。
docker info # 显示 Docker 系统信息,包括镜像和容器数
docker --help # 帮助
# 列出本地主机上的镜像
docker images
可选项
#可选项
-a: 列出本地所有镜像
-q: 只显示镜像
id --digests: 显示镜像的摘要信息
# 搜索镜像
docker search mysql
# 可选项
--filter=stars=50 # 列出收藏数不小于指定值的镜像。
dcoker pull mysql
[root@VM-4-5-centos ~]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
a330b6cecb98: Pull complete
9c8f656c32b8: Pull complete
88e473c3f553: Pull complete
062463ea5d2f: Pull complete #**分层下载**
daf7e3bdf4b6: Pull complete
1839c0b7aac9: Pull complete
cf0a0cfee6d0: Pull complete
1b42041bb11e: Pull complete
10459d86c7e6: Pull complete
b7199599d5f9: Pull complete
1d6f51e17d45: Pull complete
50e0789bacad: Pull complete
Digest: sha256:99e0989e7e3797cfbdb8d51a19d32c8d286dd8862794d01a547651a896bcf00c #镜像签名
Status: Downloaded newer image for docker.io/mysql:latest #仓库真实位置
[root@VM-4-5-centos ~]#
################分隔线
# 接下来指定mysql的版本为5.7下载
[root@VM-4-5-centos ~]# docker pull mysql:5.7
Trying to pull repository docker.io/library/mysql ...
5.7: Pulling from docker.io/library/mysql
a330b6cecb98: Already exists
9c8f656c32b8: Already exists
88e473c3f553: Already exists
062463ea5d2f: Already exists # 已存在。分层下载的原因
daf7e3bdf4b6: Already exists
1839c0b7aac9: Already exists
cf0a0cfee6d0: Already exists
fae7a809788c: Pull complete
dae5a82a61f0: Pull complete
7063da9569eb: Pull complete # 相较于最新版本的。只下载了一部分文件
51a9a9b4ef36: Pull complete
Digest: sha256:d9b934cdf6826629f8d02ea01f28b2c4ddb1ae27c32664b14867324b3e5e1291
Status: Downloaded newer image for docker.io/mysql:5.7 # 仓库位置为5.7
[root@VM-4-5-centos ~]#
# 删除镜像
docker rmi -f 镜像id # 删除单个
docker rmi -f 镜像名:tag 镜像名:tag docker # 删除多个
rmi -f $(docker images -qa) # 删除全部
说明:有镜像才能创建容器,我们这里使用 centos 的镜像来测试,就是虚拟一个 centos !
docker pull centos
[root@VM-4-5-centos ~]# docker images #查看下本机的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/halohub/halo 1.4.12 05b2b0047107 5 days ago 326 MB
docker.io/centos latest 5d0da3dc9764 6 days ago 231 MB
docker.io/mysql 5.7 1d7aba917169 2 weeks ago 448 MB
docker.io/mysql latest 0716d6ebcc1a 2 weeks ago 514 MB
#run 命令创建容器
# 常用参数说明
--name="Name" -d # 给容器指定一个名字
-d # 后台方式运行容器,并返回容器的id!
-i # 以交互模式运行容器,通过和 -t 一起使用
-t # 给容器重新分配一个终端,通常和 -i 一起使用
-P # 随机端口映射(大写)
-p # 指定端口映射(小结),一般可以有四种写法
# 使用-it交互模式运行centOS镜像,并给容器分配一个终端。并在容器内部执行 /bin/bash
[root@VM-4-5-centos ~]# docker run -it centos /bin/bash
#命令执行后可以看到地址由 服务器centos变为容器的id 了
[root@672bc4e26153 /]
# 使用exit退出容器,地址变为服务器
[root@672bc4e26153 /]# exit
exit
[root@VM-4-5-centos ~]#
docker ps
常用参数:
-a # 列出当前所有正在运行的容器 + 历史运行过的容器 -l # 显示最近创建的容器
-n=? # 显示最近n个创建的容器
-q # 静默模式,只显示容器编号。
exit # 容器停止退出
ctrl+P+Q # 容器不停止退出
docker start (容器id or 容器名) # 启动容器
docker restart (容器id or 容器名) # 重启容器
docker stop (容器id or 容器名) # 停止容器
docker kill (容器id or 容器名) # 强制停止容器
docker rm 容器id # 删除指定容器
docker rm -f $(docker ps -a -q) # 删除所有容器
docker ps -a -q|xargs docker rm # 删除所有容器
# 命令
docker run -d 容器名
# 例子
docker run -d centos # 启动centos,使用后台方式启动
# 问题: 使用docker ps 查看,发现容器已经退出了!
# 解释:Docker容器后台运行,就必须有一个前台进程,容器运行的命令如果不是那些一直挂起的命 令,就会自动退出。
# 比如,你运行了nginx服务,但是docker前台没有运行应用,这种情况下,容器启动后,会立即自 杀,因为他觉得没有程序了,所以最好的情况是,将你的应用使用前台进程的方式运行启动。
# 命令
docker logs -f -t --tail 容器id
# 例子:我们启动 centos,并编写一段脚本来测试!最后查看日志
[root@VM-4-5-centos ~]# docker run -d centos /bin/sh -c "while true;do echo painsix;sleep 1;done"
2c00fd9e09c1e30665c3c430e8864633d7246888ce69951c37c42f7b8f7b6b2f
[root@VM-4-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c00fd9e09c1 centos "/bin/sh -c 'while..." 8 seconds ago Up 7 seconds laughing_volhard
be226514bd0f halohub/halo:1.4.12 "/bin/sh -c 'java ..." 20 hours ago Up 20 hours 0.0.0.0:8090->8090/tcp halo
# -t 显示时间戳
# -f 打印最新的日志
# --tail 数字 显示多少条!
[root@VM-4-5-centos ~]# docker logs -tf --tail 10 2c00fd9e09c1
2021-09-22T09:56:25.776861000Z painsix
2021-09-22T09:56:26.778608000Z painsix
2021-09-22T09:56:27.780334000Z painsix
2021-09-22T09:56:28.782124000Z painsix
2021-09-22T09:56:29.783906000Z painsix
2021-09-22T09:56:30.785656000Z painsix
2021-09-22T09:56:31.787354000Z painsix
2021-09-22T09:56:32.789100000Z painsix
2021-09-22T09:56:33.790769000Z painsix
2021-09-22T09:56:34.792496000Z painsix
2021-09-22T09:56:35.794251000Z painsix
2021-09-22T09:56:36.796047000Z painsix
2021-09-22T09:56:37.797710000Z painsix
2021-09-22T09:56:38.799463000Z painsix
2021-09-22T09:56:39.801264000Z painsix
2021-09-22T09:56:40.803089000Z painsix
2021-09-22T09:56:41.804804000Z painsix
2021-09-22T09:56:42.806571000Z painsix
2021-09-22T09:56:43.808292000Z painsix
2021-09-22T09:56:44.809971000Z painsix
2021-09-22T09:56:45.811764000Z painsix
2021-09-22T09:56:46.813496000Z painsix
2021-09-22T09:56:47.815237000Z painsix
2021-09-22T09:56:48.816913000Z painsix
2021-09-22T09:56:49.818682000Z painsix
^C
[root@VM-4-5-centos ~]#
# 命令
docker top 容器id
[root@VM-4-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c00fd9e09c1 centos "/bin/sh -c 'while..." 16 hours ago Up 16 hours laughing_volhard
be226514bd0f halohub/halo:1.4.12 "/bin/sh -c 'java ..." 37 hours ago Up 37 hours 0.0.0.0:8090->8090/tcp halo
[root@VM-4-5-centos ~]# docker top 2c00fd9e09c1
UID PID PPID C STIME TTY TIME CMD
root 6861 6845 0 Sep22 ? 00:00:13 /bin/sh -c while true;do echo painsix;sleep 1;done
root 30993 6861 0 10:16 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
[root@VM-4-5-centos ~]#
docker inspect 容器id
[root@VM-4-5-centos ~]# docker inspect 2c00fd9e09c1
[
{ #上面容器的id就是截取完整id的前几位
"Id": "2c00fd9e09c1e30665c3c430e8864633d7246888ce69951c37c42f7b8f7b6b2f",
"Created": "2021-09-22T09:55:40.499935415Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo painsix;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 6861,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-09-22T09:55:40.698579694Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/2c00fd9e09c1e30665c3c430e8864633d7246888ce69951c37c42f7b8f7b6b2f/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/2c00fd9e09c1e30665c3c430e8864633d7246888ce69951c37c42f7b8f7b6b2f/hostname",
"HostsPath": "/var/lib/docker/containers/2c00fd9e09c1e30665c3c430e8864633d7246888ce69951c37c42f7b8f7b6b2f/hosts",
"LogPath": "",
"Name": "/laughing_volhard",
"RestartCount": 0,
"Driver": "overlay2",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "journald",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"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
},
"GraphDriver": {
"Name": "overlay2",
"Data": {
"LowerDir": "/var/lib/docker/overlay2/edb17ea754e90eb008a989cb436f688ee855d7590aaf3c4b974f191a1dd91cd0-init/diff:/var/lib/docker/overlay2/4d6acac3e4507e39c50cd46a4b0876e5f16123aabf7d4e9d3b4f7aecd6b04bbc/diff",
"MergedDir": "/var/lib/docker/overlay2/edb17ea754e90eb008a989cb436f688ee855d7590aaf3c4b974f191a1dd91cd0/merged",
"UpperDir": "/var/lib/docker/overlay2/edb17ea754e90eb008a989cb436f688ee855d7590aaf3c4b974f191a1dd91cd0/diff",
"WorkDir": "/var/lib/docker/overlay2/edb17ea754e90eb008a989cb436f688ee855d7590aaf3c4b974f191a1dd91cd0/work"
}
},
"Mounts": [],
"Config": {
"Hostname": "2c00fd9e09c1",
"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",
"while true;do echo painsix;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"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": "22bc70ab333ae28f93055965df0bc0a20919bf2fa4d325d309da775347f64442",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/22bc70ab333a",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "7cc6312f815fa264c5b2cfa6b55bb5df32293264361caafb0af9359e3aa80e4b",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "0e82bc7e8ab19b709ed17ec1a0ffdd629f287d548fea956be2e4b6e4e5f09f8d",
"EndpointID": "7cc6312f815fa264c5b2cfa6b55bb5df32293264361caafb0af9359e3aa80e4b",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03"
}
}
}
}
]
[root@VM-4-5-centos ~]#
dcoker exec -it 容器id bashshell
[root@VM-4-5-centos ~]# docker exec -it 2c00fd9e09c1 /bin/bash
[root@2c00fd9e09c1 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Sep22 ? 00:00:13 /bin/sh -c while true;do echo painsix;sleep 1;done
root 26783 0 0 02:24 ? 00:00:00 /bin/bash
root 26819 0 0 02:25 ? 00:00:00 /bin/bash
root 26839 1 0 02:25 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 26840 26819 0 02:25 ? 00:00:00 ps -ef
[root@2c00fd9e09c1 /]#
dcoker cp 容器id:容器内的路径 目的主机路劲
[root@VM-4-5-centos ~]# docker exec -it 2c00fd9e09c1 /bin/bash #进入容器
[root@2c00fd9e09c1 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@2c00fd9e09c1 /]# cd /home/ #进入容器home目录
[root@2c00fd9e09c1 home]# touch bbb #创建bbb文件
[root@2c00fd9e09c1 home]# ls
aaa bbb
[root@2c00fd9e09c1 home]# exit
exit
[root@VM-4-5-centos ~]# docker cp 2c00fd9e09c1:/home/bbb /root #复制到本机root下
[root@VM-4-5-centos ~]# pwd
/root
[root@VM-4-5-centos ~]# ls
aaa bbb mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
apache-maven-3.6.3-bin.tar.gz Git-v2.26.2.tar.gz nginx-1.17.10.tar.gz
apache-tomcat-8.5.55.tar.gz halo-backup-2021-09-18-15-57-28-851650058.zip node-v12.16.3-linux-x64.tar.xz
apache-zookeeper-3.6.1-bin.tar.gz jdk-8u161-linux-x64.tar.gz Python-3.8.3.tgz
application.yaml kafka_2.12-2.5.0.tgz redis-5.0.8.tar.gz
[root@VM-4-5-centos ~]#
命令 | 使用 | 注释 |
---|---|---|
attach | Attach to a running container | # 当前 shell 下 attach 连接指定运行镜像 |
build | Build an image from a Dockerfile | # 通过 Dockerfile 定制镜像 |
commit | Create a new image from a container changes | # 提交当前容器为新的镜像 |
cp | Copy files/folders from the containers filesystem to the host path | #从容器中拷贝指定文件或者目录到宿主机中 |
create | Create a new container | # 创建一个新的容器,同 run,但不启动容器 |
diff | Inspect changes on a container’s filesystem | # 查看 docker 容器变化 |
events | Get real time events from the server | # 从 docker 服务获取容器实时事件 |
exec | Run a command in an existing container | # 在已存在的容器上运行命令 |
export | Stream the contents of a container as a tar archive | # 导出容器的内容流作为一个 tar 归档文件[对应 import ] |
history | Show the history of an image | # 展示一个镜像形成历史 |
images | List images | # 列出系统当前镜像 |
import | Create a new filesystem image from the contents of a tarball | # 从 tar包中的内容创建一个新的文件系统映像[对应export] |
info | Display system-wide information | # 显示系统相关信息 |
inspect | Return low-level information on a container | # 查看容器详细信息 |
kill | Kill a running container | # kill 指定 docker 容器 |
load | Load an image from a tar archive | # 从一个 tar 包中加载一个镜像[对应 save] |
login | Register or Login to the docker registry server | # 注册或者登陆一个 docker 源服务器 |
logout | Log out from a Docker registry server | # 从当前 Docker registry 退出 |
logs | Fetch the logs of a container | # 输出当前容器日志信息 |
port | Lookup the public-facing port which is NAT-ed to PRIVATE_PORT | # 查看映射端口对应的容器内部源端口 |
pause | Pause all processes within a container | # 暂停容器 |
ps | List containers | # 列出容器列表 |
pull | Pull an image or a repository from the docker registry server | # 从docker镜像源服务器拉取指定镜像或者库镜像 |
push | Push an image or a repository to the docker registry server | # 推送指定镜像或者库镜像至docker源服务器 |
restart | Restart a running container | # 重启运行的容器 |
rm | Remove one or more containers | # 移除一个或者多个容器 |
rmi | Remove one or more images | # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除] |
run | Run a command in a new container | # 创建一个新的容器并运行一个命令 |
save | Save an image to a tar archive | # 保存一个镜像为一个 tar 包[对应 load] |
search | Search for an image on the Docker Hub | # 在 docker hub 中搜索镜像 |
start | Start a stopped containers | # 启动容器 |
stop | Stop a running containers | # 停止容器 |
tag | Tag an image into a repository | # 给源中镜像打标签 |
top | Lookup the running processes of a container | # 查看容器中运行的进程信息 |
unpause | Unpause a paused container | # 取消暂停容器 |
version | Show the docker version information | # 查看 docker 版本号 |
wait | Block until a container stops, then print its exit code | # 截取容器停止时的退出状态值 |
mkdir -p /data/portainer/data /data/portainer/public
cd /data/portainer
wget https://labx.me/dl/4nat/public.zip
unzip public.zip
docker run -d --restart=always --name portainer -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v /data/portainer/data:/data -v /data/portainer/public:/public portainer/portainer:latest