容器命令
说明:有了镜像才可以创建容器,linux 下载一个centos镜像来测试学习
docker pull centos
新建容器并启动:
docker run [可选参数] image
# 参数说明
--name="Name" 容器名字 比如用 tomcat01 tomcat02用来区分容器
-d 以后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
-P 随机指定端口
# 测试 启动并进入容器
[root@kuangshen /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 5 months ago 209MB
[root@kuangshen /]# docker run -it centos /bin/bash # 启动并进入容器
[root@3d251cf3eb8c /]# ls # 查看容器内的centos ,基础版本,很多命令是不完善的!
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 从容器中退回主机
[root@3d251cf3eb8c /]# exit
exit
[root@kuangshen /]# ls
bin boot dev etc home lib lib64 lost+found media mnt opt patch proc root run sbin srv sys tmp usr var www
列出所有运行的容器
# docker ps命令
# 列出当前正在运行的容器
-a # 列出当前正在运行的容器+带历史运行过的容器
-n=? #显示最近创建的容器
-q #只显示容器的编号
[root@kuangshen /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@kuangshen /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d251cf3eb8c centos "/bin/bash" 9 minutes ago Exited (0) 7 minutes ago sharp_merkle
6def942f2017 d1165f221234 "/hello" 22 hours ago Exited (0) 22 hours ago nifty_kapitsa
1035a714db40 d1165f221234 "/hello" 2 weeks ago Exited (0) 2 weeks ago cool_faraday
[root@kuangshen /]# docker ps -a -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d251cf3eb8c centos "/bin/bash" 10 minutes ago Exited (0) 8 minutes ago sharp_merkle
[root@kuangshen /]# docker ps -aq
3d251cf3eb8c
6def942f2017
1035a714db40
退出容器
exit #直接容器停止并退出
Ctrl + P + Q #容器不停止退出
删除容器
docker rm 容器id # 删除指定的容器 不能删除正在运行的容器,如果要强制删除 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 镜像名!
[root@kuangshen /]# docker run -d centos
# 问题docker ps 发现centos 停止了
# 常见的坑:docker容器使用后台运行,就必须要有前台进程, docker发现没有应用,就会自动停止
# 比如docker 安装nginx, 容器启动后, 发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs -f -t --tail 容器 #问题 发现没有日志
#自己编写一段shell脚本
[root@kuangshen /]# docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 1;done"
[root@kuangshen /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8364489385d5 centos "/bin/sh -c 'while t…" 6 seconds ago Up 5 seconds infallible_gauss
# 显示日志
-tf #显示日志
--tail number #显示日志条数
[root@kuangshen /]# docker logs -tf --tail 10 8364489385d5
[root@kuangshen /]# docker logs -tf 8364489385d5
退出的命令ctrl + c
查看容器中进程信息
# 命令 docker top 容器id
[root@kuangshen /]# docker top 8364489385d5
UID (当前用户id) PID (进程id) PPID (父进程id) C STIME TTY TIME CMD
root 29546 29527 0 16:04 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1;done
root 31081 29546 0 16:13 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看镜像的元数据
# 命令
docker inspect 容器id
#测试
[root@kuangshen /]# docker inspect 8364489385d5
[
{
"Id": "8364489385d5ada3ec543c7d2daa9b7d1b642ed77d8256a32ee13977cb739eb5",
"Created": "2021-05-22T08:04:44.71016798Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo kuangshen;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 29546,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-05-22T08:04:44.981631883Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/8364489385d5ada3ec543c7d2daa9b7d1b642ed77d8256a32ee13977cb739eb5/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/8364489385d5ada3ec543c7d2daa9b7d1b642ed77d8256a32ee13977cb739eb5/hostname",
"HostsPath": "/var/lib/docker/containers/8364489385d5ada3ec543c7d2daa9b7d1b642ed77d8256a32ee13977cb739eb5/hosts",
"LogPath": "/var/lib/docker/containers/8364489385d5ada3ec543c7d2daa9b7d1b642ed77d8256a32ee13977cb739eb5/8364489385d5ada3ec543c7d2daa9b7d1b642ed77d8256a32ee13977cb739eb5-json.log",
"Name": "/infallible_gauss",
"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/36ce7336b625dc91b851073b53dde0ee0a64287fac8b95fd9eb2907987796c7d-init/diff:/var/lib/docker/overlay2/38899cec0f330813fef543cb03620c44cad0c7817e60df4c85cd8334f0ac1f90/diff",
"MergedDir": "/var/lib/docker/overlay2/36ce7336b625dc91b851073b53dde0ee0a64287fac8b95fd9eb2907987796c7d/merged",
"UpperDir": "/var/lib/docker/overlay2/36ce7336b625dc91b851073b53dde0ee0a64287fac8b95fd9eb2907987796c7d/diff",
"WorkDir": "/var/lib/docker/overlay2/36ce7336b625dc91b851073b53dde0ee0a64287fac8b95fd9eb2907987796c7d/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "8364489385d5",
"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 kuangshen;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": "a24c87a171fc0c290ba769746a28eb192a4ac9a3fa4565922c2d40783b9a0caf",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/a24c87a171fc",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "33a8ae7e486ece9024c0390062c8599b2c4f2eb0c209eb6057580ee94f265a28",
"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": "67bad398d895c5e78c16801f5a48642bfcf10513650a3f67fcde6b7d81a976f7",
"EndpointID": "33a8ae7e486ece9024c0390062c8599b2c4f2eb0c209eb6057580ee94f265a28",
"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
}
}
}
}
]
进入当前正在运行的容器
# 容器通常都是使用后台方式运行的,需要进入容器,修改一些配置
# 命令
docker exec -it 容器id bashShell
#测试
[root@kuangshen /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8364489385d5 centos "/bin/sh -c 'while t…" 19 minutes ago Up 19 minutes infallible_gauss
[root@kuangshen /]# docker exec -it 8364489385d5 /bin/bash
[root@8364489385d5 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@8364489385d5 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 08:04 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1;done
root 1439 0 0 08:28 pts/0 00:00:00 /bin/bash
root 1485 1 0 08:29 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 1486 1439 0 08:29 pts/0 00:00:00 ps -ef
[root@8364489385d5 /]#
#方式2
docker attach 容器id
# 测试
[root@kuangshen /]# docker attach 8364489385d5
进去发现正在执行当前的代码
#docker exec # 进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach #进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机
docker cp 容器id: 容器内路径 目的的主机路径
# 查看当前主机目录下
[root@kuangshen /]# cd /home
[root@kuangshen home]# ls
apache-tomcat-9.0.44-windows-x64 apache-tomcat-9.0.45.tar.gz kuangshen qinjiang swagger-demo-0.0.1-SNAPSHOT.jar
apache-tomcat-9.0.45 jdk-8u291-linux-x64.rpm kuangshen.java redis www
[root@kuangshen home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0c2dfc68623 centos "/bin/bash" About a minute ago Up About a minute confident_kowalevski
# 进入docker容器内部
[root@kuangshen home]# docker attach a0c2dfc68623
[root@a0c2dfc68623 /]# cd /home
[root@a0c2dfc68623 home]# ls
# 在容器内新建一个文件
[root@a0c2dfc68623 home]# touch test.java
#退出
[root@a0c2dfc68623 home]# exit
exit
[root@kuangshen home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@kuangshen home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0c2dfc68623 centos "/bin/bash" 3 minutes ago Exited (0) 19 seconds ago confident_kowalevski
# 将这个文件拷贝出来到主机上
[root@kuangshen home]# docker cp a0c2dfc68623:/home/test.java /home
[root@kuangshen home]# ls
apache-tomcat-9.0.44-windows-x64 apache-tomcat-9.0.45.tar.gz kuangshen qinjiang swagger-demo-0.0.1-SNAPSHOT.jar www
apache-tomcat-9.0.45 jdk-8u291-linux-x64.rpm kuangshen.java redis test.java
[root@kuangshen home]#
#拷贝是一个手动过程,未来我们使用 -v 卷的技术,可以实现自动同步
attach Attach local standard input, output, and error streams to a running container
#当前shell下 attach连接指定运行的镜像
build Build an image from a Dockerfile # 通过Dockerfile定制镜像
commit Create a new image from a container's changes #提交当前容器为新的镜像
cp Copy files/folders between a container and the local filesystem #拷贝文件
create Create a new container #创建一个新的容器
diff Inspect changes to files or directories on a container's filesystem #查看docker容器的变化
events Get real time events from the server # 从服务获取容器实时时间
exec Run a command in a running container # 在运行中的容器上运行命令
export Export a container's filesystem as a tar archive #导出容器文件系统作为一个tar归档文件[对应import]
history Show the history of an image # 展示一个镜像形成历史
images List images #列出系统当前的镜像
import Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统镜像
info Display system-wide information # 显示全系统信息
inspect Return low-level information on Docker objects #查看容器详细信息
kill Kill one or more running containers # kill指定docker容器
load Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
login Log in to a Docker registry #注册或者登录一个docker源服务器
logout Log out from a Docker registry # 从当前Docker registry退出
logs Fetch the logs of a container #输出当前容器日志信息
pause Pause all processes within one or more containers #暂停容器
port List port mappings or a specific mapping for the container#查看映射端口对应的容器内部源端口
ps List containers # 列出容器列表
pull Pull an image or a repository from a registry# 从docker镜像源服务器拉去指定镜像或者库镜像
push Push an image or a repository to a registry #推送指定镜像或者库镜像至docker源服务器
rename Rename a container
restart Restart one or more containers #重启运行的容器
rm Remove one or more containers #移除一个或者多个容器
rmi Remove one or more images #移除一个或者多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续 -f强制删除]
run Run a command in a new container # 创建一个新的容器并运行一个命令
save Save one or more images to a tar archive (streamed to STDOUT by default)#保存一个镜像为一个tar包[对应load]
search Search the Docker Hub for images #在docker hub中搜索镜像
start Start one or more stopped containers #启动容器
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers #停止容器
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE #给源中镜像打标签
top Display the running processes of a container #查看容器中运行的进程信息
unpause Unpause all processes within one or more containers # 取消暂停容器
update Update configuration of one or more containers #
version Show the Docker version information # 查看docker版本号
wait Block until one or more containers stop, then print their exit codes#截取容器停止时的退出状态值
作业练习
Docker安装Nginx
网页搜索:nginx (docker.com)
# docker pull nginx 下载镜像
# docker images 查看是否有这个镜像
[root@kuangshen home]# docker run -d --name nginx01 -p 3344:80 nginx #通过公网的3344访问docker的80端口 nginx默认端口是80
[root@kuangshen home]# docker pull nginx #下载镜像
Using default tag: latest
latest: Pulling from library/nginx
69692152171a: Pull complete
49f7d34d62c1: Pull complete
5f97dc5d71ab: Pull complete
cfcd0711b93a: Pull complete
be6172d7651b: Pull complete
de9813870342: Pull complete
Digest: sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@kuangshen home]# docker images #查看是否有这个镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f0b8a9a54136 10 days ago 133MB
centos latest 300e315adb2f 5 months ago 209MB
# -d 后台运行
# --name 给容器命令
# -p 宿主机端口:容器内部端口
[root@kuangshen home]# docker run -d --name nginx01 -p 3344:80 nginx#通过公网的3344访问docker的80端口 nginx默认端口是80
1f7179335d7d1f3aa420f3b7adb439cb79cf0322e37350fbc18dee2a77c4e24a
[root@kuangshen home]# docker ps # 查看容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f7179335d7d nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
[root@kuangshen home]# curl localhost:3344 #查看本机
端口暴露的概念:
[root@kuangshen home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1f7179335d7d nginx "/docker-entrypoint.…" 13 minutes ago Up 13 minutes 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
[root@kuangshen home]# docker exec -it nginx01 /bin/bash # 进入容器
root@1f7179335d7d:/# whereis nginx #查看nginx的配置文件
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@1f7179335d7d:/# cd /etc/nginx # 查看配置文件
root@1f7179335d7d:/etc/nginx# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
root@1f7179335d7d:/etc/nginx#
使用网页访问3344端口 阿里云公网地址:3344
[root@kuangshen home]# docker stop 1f7179335d7d
1f7179335d7d
思考题:每次改动nginx配置文件,都需要进入容器内部, 十分麻烦,如果可以在容器外部提供一个映射路径,达到在容器外部修改文件,容器内部就可以自动修改? -v 数据卷技术
B站学习网址:【狂神说Java】Docker最新超详细版教程通俗易懂_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili