有了镜像才可以创建容器,下载一个centos镜像来测试学习
下载镜像
docker pull centos
新建容器并启动
docker run [可选参数] image
参数说明
--name='Name' 容器名字,用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p 主机端口映射:容器端口(常用)
-p 直接容器端口
-P 随机指定端口(大写)
查看docker run帮助命令
docker run --help
查看下载的镜像
docker images
测试启动并进入容器
docker run -it centos /bin/bash
#主机名称变更
cd /
ls
查看容器内的centos
从容器中退回主机
exit #直接停止并退出
列出运行正在的容器
docker ps
列出所有运行过的容器
docker ps -a
显示最近创建的容器
docker ps -a -n=1
显示所有运行容器的编号
docker ps -aq
```![](https://img2020.cnblogs.com/blog/1488047/202011/1488047-20201119155254571-1335012752.png)
## 退出容器
exit
直接停止并退出
Ctrl+P+Q
容器不停止退出
![](https://img2020.cnblogs.com/blog/1488047/202011/1488047-20201119155341966-1527747864.png)
## 删除容器镜像
```shell
docker rm ID #删除指定的容器镜像,不能删除正在运行的容器镜像
docker rm -f $(docker ps -aq) #删除所有的容器镜像
docker ps -a -q|xargs docker rm -f #删除所有的容器镜像
centos8报错
启动停止容器
启动
docker start ID
重启
docker restart ID
停止当前正在运行容器
docker stop ID
强制停止当前运行容器
docker kill ID
后台启动
docker run -d 镜像名
问题:
1.Docker容器使用后台运行,就必须要有一个前台进程,Docker发现没有前台应用,就会自动停止
2.例:nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs
查看帮助命令
docker logs --help
首先启动容器镜像
docker run -it centos /bin/bash
写一点脚本
docker run -d centos /bin/sh -c "while true;do echo test;sleep 1;done"
查看日志
docker logs -tf ID
查看具体行的日志
docker logs -tf --tail N ID
进程信息
docker top ID
docker top ID
容器信息
docker inspect ID
帮助命令
Args:传递的参数
pid:进程
Mounts:挂载信息
Env:环境变量
[root@localhost ~]# docker inspect 072d6fb47015
[
{
"Id": "072d6fb47015c713a12a27f9dd9b52d140260fadc320c50415656f1547d3b056",
"Created": "2020-11-02T04:44:44.123849543Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2448,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-11-02T04:44:44.305600406Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
"ResolvConfPath": "/var/lib/docker/containers/072d6fb47015c713a12a27f9dd9b52d140260fadc320c50415656f1547d3b056/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/072d6fb47015c713a12a27f9dd9b52d140260fadc320c50415656f1547d3b056/hostname",
"HostsPath": "/var/lib/docker/containers/072d6fb47015c713a12a27f9dd9b52d140260fadc320c50415656f1547d3b056/hosts",
"LogPath": "/var/lib/docker/containers/072d6fb47015c713a12a27f9dd9b52d140260fadc320c50415656f1547d3b056/072d6fb47015c713a12a27f9dd9b52d140260fadc320c50415656f1547d3b056-json.log",
"Name": "/wonderful_carson",
"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/31c0a7dadc6ab0aac8610ef1cbc6453a7deb07b7e4041eb13d0fde9ffcc1fca2-init/diff:/var/lib/docker/overlay2/a3eea815ce2e49aeb13672555a2278994bc4d4510b0e7d8418f5966573d90b7b/diff",
"MergedDir": "/var/lib/docker/overlay2/31c0a7dadc6ab0aac8610ef1cbc6453a7deb07b7e4041eb13d0fde9ffcc1fca2/merged",
"UpperDir": "/var/lib/docker/overlay2/31c0a7dadc6ab0aac8610ef1cbc6453a7deb07b7e4041eb13d0fde9ffcc1fca2/diff",
"WorkDir": "/var/lib/docker/overlay2/31c0a7dadc6ab0aac8610ef1cbc6453a7deb07b7e4041eb13d0fde9ffcc1fca2/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "072d6fb47015",
"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/bash"
],
"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": "8b6a572d50123d07c047632ddc336df8a9f717e485a09e5b469d572a435514ce",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/8b6a572d5012",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "c75a2ed0b7f7a7de7315f9c0ce8d1a77324949ae192f5ca4d8a0040ec1581738",
"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": "697346d198aa6d39088da36bf3aa32eee7ef70ae170fa52118cb7ed8cc8993f6",
"EndpointID": "c75a2ed0b7f7a7de7315f9c0ce8d1a77324949ae192f5ca4d8a0040ec1581738",
"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
docker exec -it 容器ID bashshell
测试
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
072d6fb47015 centos "/bin/bash" 6 hours ago Up 6 hours wonderful_carson
[root@localhost ~]# docker exec -it 072d6fb47015 /bin/bash
[root@072d6fb47015 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@072d6fb47015 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 05:52 pts/0 00:00:00 /bin/bash
root 16 0 0 10:25 pts/1 00:00:00 /bin/bash
root 30 16 0 10:26 pts/1 00:00:00 ps -ef
[root@072d6fb47015 /]#
docker attach
docker attach 容器ID
比较:
docker exec 进入容器后开启一个新的终端,可以在里面操作(常用)
docker attach 进入容器正在执行的终端,不会启动新的进程!
从容器内拷贝文件到主机
docker cp
docker cp 容器ID:容器内路径 主机路径
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
072d6fb47015 centos "/bin/bash" 6 hours ago Up 6 hours wonderful_carson
#进入docker容器内部
[root@localhost ~]# docker attach 072d6fb47015
[root@072d6fb47015 /]# cd /home/
[root@072d6fb47015 home]# ls
#在容器内新建一个文件
[root@072d6fb47015 home]# touch test.txt
[root@072d6fb47015 home]# ls
test.txt
#退出容器进行拷贝测试,退出不影响文件的拷贝
[root@072d6fb47015 home]# exit
exit
#将文件拷贝到主机上
[root@localhost ~]# docker cp 072d6fb47015:/home/test.txt /home/
[root@localhost ~]# cd /home/
[root@localhost home]# ls
test.txt
[root@localhost home]#
小结
attach Attach to a running container #当前she 11 下attach连接指定运行镜像
build Bui1dan image from a Docker file #通过Docker file定制镜像
commit Create a new image from a container changes #提交当前容器为新的镜像
cp Copyfiles/fo1ders 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 at ar archive #导出容器的内容流作为一个tar归档文件[对应 import]
history show the history of an image #展示一个镜像形成历史
images List images #列出系统当前镜像
import create a new filesystem image from the contents of at arba 11 #从tar包中的内容创建一个新的文件系 统映像[对应export]
info Display system-wide information #显示系统相关信息
inspect Return 1ow-1eve 1 information on a container #查看容器详细信息
kill kill a running container #ki ll指定docker容器
load Load an image from at ar archive #从一个tar包中加载一个镜像[对应save]
login Register or Login to the docker registry server #注册或者登陆一个docker源服务器
logout Logout from a Docker registry server #从当前Docker registry退出
logs Fetch the 1ogsofa container #输出当前容器日志信息
port Lookup the pub1ic-facing port which is NAT-ed to PRIVATE_PORT #查看映射端口对应的容器内部源端 口
pause Pause a 11 processes within a container #暂停容器
ps List containers #列出容器列表
pull Pullanimageora 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 at ar archive #保存一个镜像为一个tar包[对应1oad]
search Search for an image on the Docker Hub #在docker hub中搜索镜像
start Start a stopped containers #启动容器
stop Stop a running containers #停止容器
tag Tagan image into a repository #给源中镜像打标签
top Lookup the running processes of a container #查看容器中运行的进程信息
un pause Un pause a paused container #取消暂停容器
version Show the docker version information #查看docker版本号
wait Block until a container stops, then print its exitcode #截取容器停止时的退出状态值