Docker 基本操作命令
一、docker网址
官网 :https://docs.docker.com/ 镜像地址:https://hub.docker.com/
docker组成
一、centos 安装docker
1、如果之前安装过docker,则清除掉
yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
2、安装docker ,地址使用阿里云镜像
sudo yum install -y yum-utils
镜像地址
sudo yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新索引
yum makecache fast
安装引擎
sudo yum install docker-ce docker-ce-cli containerd.io
3、更新软件包的索引
yum makecache fast
4、启动docker
systemctl start docker
二、镜像命令
1、显示版本信息
docker version
2、显示更加详细信息
docker info
3、docker帮助命令
docker --help
4、查看所有镜像
docker images
5、搜索镜像
docker search mysql #搜索所有镜像
docker search mysql --filter=Stars=800 #详细搜索镜像
6、下载镜像
docker pull mysql #下载最新镜像 docker pull 镜像名:[tag] #下载指定镜像 docker pull mysql:5.7
7、删除镜像
docker rmi -f 镜像id #删除一个镜像
docker rmi -f $(docker images -aq) # 删除全部镜像
三、容器命令
1、拉去镜像
docker pull redis # 下载镜像
2、运行镜像
docker run [可选参数] image
#参数说明
--name="Name" 容器名字 redis1,redis2,用来区分容器
-d 以后台方式运行
-it 使用交互方式运行
-p 指定容器的端口 3366:80 (主机端口:容器端口)
-P 随机指定端口
[root@xiaoyaodijun ~]# docker run -it centos /bin/bash //启动并进入容器 [root@939a2a495ab8 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@939a2a495ab8 /]# ^C [root@939a2a495ab8 /]# exit //退出容器 exit [root@xiaoyaodijun ~]#
3、进入容器内部
docker run -it centos /bin/hash #启动并进入容器内部
docker run -d centos /bin/hash #后台运行,启动并进入容器内部
4、指定映射端口
docker run -d --name nginx01 -p 3344:80 nginx # --name 容器的名字 -p 3344 主机的IP,80容器的id ,主机的3344端口对应容器的80端口
5、列出容器
docker ps -a #列出正在运行的容器+历史容器 docker ps #列出所有正在运行的容器 docker ps -a -n=1 #最近运行的一个容器 docker ps -q #获取容器编号
6、从容器里面退出
exit #退出容器,并停止
ctrl+p+q #推出容器,但不停止
7、删除容器
docker rm 容器id #不能删除正在运行的容器 docker rm -f $(docker ps -aq) #删除所有容器 docker ps -a -q|xargs docker rm #删除所有容器
8、启动与停止容器
docker start 容器id #启动
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id # 停止当前正在运行的容器
四、其他常用命令
docker run -d 镜像名 #docker ps 发现容器停止了,容器使用后台运行,必须要有一个前台线程。docker发现没有应用,就会自动停止 docker logs -f -t --tail (num) 容器名 #显示指定条数日志 docker logs -f -t 容器名 #显示所有日志
2、查看容器中的进程信息
docker top 容器id # 命令
docker inspect 容器id #查看容器内部信息
[root@xiaoyaodijun ~]# docker inspect c24821f8192d [ { "Id": "c24821f8192d427ee130380b2881d8e8466773d7e852efcfba5a5e8df436ede5", "Created": "2021-05-11T12:15:19.544112765Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 24209, "ExitCode": 0, "Error": "", "StartedAt": "2021-08-23T23:53:56.661123129Z", "FinishedAt": "2021-08-23T23:53:30.599217172Z" }, "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55", "ResolvConfPath": "/var/lib/docker/containers/c24821f8192d427ee130380b2881d8e8466773d7e852efcfba5a5e8df436ede5/resolv.conf", "HostnamePath": "/var/lib/docker/containers/c24821f8192d427ee130380b2881d8e8466773d7e852efcfba5a5e8df436ede5/hostname", "HostsPath": "/var/lib/docker/containers/c24821f8192d427ee130380b2881d8e8466773d7e852efcfba5a5e8df436ede5/hosts", "LogPath": "/var/lib/docker/containers/c24821f8192d427ee130380b2881d8e8466773d7e852efcfba5a5e8df436ede5/c24821f8192d427ee130380b2881d8e8466773d7e852efcfba5a5e8df436ede5-json.log", "Name": "/dotCentos", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": [ "/home/web/bin:/home/web/", "/home/web/log:/var/log/nginx/" ], "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": { "80/tcp": [ { "HostIp": "", "HostPort": "3366" } ] }, "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/79b88e2d39fa5bcd891e8cd00730cde0ad5d6c9cddd38584746690b9b410cf58-init/diff:/var/lib/docker/overlay2/c6963da9780a28bd63b97c8954fc3c198471e3b8304cf556da3d6b7ef867535f/diff", "MergedDir": "/var/lib/docker/overlay2/79b88e2d39fa5bcd891e8cd00730cde0ad5d6c9cddd38584746690b9b410cf58/merged", "UpperDir": "/var/lib/docker/overlay2/79b88e2d39fa5bcd891e8cd00730cde0ad5d6c9cddd38584746690b9b410cf58/diff", "WorkDir": "/var/lib/docker/overlay2/79b88e2d39fa5bcd891e8cd00730cde0ad5d6c9cddd38584746690b9b410cf58/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "bind", "Source": "/home/web/bin", "Destination": "/home/web", "Mode": "", "RW": true, "Propagation": "rprivate" }, { "Type": "bind", "Source": "/home/web/log", "Destination": "/var/log/nginx", "Mode": "", "RW": true, "Propagation": "rprivate" } ], "Config": { "Hostname": "c24821f8192d", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "ExposedPorts": { "80/tcp": {} }, "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": "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": "2f6e4a032ca49c8b0d6950580d2332cc97e4bc606fe13de65363f6734e291075", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "80/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "3366" }, { "HostIp": "::", "HostPort": "3366" } ] }, "SandboxKey": "/var/run/docker/netns/2f6e4a032ca4", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "11eb690c584a15da74029fb4de7e677f2bda1ce4d4b8eee66dc19b0e39ae97be", "Gateway": "172.18.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.18.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:12:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "c3caef73c8d7dc46ee889e0c28ae06990fb26bee16396badf6dd8e7adc0daf05", "EndpointID": "11eb690c584a15da74029fb4de7e677f2bda1ce4d4b8eee66dc19b0e39ae97be", "Gateway": "172.18.0.1", "IPAddress": "172.18.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:12:00:02", "DriverOpts": null } } } } ]
3、进入当前正在运行的容器
dcoker exec -it 容器id /bin/bash #以交互模式(-it)进入容器内部,进入容器后,开启一个新的终端
[root@xiaoyaodijun ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c24821f8192d centos "/bin/bash" 3 months ago Up About an hour 0.0.0.0:3366->80/tcp, :::3366->80/tcp dotCentos [root@xiaoyaodijun ~]# docker exec -it c24821f8192d /bin/bash [root@c24821f8192d /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@c24821f8192d /]#
docker attach 容器id #进入容器后正在执行的终端,不会启动一个新的进程
[root@xiaoyaodijun ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c24821f8192d centos "/bin/bash" 3 months ago Up 2 hours 0.0.0.0:3366->80/tcp, :::3366->80/tcp dotCentos [root@xiaoyaodijun ~]# [root@xiaoyaodijun ~]# docker attach c24821f8192d [root@c24821f8192d /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@c24821f8192d /]#
4、从容器拷贝文件到主机
docker cp 容器id:容器内路径 主机路径 #docker cp 容器id:/home/test/py /etc
[root@xiaoyaodijun ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c24821f8192d centos "/bin/bash" 3 months ago Up 9 seconds 0.0.0.0:3366->80/tcp, :::3366->80/tcp dotCentos [root@xiaoyaodijun ~]# docker attach c24821f8192d [root@c24821f8192d /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@c24821f8192d /]# cd home [root@c24821f8192d home]# ls web [root@xiaoyaodijun ~]# docker cp c24821f8192d:/home/web /var //将文件拷贝出来 [root@xiaoyaodijun ~]# cd ../ [root@xiaoyaodijun /]# cd /var [root@xiaoyaodijun var]# ls account cache db ftp gopher lib lock mail opt run tmp yp adm crash empty games kerberos local log nis preserve spool web
5、运行下载的容器并指定端口
docker run -d --name nginx01 -p 3344:80 nginx # -d 后台运行 --name 容器的名字 -p 3344 主机的IP,80容器的id ,主机的3344端口对应容器的80端口
curl localhost:3344 #测试访问
[root@xiaoyaodijun ~]# docker run -d --name nginx01 -p 3366:80 nginx 99366f3aadfff5976e7663f4be235dd82b587b534df26472f53cc64575489cd5 [root@xiaoyaodijun ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 99366f3aadff nginx "/docker-entrypoint.…" 4 seconds ago Up 2 seconds 0.0.0.0:3366->80/tcp, :::3366->80/tcp nginx01 [root@xiaoyaodijun ~]# [root@xiaoyaodijun ~]# [root@xiaoyaodijun ~]# [root@xiaoyaodijun ~]# [root@xiaoyaodijun ~]# cur localhost:3366 -bash: cur: command not found [root@xiaoyaodijun ~]# curl localhost:3366 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
docker exec -it nginx01 /bin/bash #进入容器内部
[root@xiaoyaodijun ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9a89dbea9358 nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:3366->80/tcp, :::3366->80/tcp nginx02 [root@xiaoyaodijun ~]# [root@xiaoyaodijun ~]# docker exec -it nginx02 /bin/bash root@9a89dbea9358:/#
centos镜像容器启动
[root@xiaoyaodijun ~]# docker run -dit --name dotnetCentos -p 8080:80 centos 39479285d597d7dec795f9ff19c05d887c0dcbf3f3ec8f122df7f0a0e7aee02d [root@xiaoyaodijun ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 39479285d597 centos "/bin/bash" 4 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp dotnetCentos [root@xiaoyaodijun ~]#
6、查看cpu状态
docker stats
五、图形化工具
docker run -d -p 8088:9000 \ --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer