1 docker安装
yum install docker [root@topcheer ~]# systemctl start docker [root@topcheer ~]# mkdir -p /etc/docker [root@topcheer ~]# vim /etc/docker/daemon.json #配置阿里云镜像加速 { "registry-mirrors": ["XXXXXXXXXXXXXXXX"] } [root@topcheer ~]# systemctl daemon-reload #加载配置文件 [root@topcheer ~]# systemctl restart docker #重启 [root@topcheer ~]#
2 docker命令
2.1 docker帮助命令
-
docker version
1 [root@topcheer ~]# docker version 2 Client: 3 Version: 1.13.1 4 API version: 1.26 5 Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64 6 Go version: go1.10.3 7 Git commit: 7f2769b/1.13.1 8 Built: Sun Sep 15 14:06:47 2019 9 OS/Arch: linux/amd64 10 11 Server: 12 Version: 1.13.1 13 API version: 1.26 (minimum version 1.12) 14 Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64 15 Go version: go1.10.3 16 Git commit: 7f2769b/1.13.1 17 Built: Sun Sep 15 14:06:47 2019 18 OS/Arch: linux/amd64 19 Experimental: false 20 [root@topcheer ~]#
-
docker info
1 [root@topcheer ~]# docker info 2 Containers: 1 3 Running: 0 4 Paused: 0 5 Stopped: 1 6 Images: 1 7 Server Version: 1.13.1 8 Storage Driver: overlay2 9 Backing Filesystem: xfs 10 Supports d_type: true 11 Native Overlay Diff: true 12 Logging Driver: journald 13 Cgroup Driver: systemd 14 Plugins: 15 Volume: local 16 Network: bridge host macvlan null overlay 17 Swarm: inactive 18 Runtimes: docker-runc runc 19 Default Runtime: docker-runc 20 Init Binary: /usr/libexec/docker/docker-init-current 21 containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1) 22 runc version: 9c3c5f853ebf0ffac0d087e94daef462133b69c7 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f) 23 init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574) 24 Security Options: 25 seccomp 26 WARNING: You're not using the default seccomp profile 27 Profile: /etc/docker/seccomp.json 28 selinux 29 Kernel Version: 3.10.0-957.el7.x86_64 30 Operating System: CentOS Linux 7 (Core) 31 OSType: linux 32 Architecture: x86_64 33 Number of Docker Hooks: 3 34 CPUs: 4 35 Total Memory: 1.777 GiB 36 Name: topcheer 37 ID: SR5A:YSH6:3YGH:YEZ4:PWLB:EEVE:3L5S:Z5AR:ARIA:SDGX:CZI5:MJ7O 38 Docker Root Dir: /var/lib/docker 39 Debug Mode (client): false 40 Debug Mode (server): false 41 Registry: https://index.docker.io/v1/ 42 Experimental: false 43 Insecure Registries: 44 127.0.0.0/8 45 Registry Mirrors: 46 https://lara9y80.mirror.aliyuncs.com 47 Live Restore Enabled: false 48 Registries: docker.io (secure) 49 [root@topcheer ~]#
-
docker --help
[root@topcheer ~]# docker --help Usage: docker COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -D, --debug Enable debug mode --help Print usage -H, --host list Daemon socket(s) to connect to (default []) -l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") --tlskey string Path to TLS key file (default "/root/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: container Manage containers image Manage images network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker volume Manage volumes Commands: attach Attach to a running container build Build an image from a 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 on a container's filesystem 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 history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a 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 push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images 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 wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command. [root@topcheer ~]#
2.2 镜像命令
-
docker images
[root@topcheer ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/hello-world latest fce289e99eb9 8 months ago 1.84 kB [root@topcheer ~]# REPOSITORY:表示镜像的仓库源 TAG:镜像的标签 IMAGE ID:镜像ID CREATED:镜像创建时间 SIZE:镜像大小 同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。 如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像
-
docker search
[root@topcheer ~]# docker search redis INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/redis Redis is an open source key-value store th... 7342 [OK] docker.io docker.io/bitnami/redis Bitnami Redis Docker Image 127 [OK] docker.io docker.io/sameersbn/redis 77 [OK] docker.io docker.io/grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0 & 5.0 56 docker.io docker.io/rediscommander/redis-commander Alpine image for redis-commander - Redis m... 31 [OK] docker.io docker.io/kubeguide/redis-master redis-master with "Hello World!" 29 docker.io docker.io/redislabs/redis Clustered in-memory database engine compat... 23 docker.io docker.io/arm32v7/redis Redis is an open source key-value store th... 17 docker.io docker.io/redislabs/redisearch Redis With the RedisSearch module pre-load... 17 docker.io docker.io/oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Su... 15 docker.io docker.io/webhippie/redis Docker images for Redis 10 [OK] docker.io docker.io/s7anley/redis-sentinel-docker Redis Sentinel 9 [OK] docker.io docker.io/insready/redis-stat Docker image for the real-time Redis monit... 8 [OK] docker.io docker.io/redislabs/redisgraph A graph database module for Redis 8 [OK] docker.io docker.io/arm64v8/redis Redis is an open source key-value store th... 6 docker.io docker.io/bitnami/redis-sentinel Bitnami Docker Image for Redis Sentinel 6 [OK] docker.io docker.io/centos/redis-32-centos7 Redis in-memory data structure store, used... 4 docker.io docker.io/redislabs/redismod An automated build of redismod - latest Re... 4 [OK] docker.io docker.io/circleci/redis CircleCI images for Redis 2 [OK] docker.io docker.io/frodenas/redis A Docker Image for Redis 2 [OK] docker.io docker.io/runnable/redis-stunnel stunnel to redis provided by linking conta... 1 [OK] docker.io docker.io/tiredofit/redis Redis Server w/ Zabbix monitoring and S6 O... 1 [OK] docker.io docker.io/wodby/redis Redis container image with orchestration 1 [OK] docker.io docker.io/cflondonservices/redis Docker image for running redis 0 docker.io docker.io/xetamus/redis-resource forked redis-resource 0 [OK] [root@topcheer ~]#
-
docker pull
[root@topcheer ~]# docker pull docker.io/redis Using default tag: latest Trying to pull repository docker.io/library/redis ... latest: Pulling from docker.io/library/redis b8f262c62ec6: Pull complete 93789b5343a5: Pull complete 49cdbb315637: Pull complete 2c1ff453e5c9: Pull complete 9341ee0a5d4a: Pull complete 770829e1df34: Pull complete Digest: sha256:5dcccb533dc0deacce4a02fe9035134576368452db0b4323b98a4b2ba2d3b302 Status: Downloaded newer image for docker.io/redis:latest [root@topcheer ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/redis latest 63130206b0fa 9 days ago 98.2 MB docker.io/hello-world latest fce289e99eb9 8 months ago 1.84 kB [root@topcheer ~]#
-
docker rmi
[root@topcheer ~]# docker rmi 63130206b0fa Untagged: docker.io/redis:latest Untagged: docker.io/redis@sha256:5dcccb533dc0deacce4a02fe9035134576368452db0b4323b98a4b2ba2d3b302 Deleted: sha256:63130206b0fa808e4545a0cb4a1f14f6d40b8a7e2e6fda0a31fd326c2ac0971c Deleted: sha256:9476758634326bb436208264d0541e9a0d42e4add35d00c2a7408f810223013d Deleted: sha256:0f3d9de16a216bfa5e2c2bd0e3c2ba83afec01a1b326d9f39a5ea7aecc112baf Deleted: sha256:452d665d4efca3e6067c89a332c878437d250312719f9ea8fff8c0e350b6e471 Deleted: sha256:d6aec371927a9d4bfe4df4ee8e510624549fc08bc60871ce1f145997f49d4d37 Deleted: sha256:2957e0a13c30e89650dd6c00644c04aa87ce516284c76a67c4b32cbb877de178 Deleted: sha256:2db44bce66cde56fca25aeeb7d09dc924b748e3adfe58c9cc3eb2bd2f68a1b68 [root@topcheer ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/hello-world latest fce289e99eb9 8 months ago 1.84 kB [root@topcheer ~]#
2.3 容器命令
-
docker run
OPTIONS说明(常用):有些是一个减号,有些是两个减号 --name="容器新名字": 为容器指定一个名称; -d: 后台运行容器,并返回容器ID,也即启动守护式容器; -i:以交互模式运行容器,通常与 -t 同时使用; -t:为容器重新分配一个伪输入终端,通常与 -i 同时使用; -P: 随机端口映射; -p: 指定端口映射,有以下四种格式 ip:hostPort:containerPort ip::containerPort hostPort:containerPort containerPort [root@topcheer ~]# docker run -it centos /bin/bash [root@3d2a94b63807 /]# cd / [root@3d2a94b63807 /]# ll
-
docker ps
OPTIONS说明(常用): -a :列出当前所有正在运行的容器+历史上运行过的 -l :显示最近创建的容器。 -n:显示最近n个创建的容器。 -q :静默模式,只显示容器编号。 --no-trunc :不截断输出。 退出容器 exit:容器停止退出 crtl p q容器不停止退出 [root@topcheer ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3d2a94b63807 centos "/bin/bash" 3 minutes ago Up 3 minutes nostalgic_darwin [root@topcheer ~]#
-
docker stop
root@topcheer ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3d2a94b63807 centos "/bin/bash" 3 minutes ago Up 3 minutes nostalgic_darwin [root@topcheer ~]# docker stop 3d2a94b63807 3d2a94b63807
-
docker start
[root@topcheer ~]# docker start 3d2a94b63807 3d2a94b63807 [root@topcheer ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3d2a94b63807 centos "/bin/bash" 6 minutes ago Up 17 seconds nostalgic_darwin [root@topcheer ~]#
-
docker rm
[root@topcheer ~]# docker rm -f $(docker ps -a -q) 3d2a94b63807 299b22d3d143 [root@topcheer ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@topcheer ~]#
-
docker run -d
[root@topcheer ~]# docker run -d centos 3c618cadb296fd013384201958f175085395a505a0aa1f7727e3c24b744b0b7f [root@topcheer ~]# 问题:然后docker ps -a 进行查看, 会发现容器已经退出 很重要的要说明的一点: Docker容器后台运行,就必须有一个前台进程. 容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。 这个是docker的机制问题,比如你的web容器,我们以nginx为例,正常情况下,我们配置启动服务只需要启动响应的service即可。例如 service nginx start 但是,这样做,nginx为后台进程模式运行,就导致docker前台没有运行的应用, 这样的容器后台启动后,会立即自杀因为他觉得他没事可做了. 所以,最佳的解决方案是,将你要运行的程序以前台进程的形式运行
-
docker logs
* -t 是加入时间戳 * -f 跟随最新的日志打印 * --tail 数字 显示最后多少条 [root@topcheer ~]# docker run -d centos /bin/sh -c "while true;do echo hello zzyy;sleep 2;done" 6c4bb3ce4c35a5380b553e686b806a1581bfb8dd0a115f63fa9b14da6195e667 [root@topcheer ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6c4bb3ce4c35 centos "/bin/sh -c 'while..." 6 seconds ago Up 4 seconds eloquent_shannon 3c618cadb296 centos "/bin/bash" About a minute ago Exited (0) About a minute ago upbeat_jepsen [root@topcheer ~]# docker logs -f -t --tail 6c4bb3ce4c35 "docker logs" requires exactly 1 argument(s). See 'docker logs --help'. Usage: docker logs [OPTIONS] CONTAINER Fetch the logs of a container [root@topcheer ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6c4bb3ce4c35 centos "/bin/sh -c 'while..." 47 seconds ago Up 46 seconds eloquent_shannon 3c618cadb296 centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago upbeat_jepsen [root@topcheer ~]# docker logs -tf --tail10 6c4bb3ce4c35 unknown flag: --tail10 See 'docker logs --help'. [root@topcheer ~]# docker logs -tf --tail 10 6c4bb3ce4c35 2019-09-22T10:23:14.595414000Z hello zzyy 2019-09-22T10:23:16.597109000Z hello zzyy 2019-09-22T10:23:18.600019000Z hello zzyy 2019-09-22T10:23:20.602673000Z hello zzyy 2019-09-22T10:23:22.605026000Z hello zzyy 2019-09-22T10:23:24.625059000Z hello zzyy
-
docker top 查看容器内运行的进程
[root@topcheer ~]# docker top 6c4bb3ce4c35 UID PID PPID C STIME TTY TIME CMD root 116050 116030 0 18:21 ? 00:00:00 /bin/sh -c while true;do echo hello zzyy;sleep 2;done root 116250 116050 2 18:25 ? 00:00:00 sleep 2 [root@topcheer ~]#
-
docker inspect 查看容器内部细节
[root@topcheer ~]# docker inspect 6c4bb3ce4c35 [ { "Id": "6c4bb3ce4c35a5380b553e686b806a1581bfb8dd0a115f63fa9b14da6195e667", "Created": "2019-09-22T10:21:57.924998005Z", "Path": "/bin/sh", "Args": [ "-c", "while true;do echo hello zzyy;sleep 2;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 116050, "ExitCode": 0, "Error": "", "StartedAt": "2019-09-22T10:21:58.43216616Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:67fa590cfc1c207c30b837528373f819f6262c884b7e69118d060a0c04d70ab8", "ResolvConfPath": "/var/lib/docker/containers/6c4bb3ce4c35a5380b553e686b806a1581bfb8dd0a115f63fa9b14da6195e667/resolv.conf", "HostnamePath": "/var/lib/docker/containers/6c4bb3ce4c35a5380b553e686b806a1581bfb8dd0a115f63fa9b14da6195e667/hostname", "HostsPath": "/var/lib/docker/containers/6c4bb3ce4c35a5380b553e686b806a1581bfb8dd0a115f63fa9b14da6195e667/hosts", "LogPath": "", "Name": "/eloquent_shannon", "RestartCount": 0, "Driver": "overlay2", "MountLabel": "system_u:object_r:svirt_sandbox_file_t:s0:c71,c940", "ProcessLabel": "system_u:system_r:svirt_lxc_net_t:s0:c71,c940", "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/d8d3dca6c9115b3c782bf358a744475e78f5e62b627cca79e10a34e754310933-init/diff:/var/lib/docker/overlay2/7bc85336eb8ca768f43d8eb3d5f27bdf35fa99068be45c84622d18c0f87c90bd/diff", "MergedDir": "/var/lib/docker/overlay2/d8d3dca6c9115b3c782bf358a744475e78f5e62b627cca79e10a34e754310933/merged", "UpperDir": "/var/lib/docker/overlay2/d8d3dca6c9115b3c782bf358a744475e78f5e62b627cca79e10a34e754310933/diff", "WorkDir": "/var/lib/docker/overlay2/d8d3dca6c9115b3c782bf358a744475e78f5e62b627cca79e10a34e754310933/work" } }, "Mounts": [], "Config": { "Hostname": "6c4bb3ce4c35", "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 hello zzyy;sleep 2;done" ], "Image": "centos", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20190801", "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": "d5f116b329f01e9bab7f985282fd568e379c8e7aa4fcc3677b9b025ded771149", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/d5f116b329f0", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "825091555bc0adfdf32667650884ec2b6274c44c787291870de32ec2cee8575b", "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": "fe000671b1b7f9a2e634f409bd33ada7bed50e818a28c1d9c8245aba67b1b625", "EndpointID": "825091555bc0adfdf32667650884ec2b6274c44c787291870de32ec2cee8575b", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02" } } } } ] [root@topcheer ~]#