Docker的三大核心概念:鏡像、容器、倉庫linux
鏡像:相似虛擬機的鏡像、用俗話說就是安裝文件。web
容器:相似一個輕量級的沙箱,容器是從鏡像建立應用運行實例,能夠將其啓動、開始、中止、刪除、而這些容器都是相互隔離、互不可見的。redis
倉庫:相似代碼倉庫,是Docker集中存放鏡像文件的場所。docker
使用yum安裝好docker後,設置開機啓動。centos
[root@CentOS ~]# systemctl enable docker
[root@localhost ~]# service docker restart Redirecting to /bin/systemctl restart docker.service [root@localhost ~]# service docker stop Redirecting to /bin/systemctl stop docker.service [root@localhost ~]# service docker start Redirecting to /bin/systemctl start docker.service
docker search <image>用於搜索線上鏡像倉庫,這樣就能夠搜索出在Docker Hub上全部帶redis的公共的可用鏡像。bash
[root@CentOS ~]# docker search redis INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/redis Redis is an open source key-value store th... 5472 [OK] docker.io docker.io/bitnami/redis Bitnami Redis Docker Image 82 [OK] docker.io docker.io/sameersbn/redis 68 [OK] docker.io docker.io/tenstartups/redis-commander 32 [OK] docker.io docker.io/hypriot/rpi-redis Raspberry Pi compatible redis image 31 docker.io docker.io/kubeguide/redis-master redis-master with "Hello World!" 25 docker.io docker.io/kubeguide/guestbook-redis-slave Guestbook redis slave 20 docker.io docker.io/redislabs/redis Clustered in-memory database engine compat... 15 docker.io docker.io/webhippie/redis Docker images for redis 9 [OK] docker.io docker.io/arm32v7/redis Redis is an open source key-value store th... 7 docker.io docker.io/rediscommander/redis-commander Alpine image for redis-commander - Redis m... 7 [OK] docker.io docker.io/oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Su... 5 docker.io docker.io/rtoma/logspout-redis-logstash Logspout including Redis adapter for sendi... 5 docker.io docker.io/centos/redis-32-centos7 Redis in-memory data structure store, used... 3 docker.io docker.io/dynomitedb/redis Redis backend for DynomiteDB. 2 [OK] ...
docker pull <image>拉取服務器鏡像倉庫裏的鏡像。服務器
[root@CentOS ~]# docker pull redis Using default tag: latest Trying to pull repository docker.io/library/redis ... latest: Pulling from docker.io/library/redis be8881be8156: Pull complete d6f5ea773ca3: Pull complete 735cc65c0db4: Pull complete ff89c30e4d8c: Pull complete 59bf782a86b3: Pull complete ce8aaa9fe90a: Pull complete Digest: sha256:096cff9e6024603decb2915ea3e501c63c5bb241e1b56830a52acfd488873843 Status: Downloaded newer image for docker.io/redis:latest
不指定版本,默認會下載最新的一個版本。因爲官方的鏡像在國外,有時網速較慢,因此推薦下載國內的鏡像,好比阿里雲,網易雲。網絡
docker push 推送本地鏡像到服務器。tcp
查看鏡像ide
docker images:列出images
docker images -a:列出全部的images(包含歷史)
docker images --tree :顯示鏡像的全部層(layer)
docker rmi <image ID>:刪除一個或多個image
上面介紹了docker鏡像的拉取,咱們接下來看下如何建立一個容器。
docker run 命令格式
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
-d, --detach=false 指定容器運行於前臺仍是後臺,默認爲false
-i, --interactive=false 打開STDIN,用於控制檯交互
-t, --tty=false 分配tty設備,該能夠支持終端登陸,默認爲false
-u, --user="" 指定容器的用戶
-a, --attach=[] 標準輸入輸出流和錯誤信息(必須是以非docker run -d啓動的容器)
-w, --workdir="" 指定容器的工做目錄
-c, --cpu-shares=0 設置容器CPU權重,在CPU共享場景使用
-e, --env=[] 指定環境變量,容器中可使用該環境變量
-m, --memory="" 指定容器的內存上限
-P, --publish-all=false 指定容器暴露的端口
-p, --publish=[] 指定容器暴露的端口
-h, --hostname="" 指定容器的主機名
-v, --volume=[] 給容器掛載存儲卷,掛載到容器的某個目錄
--volumes-from=[] 給容器掛載其餘容器上的卷,掛載到容器的某個目錄
--cap-add=[] 添加權限,權限清單詳見:http://linux.die.net/man/7/capabilities
--cap-drop=[] 刪除權限,權限清單詳見:http://linux.die.net/man/7/capabilities
--cidfile="" 運行容器後,在指定文件中寫入容器PID值,一種典型的監控系統用法
--cpuset="" 設置容器可使用哪些CPU,此參數能夠用來容器獨佔CPU
--device=[] 添加主機設備給容器,至關於設備直通
--dns=[] 指定容器的dns服務器
--dns-search=[] 指定容器的dns搜索域名,寫入到容器的/etc/resolv.conf文件
--entrypoint="" 覆蓋image的入口點
--env-file=[] 指定環境變量文件,文件格式爲每行一個環境變量
--expose=[] 指定容器暴露的端口,即修改鏡像的暴露端口
--link=[] 指定容器間的關聯,使用其餘容器的IP、env等信息
--lxc-conf=[] 指定容器的配置文件,只有在指定--exec-driver=lxc時使用
--name="" 指定容器名字,後續能夠經過名字進行容器管理,links特性須要使用名字
--net="bridge" 容器網絡設置:
bridge 使用docker daemon指定的網橋
host //容器使用主機的網絡
container:NAME_or_ID >//使用其餘容器的網路,共享IP和PORT等網絡資源
none 容器使用本身的網絡(相似--net=bridge),可是不進行配置
--privileged=false 指定容器是否爲特權容器,特權容器擁有全部的capabilities
--restart="no" 指定容器中止後的重啓策略:
no:容器退出時不重啓
on-failure:容器故障退出(返回值非零)時重啓
always:容器退出時老是重啓
--rm=false 指定容器中止後自動刪除容器(不支持以docker run -d啓動的容器)
--sig-proxy=true 設置由代理接受並處理信號,可是SIGCHLD、SIGSTOP和SIGKILL不能被代理
實例:
[root@CentOS ~]# docker run -p 8090:8080 -p 50000:50000 --restart always --link Redis:redis --name jenkins -v /home/jenkins:/home/jenkins_home --privileged=true dokcer.io/jenkins/jenkins
-p:把容器的8080端口映射到宿主機8090上
-v:主機的目錄/home/jenkins映射到容器的目錄/home/jenkins_home
--name:給容器起個名字jenkins,docker.io/jenkins/jenkins是你下載的鏡像
--restart:always 容器退出時老是重啓
--privileged=true:掛載主機目錄Docker訪問出現Permission denied的解決辦法
--link:爲redis容器起個別名Redis,訪問的時候使用別名Redis
docker ps #查看正在運行的容器
[root@CentOS ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES faa1b0be4e0b docker.io/jenkins/jenkins "/sbin/tini -- /us..." 15 hours ago Up 4 seconds 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins
docker ps -a #查看全部容器
[root@CentOS ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 45af2f4d1f82 docker.io/redis "docker-entrypoint..." 15 hours ago Exited (255) 3 minutes ago 0.0.0.0:6378->6379/tcp redis1 ffa152fb76cc docker.io/redis "docker-entrypoint..." 15 hours ago Exited (255) 3 minutes ago 0.0.0.0:6379->6379/tcp redis faa1b0be4e0b docker.io/jenkins/jenkins "/sbin/tini -- /us..." 15 hours ago Up 2 minutes 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp jenkins
根據id和name對容器進行操做
#啓動容器 docker start <ContainerId(或者name)> #中止容器 docker stop <ContainerId(或者name)> #重啓容器 docker restart <ContainerId(或者name)> #刪除容器 docker rm <ContainerId(或者name)> #刪除全部容器 docker rm $(docker ps -a -q)
#進入容器 docker exec -it containerID /bin/bash
ctrl+d 退出容器且關閉
ctrl+p+q 退出容器但不關閉
docker logs -f -t --tail 行數 容器名
[root@CentOS ~]# docker logs -f -t --tail 10 redis #查看容器最後10行日誌 2018-08-09T05:55:16.204504000Z 1:C 09 Aug 05:55:16.135 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 2018-08-09T05:55:16.205232000Z 1:C 09 Aug 05:55:16.151 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=1, just started 2018-08-09T05:55:16.205822000Z 1:C 09 Aug 05:55:16.151 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 2018-08-09T05:55:16.206545000Z 1:M 09 Aug 05:55:16.199 * Running mode=standalone, port=6379. 2018-08-09T05:55:16.207152000Z 1:M 09 Aug 05:55:16.200 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 2018-08-09T05:55:16.207713000Z 1:M 09 Aug 05:55:16.200 # Server initialized 2018-08-09T05:55:16.208315000Z 1:M 09 Aug 05:55:16.200 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 2018-08-09T05:55:16.209055000Z 1:M 09 Aug 05:55:16.201 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 2018-08-09T05:55:16.209806000Z 1:M 09 Aug 05:55:16.201 * DB loaded from disk: 0.000 seconds 2018-08-09T05:55:16.210405000Z 1:M 09 Aug 05:55:16.202 * Ready to accept connections
#在主機上建立一個網絡 docker network create mynet #查看自定義bridge網絡 docker network inspect mynet #移除網絡要求網絡中全部的容器關閉或斷開與此網絡的鏈接時,纔可以使用移除命令 docker network disconnet mynet 容器ID #移除網絡 docker network rm mynet