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 ~]#
docker versionhtml
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 infojava
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 --helpnode
[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 ~]#
docker imagesmysql
[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 searchlinux
[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 pullnginx
[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 rmigit
[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 ~]#
docker runweb
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 psredis
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 stopsql
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 ~]#