本文以视频教程 https://www.bilibili.com/video/BV1og4y1q7M4 为基础,忽略部分基础原理,保留操作过程代码和结果截图,旨在对docker镜像和容器基本命令提供可快速上手并有可操作性的练习指南。
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help(man docker 命令) # 帮助命令
帮助文档的地址:https://docs.docker.com/engine/reference/commandline/docker/
docker images 查看所有本地的主机上的镜像
[root@VM_0_13_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 8 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源(镜像名)
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
-a, --all # 列出所有镜像
-q, --quiet # 只显示镜像的id
docker search 搜索镜像
[root@VM_0_13_centos ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9942 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3636 [OK]
# 可选项,通过收藏来过滤
--filter=STARS=3000 # 搜索出来的镜像就是STARS大于3000的
docker pull 下载镜像
# 下载镜像 docker pull 镜像名[:tag]
[root@VM_0_13_centos ~]# docker pull mysql
Using default tag: latest # 如果不写tag,默认就是 latest
latest: Pulling from library/mysql
bf5952930446: Pull complete # 分层下载,docker images的核心 联合文件系统
8254623a9871: Pull complete
938e3e06dac4: Pull complete
ea28ebf28884: Pull complete
f3cef38785c2: Pull complete
894f9792565a: Pull complete
1d8a57523420: Pull complete
6c676912929f: Pull complete
3cdd8ff735c9: Pull complete
4c70cbe51682: Pull complete
e21cf0cb4dc3: Pull complete
28c36cd3abcc: Pull complete
Digest: sha256:6ded54eb1e5d048d8310321ba7b92587e9eadc83b519165b70bbe47e4046e76a # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 等价于它
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@VM_0_13_centos ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
bf5952930446: Already exists
8254623a9871: Already exists
938e3e06dac4: Already exists
ea28ebf28884: Already exists
f3cef38785c2: Already exists
894f9792565a: Already exists
1d8a57523420: Already exists
5f09bf1d31c1: Pull complete
1591b28581e8: Pull complete
96ef942f7603: Pull complete
2e009731422e: Pull complete
Digest: sha256:1a83136153b238b659b0887ceb4e08275473af1eab2e67de4c22b37c5f4130cd
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
docker rmi 删除镜像
[root@VM_0_13_centos ssh]# docker rmi -f 镜像id/镜像名称 # 删除指定镜像
[root@VM_0_13_centos ssh]# docker rmi -f 镜像id/镜像名称 镜像id/镜像名称 镜像id/镜像名称 # 删除多个镜像
[root@VM_0_13_centos ssh]# docker rmi -f $(docker iamges -aq) # 删除全部的镜像
[root@VM_0_13_centos ssh]# docker images -a -q|xargs docker rmi # 删除所有的镜像
说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像才测试学习(套娃?hhhhh)
docker pull centos
docker run [可选参数] image
# 参数说明
--name="Name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台方式启动
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
容器端口
-P 随机指定端口
# 测试,启动并进入容器 (套娃?hhhhh)
[root@VM_0_13_centos /]# docker run -it centos /bin/bash
[root@5e7d1cb8ec28 /]# ls # 查看容器内的centos,基础版本,很多命令都是不完善的!
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 从容器中退回主机
[root@4d6cf194f179 /]# exit
exit
[root@VM_0_13_centos /]# ls
bin dev lib media proc sbin tmp
boot etc lib64 mnt root srv usr
data home lost+found opt run sys var
[root@VM_0_13_centos /]#
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出当前正在运行的容器+带出历史运行过的容器
-n=? # 显示最近创建的n个容器
-q # 只显示容器的编号
[root@VM_0_13_centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM_0_13_centos /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
4d6cf194f179 centos "/bin/bash" 2 minutes ago Exited (0) About a minute ago priceless_napier
5e7d1cb8ec28 centos "/bin/bash" 6 minutes ago Exited (127) 2 minutes ago busy_sinoussi
05ce294f9ba1 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago nostalgic_poitras
4ac5474112ad hello-world "/hello" 2 hours ago Exited (0) 2 hours ago wonderful_tesla
6256c499dc85 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago intelligent_volhard
exit # 直接容器停止并退出
Ctrl + P + Q # 容器不停止退出
docker rm 容器id # 删除指定的容器,不能删除正在运行的容器
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 镜像名!
[root@VM_0_13_centos /]# docker run -d centos
# 问题docker ps,发现 centos 停止了
# 常见的坑,docker 容器使用后台进行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
docker logs -f -t --tail 容器,没有日志
# 自己编写一段shell脚本
[root@VM_0_13_centos /]# docker run -d centos /bin/sh -c "while true;do echo yushuo;sleep 1;done"
CONTAINER ID IMAGE
d209eedceec2 centos
# 显示日志
-tf # 显示日志 -t 显示时间戳 -f 显示接下来的log信息
--tail n # 要显示的日志条数
[root@VM_0_13_centos /]# docker logs -tf --tail 10 d209eedceec2
2020-09-08T13:37:52.434663412Z yushuo
2020-09-08T13:37:53.436735983Z yushuo
(类似linux中:ps命令)
# 命令
docker top 容器id
[root@VM_0_13_centos /]# docker top d209eedceec2
UID PID PPID C STIME TTY
root 11564 11548 0 20:59 pts/0
root 31362 11564 0 22:19 pts/0
# 命令
docker inspect 容器id
#测试
[root@VM_0_13_centos /]# docker inspect d209eedceec2
[
{
"Id": "d209eedceec2367f2df149c2f0a8161347e60298727a70e3aea3fb6b1ea3f3ab",
"Created": "2020-09-08T12:59:04.0332043Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo yushuo;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 11564,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-09-08T12:59:04.358426426Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
"ResolvConfPath": "/var/lib/docker/containers/d209eedceec2367f2df149c2f0a8161347e60298727a70e3aea3fb6b1ea3f3ab/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/d209eedceec2367f2df149c2f0a8161347e60298727a70e3aea3fb6b1ea3f3ab/hostname",
"HostsPath": "/var/lib/docker/containers/d209eedceec2367f2df149c2f0a8161347e60298727a70e3aea3fb6b1ea3f3ab/hosts",
"LogPath": "/var/lib/docker/containers/d209eedceec2367f2df149c2f0a8161347e60298727a70e3aea3fb6b1ea3f3ab/d209eedceec2367f2df149c2f0a8161347e60298727a70e3aea3fb6b1ea3f3ab-json.log",
"Name": "/peaceful_shamir",
"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,
"Capabilities": null,
"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/8283ec51aadb12bb2833db9657734d64dd3f132073bc5299d2eb4f7d1ed99a36-init/diff:/var/lib/docker/overlay2/7711d01c217dc0e6e48097163cfbed122113fa2ee94b35f9e0af3334cde350e2/diff",
"MergedDir": "/var/lib/docker/overlay2/8283ec51aadb12bb2833db9657734d64dd3f132073bc5299d2eb4f7d1ed99a36/merged",
"UpperDir": "/var/lib/docker/overlay2/8283ec51aadb12bb2833db9657734d64dd3f132073bc5299d2eb4f7d1ed99a36/diff",
"WorkDir": "/var/lib/docker/overlay2/8283ec51aadb12bb2833db9657734d64dd3f132073bc5299d2eb4f7d1ed99a36/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "d209eedceec2",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"while true;do echo yushuo;sleep 1;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20200809",
"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": "50f7730df4984bc750da43cced49ed17b7b6b7a7bf49fd2448a3ffac25b5d38d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/50f7730df498",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "03cde5e54624ee1b838b6699a32050b97adc86eb771c50bbcd5c890cb2a826d1",
"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": "db15aa787815809de73afa7b29a531e84d429455c1b9cb549ae74da3282ed6e3",
"EndpointID": "03cde5e54624ee1b838b6699a32050b97adc86eb771c50bbcd5c890cb2a826d1",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
# 我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
# 命令【方式1】
docker exec -it 容器id bashShell
# 测试
[root@VM_0_13_centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d209eedceec2 centos "/bin/sh -c 'while t…" 2 hours ago Up 2 hours
peaceful_shamir
[root@VM_0_13_centos /]# docker exec -it d209eedceec2 /
bin/bash
[root@d209eedceec2 /]# ls
bin home lost+found opt run sys var
dev lib media proc sbin tmp
etc lib64 mnt root srv usr
[root@d209eedceec2 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 12:59 pts/0 00:00:01 /bin/sh
root 5603 0 0 14:32 pts/1 00:00:00 /bin/ba
root 5663 1 0 14:33 pts/0 00:00:00 /usr/bi
root 5664 5603 0 14:33 pts/1 00:00:00 ps -ef
# 命令【方式2】
docker attach 容器id
# 测试
[root@VM_0_13_centos /]# docker attach d209eedceec2
yushuo
yushuo
# 进入正在执行当前的代码
# docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach # 进入容器正在执行的终端,不会启动新的进程!
docker run、exec和attach使用和区别: https://blog.csdn.net/MePlusPlus/article/details/92822329 docker run;创建和启动一个新的容器实例,操作对象是镜像,选项较多,如果你要创建和启动一个容器,只能用run; docker exec: 在已运行的容器中,执行命令,操作对象是容器,如果你要进入已运行的容器,并且执行命令,用exec; docker attach: 同样操作的是已运行的容器,可以将本机标准输入(键盘输入)输到容器中,也可以将容器的输出显示在本机的屏幕上,如果你想查看容器运行过程中产生的标准输入输出,用attach;
docker cp 容器id:容器内的路径 目的地主机路径
# 进入docker容器内部
[root@VM_0_13_centos /]# docker run -it centos /bin/bash
[root@d0b9dbc42b33 /]# cd /home
[root@d0b9dbc42b33 home]# ll
bash: ll: command not found
[root@d0b9dbc42b33 home]# ls
# 在容器内新建一个文件
[root@d0b9dbc42b33 home]# touch test.java
[root@d0b9dbc42b33 home]# ls
test.java
[root@d0b9dbc42b33 home]# ls -l
total 0
-rw-r--r-- 1 root root 0 Sep 8 14:50 test.java
[root@d0b9dbc42b33 home]# exit
exit
[root@VM_0_13_centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
[root@VM_0_13_centos /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
d0b9dbc42b33 centos "/bin/bash" About a minute ago Exited (0) 9 seconds
# 将这文件拷贝出来到主机
[root@VM_0_13_centos /]# docker cp d0b9dbc42b33:/home/test.java /home
[root@VM_0_13_centos /]# cd /home
[root@VM_0_13_centos home]# ls
test.java
# 拷贝是一个手动过程,未来我们使用 -v 数据卷的技术,可以实现 自动同步 容器内/home 主机/home
Child commands:https://docs.docker.com/engine/reference/commandline/docker/
Command | Description |
---|---|
Attach local standard input, output, and error streams to a running container | |
Build an image from a Dockerfile | |
Manage builds | |
Manage checkpoints | |
Create a new image from a container’s changes | |
Manage Docker configs | |
Manage containers | |
Manage contexts | |
Copy files/folders between a container and the local filesystem | |
Create a new container | |
Inspect changes to files or directories on a container’s filesystem | |
Get real time events from the server | |
Run a command in a running container | |
Export a container’s filesystem as a tar archive | |
Show the history of an image | |
Manage images | |
List images | |
Import the contents from a tarball to create a filesystem image | |
Display system-wide information | |
Return low-level information on Docker objects | |
Kill one or more running containers | |
Load an image from a tar archive or STDIN | |
Log in to a Docker registry | |
Log out from a Docker registry | |
Fetch the logs of a container | |
Manage Docker image manifests and manifest lists | |
Manage networks | |
Manage Swarm nodes | |
Pause all processes within one or more containers | |
Manage plugins | |
List port mappings or a specific mapping for the container | |
List containers | |
Pull an image or a repository from a registry | |
Push an image or a repository to a registry | |
Rename a container | |
Restart one or more containers | |
Remove one or more containers | |
Remove one or more images | |
Run a command in a new container | |
Save one or more images to a tar archive (streamed to STDOUT by default) | |
Search the Docker Hub for images | |
Manage Docker secrets | |
Manage services | |
Manage Docker stacks | |
Start one or more stopped containers | |
Display a live stream of container(s) resource usage statistics | |
Stop one or more running containers | |
Manage Swarm | |
Manage Docker | |
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE | |
Display the running processes of a container | |
Manage trust on Docker images | |
Unpause all processes within one or more containers | |
Update configuration of one or more containers | |
Show the Docker version information | |
Manage volumes | |
Block until one or more containers stop, then print their exit codes |
docker的命令是十分多的,上面我们学习的那些都是最常用的容器和镜像的命令,之后我们还会学习很多命令!
docker commit 提交容器成为一个新的副本
# 命令和git命令类似
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名:[TAG]
实战测试
# 1、启动一个默认的tomcat
# 2、发现这个默认的tomcat 是没有webapps应用的,镜像的原因,官方的镜像默认是 webapps 下面是没有文件的!
# 3、我自己拷贝进去了基本的文件
# 4、将我们操作过的容器通过commit提交为一个镜像!我们以后就使用我们修改过的镜像即可,这就是我们自己的一个修改的镜像
学习方式说明:理解概念,但是一定要实践,最后实践和理论相结合一次搞定这个知识
如果你想保存当前容器的状态,就可以通过commit来提交,获得一个镜像,
就好比我们以前学习vm时候,快照!
什么portainer?
Docker图形化界面管理工作!提供一个后台面板供我们操作!(先打开云服务器安全组)
$ docker volume create portainer_data
$ docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
访问测试:外网ip:8080 http://ip:9000/
通过它来访问:
这里目前选择管理本地
进入之后的面板:
点击local进入详细管理面板:
可视化面板我们平时不会经常使用,大家可以自己测试玩玩即可!
到了这里我们算是入门了Docker!
容器数据卷
DockerFile
Docker 网络
Docker Compose
Docker Swarm
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。