-
虛擬化技術(shù)與Docker區(qū)別
1、虛擬化技術(shù):將服務(wù)淋叶,網(wǎng)絡(luò),內(nèi)存伪阶,存儲(chǔ)等硬件抽象出來(lái)運(yùn)行的一整套完整操作系統(tǒng)煞檩。運(yùn)行進(jìn)程屬于虛擬系統(tǒng)。
2栅贴、Docker:通過一些linux內(nèi)核技術(shù)斟湃,對(duì)進(jìn)程進(jìn)行封裝、文件系統(tǒng)筹误,網(wǎng)絡(luò)隔離等,沒有獨(dú)立的存儲(chǔ)空間以及硬件資源癣缅,屬于相對(duì)于虛擬化技術(shù)輕量級(jí)厨剪。
基本概念
1、鏡像(image)
相當(dāng)于一個(gè)root文件系統(tǒng)友存,Docker的每個(gè)鏡像是分層存儲(chǔ)祷膳,每一層隸屬一個(gè)root文件系統(tǒng)層。相當(dāng)于java中的類屡立。
2直晨、容器(container)
以鏡像層為基礎(chǔ)層,在其上創(chuàng)建一個(gè)容器存儲(chǔ)層而獨(dú)立運(yùn)行的進(jìn)程膨俐。相當(dāng)于java類創(chuàng)建的實(shí)例勇皇。
3、倉(cāng)庫(kù)(repository)
用于分發(fā)鏡像焚刺,集中存儲(chǔ)的服務(wù)敛摘。類似于maven中央倉(cāng)庫(kù)。Docker 的中央鏡像服務(wù)器是Docker Hub乳愉。還有其他的Docker Registry 服務(wù)兄淫。國(guó)內(nèi)的鏡像服務(wù)倉(cāng)庫(kù)被稱為加速器屯远。比如網(wǎng)易云鏡像服務(wù),阿里云鏡像庫(kù)等捕虽。同時(shí)可以自己本地搭建Registry服務(wù)慨丐。Docker 安裝(如下是Centos7 64位安裝CE版本)
Docker 分為 CE 和 EE 兩大版本。CE 即社區(qū)版(免費(fèi)泄私,支持周期 7 個(gè)月)房揭,EE 即企業(yè)版,強(qiáng)調(diào)安全挖滤,付費(fèi)使用崩溪,支持周期 24 個(gè)月。
- moby斩松、docker-ce與docker-ee
最早的時(shí)候docker就是一個(gè)開源項(xiàng)目伶唯,主要由docker公司維護(hù)。
2017年年初惧盹,docker公司將原先的docker項(xiàng)目改名為moby乳幸,并創(chuàng)建了docker-ce和docker-ee。
這三者的關(guān)系是:
moby是繼承了原先的docker的項(xiàng)目钧椰,是社區(qū)維護(hù)的的開源項(xiàng)目粹断,誰(shuí)都可以在moby的基礎(chǔ)打造自己的容器產(chǎn)品
docker-ce是docker公司維護(hù)的開源項(xiàng)目,是一個(gè)基于moby項(xiàng)目的免費(fèi)的容器產(chǎn)品
docker-ee是docker公司維護(hù)的閉源產(chǎn)品嫡霞,是docker公司的商業(yè)產(chǎn)品瓶埋。
moby project由社區(qū)維護(hù),docker-ce project是docker公司維護(hù)诊沪,docker-ee是閉源的养筒。
要使用免費(fèi)的docker,從網(wǎng)頁(yè)docker-ce上獲取端姚。
要使用收費(fèi)的docker晕粪,從網(wǎng)頁(yè)docker-ee上獲取。
1渐裸、添加Docker源以及安裝啟動(dòng)
[root@Eden666 ~]# yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@Eden666 ~]# yum install docker-ce
[root@Eden666 ~]# systemctl enable docker //用于設(shè)置Docker在系統(tǒng)重啟時(shí)巫湘,自動(dòng)啟動(dòng)Docker服務(wù)
//systemctl disable docker:取消系統(tǒng)重啟時(shí)自動(dòng)啟動(dòng)docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@Eden666 ~]# systemctl start docker
//重新啟動(dòng):service docker restart
2、測(cè)試是否安裝正確
[root@Eden666 ~]# docker version
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:22 2018
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd64
Experimental: false
[root@Eden666 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@Eden666 ~]# docker run -it ubuntu bash //測(cè)試Ubuntu鏡像
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
32802c0cfa4d: Pull complete
da1315cffa03: Pull complete
fa83472a3562: Pull complete
f85999a86bef: Pull complete
Digest: sha256:6d0e0c26489e33f5a6f0020edface2727db9489744ecc9b4f50c7fa671f23c49
Status: Downloaded newer image for ubuntu:latest
root@12e3f456d4c2:/# exit //退出當(dāng)前鏡像容器
exit
- 其他操作命令
1昏鹃、 卸載Docker包:yum remove docker-ce
2尚氛、安裝完docker后,宿主機(jī)上有圖像洞渤,容器怠褐,卷或自定義配置文件等信息在文件/var/lib/docker下,若需要?jiǎng)h除您宪,則同時(shí)需要rm -rf /var/lib/docker
Docker中軟件環(huán)境安裝
1奈懒、jdk
- docker pull java 默認(rèn)拉取Docker Hub 官方倉(cāng)庫(kù)中l(wèi)atest鏡像奠涌。
[root@Eden666 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 93fd78260bd1 10 days ago 86.2MB
hello-world latest 4ab4c602aa5e 2 months ago 1.84kB
java latest d23bdf5b1b1b 22 months ago 643MB
運(yùn)行docker:docker run -it --name java java
重新使用別名后再次運(yùn)行:docker run -it java
其中,--name后面的“java”是為容器指定了一個(gè)別名磷杏,而最后的那個(gè)“java”指的是下載鏡像時(shí)的名稱溜畅。
命令以及參數(shù)的含義:
run:?jiǎn)?dòng)一個(gè)鏡像容器
-d:指定容器運(yùn)行于后臺(tái)
-it:-i 和 -t 的縮寫;
-i:以交互模式運(yùn)行容器极祸,通常與 -t 同時(shí)使用
-t:為容器重新分配一個(gè)偽輸入終端慈格,通常與 -i 同時(shí)使用
--name:指定容器名字,后續(xù)可以通過名字進(jìn)行容器管理進(jìn)入java容器并測(cè)試
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@Eden666 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 93fd78260bd1 10 days ago 86.2MB
hello-world latest 4ab4c602aa5e 2 months ago 1.84kB
java latest d23bdf5b1b1b 22 months ago 643MB
[root@Eden666 ~]# docker run -it java
root@c04af2d9dbfa:/# java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-2~bpo8+1-b14)
OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)
root@c04af2d9dbfa:/#
- 多窗口進(jìn)入java容器的方式遥金。
1浴捆、docker attach 容器id
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c3c1957ca37 java "/bin/bash" 8 seconds ago Up 8 seconds trusting_galois
[root@Eden666 ~]# docker attach 9c //只需兩位就行
2、docker exec -it 容器id /bin/bash
[root@Eden666 ~]# docker exec -it 82 /bin/bash
root@822df775abf9:/# exit
exit
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
822df775abf9 java "/bin/bash" 52 seconds ago Up 50 seconds practical_wright
[root@Eden666 ~]# docker stop 82
82
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
以上兩種進(jìn)入兩種進(jìn)入docker 容器的方式區(qū)別:attach方式會(huì)導(dǎo)致不同終端窗口阻塞稿械,若其中一個(gè)終端退出以及任何操作會(huì)影響其他終端窗口同樣變化选泻。exec方式可以操作不同終端窗口命令,互不影響美莫。
- 停止容器:docker stop java
2页眯、tomcat
[root@Eden666 ~]#docker pull tomcat
[root@Eden666 ~]# docker run -p 9300:8080 tomcat //加上 -d 參數(shù)控制臺(tái)不會(huì)輸出日志信息以及會(huì)后臺(tái)運(yùn)行
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a99d71dae3d tomcat "catalina.sh run" 6 minutes ago Up 6 minutes 0.0.0.0:9300->8080/tcp cocky_ritchie
[root@Eden666 ~]# netstat -na | grep 9300
tcp6 0 0 :::9300 :::* LISTEN
說(shuō)明:docker stop 容器id 并不是刪除了容器,而是標(biāo)志位Exited 狀態(tài)厢呵。docker run 每次啟動(dòng)都是全新的docker
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a99d71dae3d tomcat "catalina.sh run" 6 minutes ago Up 6 minutes 0.0.0.0:9300->8080/tcp cocky_ritchie
[root@Eden666 ~]# docker stop 5a
5a
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@Eden666 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a99d71dae3d tomcat "catalina.sh run" 12 minutes ago Exited (143) 56 seconds ago cocky_ritchie
822df775abf9 java "/bin/bash" 35 minutes ago Exited (0) 34 minutes ago practical_wright
9c3c1957ca37 java "/bin/bash" 40 minutes ago Exited (0) 36 minutes ago trusting_galois
c04af2d9dbfa java "/bin/bash" 42 minutes ago Exited (0) 40 minutes ago lucid_dubinsky
6a5e5a96f7ae java "/bin/bash" About an hour ago Exited (0) About an hour ago reverent_margulis
75a7f9618f3e java "/bin/bash" About an hour ago Exited (0) About an hour ago hardcore_beaver
b4bd7bc6afb1 java "/bin/bash" About an hour ago Exited (1) About an hour ago gifted_aryabhata
48d5abffea90 java "/bin/bash" About an hour ago Exited (0) About an hour ago affectionate_margulis
a34aad33ff1c java "/bin/bash" About an hour ago Exited (0) 24 minutes ago java
35ffb2bfbd4e hello-world "/hello" 2 hours ago Exited (0) 2 hours ago zealous_curran
bee48ff4fd0a hello-world "/hello" 25 hours ago Exited (0) 25 hours ago gracious_benz
12e3f456d4c2 ubuntu "bash" 25 hours ago Exited (0) 25 hours ago thirsty_hellman
[root@Eden666 ~]# docker start 5a
5a
[root@Eden666 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a99d71dae3d tomcat "catalina.sh run" 12 minutes ago Up 3 seconds 0.0.0.0:9300->8080/tcp cocky_ritchie
[root@Eden666 ~]# docker run -dp 9200:8080 tomcat
4e0e74a94cafa8fb03c50d4ca3fd6f3a51360d2cc9d477173c3d106bd3377ef2
[root@Eden666 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e0e74a94caf tomcat "catalina.sh run" 18 seconds ago Up 17 seconds 0.0.0.0:9200->8080/tcp upbeat_sammet
5a99d71dae3d tomcat "catalina.sh run" 16 minutes ago Up 3 minutes 0.0.0.0:9300->8080/tcp cocky_ritchie
822df775abf9 java "/bin/bash" 40 minutes ago Exited (0) 39 minutes ago practical_wright
9c3c1957ca37 java "/bin/bash" 44 minutes ago Exited (0) 40 minutes ago trusting_galois
c04af2d9dbfa java "/bin/bash" About an hour ago Exited (0) 44 minutes ago lucid_dubinsky
6a5e5a96f7ae java "/bin/bash" About an hour ago Exited (0) About an hour ago reverent_margulis
75a7f9618f3e java "/bin/bash" About an hour ago Exited (0) About an hour ago hardcore_beaver
b4bd7bc6afb1 java "/bin/bash" About an hour ago Exited (1) About an hour ago gifted_aryabhata
48d5abffea90 java "/bin/bash" About an hour ago Exited (0) About an hour ago affectionate_margulis
a34aad33ff1c java "/bin/bash" About an hour ago Exited (0) 28 minutes ago java
35ffb2bfbd4e hello-world "/hello" 2 hours ago Exited (0) 2 hours ago zealous_curran
bee48ff4fd0a hello-world "/hello" 25 hours ago Exited (0) 25 hours ago gracious_benz
12e3f456d4c2 ubuntu "bash" 25 hours ago Exited (0) 25 hours ago thirsty_hellman
如何刪除Docker中狀態(tài)為Exited的容器
[root@Eden666 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e0e74a94caf tomcat "catalina.sh run" 18 seconds ago Up 17 seconds 0.0.0.0:9200->8080/tcp upbeat_sammet
5a99d71dae3d tomcat "catalina.sh run" 16 minutes ago Up 3 minutes 0.0.0.0:9300->8080/tcp cocky_ritchie
822df775abf9 java "/bin/bash" 40 minutes ago Exited (0) 39 minutes ago practical_wright
9c3c1957ca37 java "/bin/bash" 44 minutes ago Exited (0) 40 minutes ago trusting_galois
c04af2d9dbfa java "/bin/bash" About an hour ago Exited (0) 44 minutes ago lucid_dubinsky
6a5e5a96f7ae java "/bin/bash" About an hour ago Exited (0) About an hour ago reverent_margulis
75a7f9618f3e java "/bin/bash" About an hour ago Exited (0) About an hour ago hardcore_beaver
b4bd7bc6afb1 java "/bin/bash" About an hour ago Exited (1) About an hour ago gifted_aryabhata
48d5abffea90 java "/bin/bash" About an hour ago Exited (0) About an hour ago affectionate_margulis
a34aad33ff1c java "/bin/bash" About an hour ago Exited (0) 28 minutes ago java
35ffb2bfbd4e hello-world "/hello" 2 hours ago Exited (0) 2 hours ago zealous_curran
bee48ff4fd0a hello-world "/hello" 25 hours ago Exited (0) 25 hours ago gracious_benz
12e3f456d4c2 ubuntu "bash" 25 hours ago Exited (0) 25 hours ago thirsty_hellman
[root@Eden666 ~]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs
-s, --size Display total file sizes
[root@Eden666 ~]# docker rm $(sudo docker ps -qf status=exited)
822df775abf9
9c3c1957ca37
c04af2d9dbfa
6a5e5a96f7ae
75a7f9618f3e
b4bd7bc6afb1
48d5abffea90
a34aad33ff1c
35ffb2bfbd4e
bee48ff4fd0a
12e3f456d4c2
[root@Eden666 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e0e74a94caf tomcat "catalina.sh run" 3 minutes ago Up 3 minutes 0.0.0.0:9200->8080/tcp upbeat_sammet
5a99d71dae3d tomcat "catalina.sh run" 19 minutes ago Up 7 minutes 0.0.0.0:9300->8080/tcp cocky_ritchie
- 配置國(guó)內(nèi)鏡像加速參考鏈接(http://www.docker-cn.com/registry-mirror)
3窝撵、mysql
[root@Eden666 docker]# docker run -dp 3308:3306 -e MYSQL_ROOT_PASSWORD=123456 --name=mysql mysql
44de7df2a0fd8307b5ae631e48ffc7e448df496d062340273840ab039b5d5e55
[root@Eden666 docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
44de7df2a0fd mysql "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 33060/tcp, 0.0.0.0:3308->3306/tcp mysql
[root@Eden666 docker]# docker exec -it 44 /bin/bash
root@44de7df2a0fd:/# mysql -u root -p123456
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)