1.Docke基礎(chǔ)
1.1 理念
? Docker的主要目標(biāo)是“Build,Ship and Run Any App,Anywhere”魁莉,也就是通過對應(yīng)用組件的封裝、分發(fā)募胃、部署旗唁、運(yùn)行等生命周期的管理,使用戶的APP(可以是一個WEB應(yīng)用或數(shù)據(jù)庫應(yīng)用等等)及其運(yùn)行環(huán)境能夠做到“一次封裝摔认,到處運(yùn)行”逆皮。 一句話宅粥,Docker解決了運(yùn)行環(huán)境和配置問題参袱,方便做持續(xù)集成并有助于整體發(fā)布的容器虛擬化技術(shù)。
1.2 底層原理
? Docker是一個Client-Server結(jié)構(gòu)的系統(tǒng)秽梅,Docker守護(hù)進(jìn)程運(yùn)行在主機(jī)上抹蚀, 然后通過Socket連接從客戶端訪問,守護(hù)進(jìn)程從客戶端接受命令并管理運(yùn)行在主機(jī)上的容器企垦。 容器环壤,是一個運(yùn)行時環(huán)境,就是我們前面說到的集裝箱钞诡。
- docker有著比虛擬機(jī)更少的抽象層郑现。由于docker不需要Hypervisor實(shí)現(xiàn)硬件資源虛擬化,運(yùn)行在docker容器上的程序直接使用的都是實(shí)際物理機(jī)的硬件資源。因此在CPU荧降、內(nèi)存利用率上docker將會在效率上有明顯優(yōu)勢接箫。
- docker利用的是宿主機(jī)的內(nèi)核,而不需要Guest OS。因此,當(dāng)新建一個容器時,docker不需要和虛擬機(jī)一樣重新加載一個操作系統(tǒng)內(nèi)核朵诫。仍而避免引尋辛友、加載操作系統(tǒng)內(nèi)核返個比較費(fèi)時費(fèi)資源的過程,當(dāng)新建一個虛擬機(jī)時,虛擬機(jī)軟件需要加載Guest OS,返個新建過程是分鐘級別的。而docker由于直接利用宿主機(jī)的操作系統(tǒng),則省略了返個過程,因此新建一個docker容器只需要幾秒鐘剪返。
2.Docker安裝(CentOS7環(huán)境)
2.1 卸載舊版本
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.2 安裝所需要的包
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
2.3 設(shè)置stable
使用國內(nèi)的阿里云废累,速度會快很多
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2.4 更新yum軟件包索引
sudo yum makecache fast
2.5 安裝docker-ce
sudo yum -y install docker-ce docker-ce-cli containerd.io
2.6 配置鏡像加速
登錄阿里云,配置自己的鏡像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://9gsicmpt.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
2.7 測試運(yùn)行hello-world
docker run hello-world
3.Docker常用命令
3.1 命令參考文檔
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with
"docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-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:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
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
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams 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 to files or directories 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
3.2 幫助命令
#查看信息
docker info
#查看版本
docke version
#幫助
docker --help
3.3 鏡像命令
- 查看鏡像信息
docker images
#查看鏡像信息
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 413be204e9c3 44 hours ago 456MB
tomcat latest a7fa4ac97be4 2 weeks ago 528MB
oraclelinux latest 0f4ffc192341 3 weeks ago 232MB
centos latest 470671670cac 2 months ago 237MB
hello-world latest fce289e99eb9 15 months ago 1.84kB
hello-world latest fce289e99eb9 15 months ago 1.84kB
redis 3.2 87856cc39862 17 months ago 76MB
REPOSITORY
: 表示鏡像的倉庫源
TAG
: 鏡像的標(biāo)簽
IMAGE ID
: 鏡像ID
CREATED
: 鏡像創(chuàng)建時間
SIZE
: 鏡像大小
同一倉庫源可以有多個TAG,代表這個倉庫源有多個版本脱盲,可以使用REPOSITORY:TAG來定義不同的鏡像邑滨。
OPTIONS:
-a 顯示所有鏡像
-q 只顯示鏡像ID
--digests 顯示鏡像的摘要信息
--no-trunc 顯示完整的信息
- 查詢應(yīng)用
docker search 鏡像名[:TAG]
DockerHub查詢
命令查詢:
OPTIONS:
-s 列出收藏?cái)?shù)不小于指定值的鏡像
-attomated 只列出automated build類型的鏡像
--no-trunc 顯示完整的鏡像描述
- 下載鏡像
docker pull 鏡像名[:TAG]
#下載tomcat
[root@localhost ~]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
f15005b0235f: Pull complete
41ebfd3d2fd0: Pull complete
b998346ba308: Pull complete
f01ec562c947: Pull complete
74c11ae3efe8: Pull complete
3a7e595a3ef5: Pull complete
208407758d73: Pull complete
b5238120a381: Pull complete
1716556aae46: Pull complete
1336a11b2bd2: Pull complete
Digest: sha256:c0ba286d9903006423ff4e2818845f1cc4046aa829bbfbc9b96f887423cd9f47
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
- 刪除鏡像
docker rmi 鏡像ID | 鏡像名:TAG
#刪除hello-world
[root@localhost ~]# docker rmi hello-world:latest
Untagged: hello-world:latest
# 刪除所有鏡像
docker rmi -f $(docker images -qa)
3.4 容器命令
3.4.1 普通命令
有鏡像才能創(chuàng)建容器,這個是前提
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS說明(常用):有些是一個減號钱反,有些是兩個減號
--name="容器新名字": 為容器指定一個名稱驼修;
-d: 后臺運(yùn)行容器殿遂,并返回容器ID,也即啟動守護(hù)式容器乙各;
-i:以交互模式運(yùn)行容器墨礁,通常與 -t 同時使用;
-t:為容器重新分配一個偽輸入終端耳峦,通常與 -i 同時使用恩静;
-P: 隨機(jī)端口映射;
-p: 指定端口映射蹲坷,有以下四種格式
ip:hostPort:containerPort
ip::containerPort
hostPort:containerPort
containerPort
- 新建并啟動容器
#使用鏡像centos:latest以交互模式啟動一個容器,在容器內(nèi)執(zhí)行/bin/bash命令
[root@localhost ~]# docker run -it centos /bin/bash
[root@0ac5fdf2ade5 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 01:00 pts/0 00:00:00 /bin/bash
root 15 1 0 01:00 pts/0 00:00:00 ps -ef
- 列出當(dāng)前所有正在運(yùn)行的容器
docker ps
OPTIONS說明(常用):
-a :列出當(dāng)前所有正在運(yùn)行的容器+歷史上運(yùn)行過的
-l :顯示最近創(chuàng)建的容器驶乾。
-n:顯示最近n個創(chuàng)建的容器。
-q :靜默模式循签,只顯示容器編號级乐。
--no-trunc :不截?cái)噍敵?
- 退出容器
-
exit
: 容器停止退出 -
ctrl+P+Q
: 容器不停止退出
- 啟動容器
docker start 容器ID|容器名
- 重啟容器
docker restart 容器ID|容器名
- 停止容器
docker stop 容器ID|容器名
- 強(qiáng)制停止容器
docker kill 容器ID|容器名
- 刪除已停止的容器
#刪除一個容器
docker rm 容器ID
#一次性刪除多個容器
docker rm -f $(docker ps -qa)
docker ps -qa | xargs docker rm
3.4.2 重點(diǎn)命令
- 啟動守護(hù)式容器
docker run -d 鏡像名[:TAG]
問題:然后docker ps -a 進(jìn)行查看, 會發(fā)現(xiàn)容器已經(jīng)退出
很重要的要說明的一點(diǎn): Docker容器后臺運(yùn)行,就必須有一個前臺進(jìn)程.
容器運(yùn)行的命令如果不是那些一直掛起的命令(比如運(yùn)行top,tail)县匠,就是會自動退出的风科。
- 查看容器日志
docker logs -tf --tail 10 容器ID
OPTIONS
-t 加入時間戳
-f 跟隨最新的日志打印
--tail 數(shù)字 顯示最后幾條
- 查看容器內(nèi)運(yùn)行的進(jìn)程
docker top 容器ID
- 進(jìn)入正在運(yùn)行的容器并以命令行交互
#重新進(jìn)入容器,acctach 直接進(jìn)入容器啟動命令的終端,不會啟動新的進(jìn)程
docker attach 容器ID
#重新進(jìn)入容器乞旦,exec 在容器中打開新的終端贼穆,并開啟新的進(jìn)程
docker exec -it 容器ID /bin/bash
- 從容器拷貝文件到主機(jī)上
docker cp 容器ID:容器文件路徑 主機(jī)目錄
- 命令小結(jié)
attach Attach to a running container # 當(dāng)前 shell 下 attach 連接指定運(yùn)行鏡像
build Build an image from a Dockerfile # 通過 Dockerfile 定制鏡像
commit Create a new image from a container changes # 提交當(dāng)前容器為新的鏡像
cp Copy files/folders from the containers filesystem to the host path #從容器中拷貝指定文件或者目錄到宿主機(jī)中
create Create a new container # 創(chuàng)建一個新的容器,同 run兰粉,但不啟動容器
diff Inspect changes on a container's filesystem # 查看 docker 容器變化
events Get real time events from the server # 從 docker 服務(wù)獲取容器實(shí)時事件
exec Run a command in an existing container # 在已存在的容器上運(yùn)行命令
export Stream the contents of a container as a tar archive # 導(dǎo)出容器的內(nèi)容流作為一個 tar 歸檔文件[對應(yīng) import ]
history Show the history of an image # 展示一個鏡像形成歷史
images List images # 列出系統(tǒng)當(dāng)前鏡像
import Create a new filesystem image from the contents of a tarball # 從tar包中的內(nèi)容創(chuàng)建一個新的文件系統(tǒng)映像[對應(yīng)export]
info Display system-wide information # 顯示系統(tǒng)相關(guān)信息
inspect Return low-level information on a container # 查看容器詳細(xì)信息
kill Kill a running container # kill 指定 docker 容器
load Load an image from a tar archive # 從一個 tar 包中加載一個鏡像[對應(yīng) save]
login Register or Login to the docker registry server # 注冊或者登陸一個 docker 源服務(wù)器
logout Log out from a Docker registry server # 從當(dāng)前 Docker registry 退出
logs Fetch the logs of a container # 輸出當(dāng)前容器日志信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口對應(yīng)的容器內(nèi)部源端口
pause Pause all processes within a container # 暫停容器
ps List containers # 列出容器列表
pull Pull an image or a repository from the docker registry server # 從docker鏡像源服務(wù)器拉取指定鏡像或者庫鏡像
push Push an image or a repository to the docker registry server # 推送指定鏡像或者庫鏡像至docker源服務(wù)器
restart Restart a running container # 重啟運(yùn)行的容器
rm Remove one or more containers # 移除一個或者多個容器
rmi Remove one or more images # 移除一個或多個鏡像[無容器使用該鏡像才可刪除故痊,否則需刪除相關(guān)容器才可繼續(xù)或 -f 強(qiáng)制刪除]
run Run a command in a new container # 創(chuàng)建一個新的容器并運(yùn)行一個命令
save Save an image to a tar archive # 保存一個鏡像為一個 tar 包[對應(yīng) load]
search Search for an image on the Docker Hub # 在 docker hub 中搜索鏡像
start Start a stopped containers # 啟動容器
stop Stop a running containers # 停止容器
tag Tag an image into a repository # 給源中鏡像打標(biāo)簽
top Lookup the running processes of a container # 查看容器中運(yùn)行的進(jìn)程信息
unpause Unpause a paused container # 取消暫停容器
version Show the docker version information # 查看 docker 版本號
wait Block until a container stops, then print its exit code # 截取容器停止時的退出狀態(tài)值
3.5 命令總結(jié)
4.Docker鏡像
4.1 聯(lián)合文件系統(tǒng)(UnionFS)
Union文件系統(tǒng)(UnionFS)是一種分層、輕量級并且高性能的文件系統(tǒng)玖姑,它支持對文件系統(tǒng)的修改作為一次提交來一層層的疊加愕秫,同時可以將不同目錄掛載到同一個虛擬文件系統(tǒng)下(unite several directories into a single virtual filesystem)。Union 文件系統(tǒng)是 Docker 鏡像的基礎(chǔ)焰络。鏡像可以通過分層來進(jìn)行繼承戴甩,基于基礎(chǔ)鏡像(沒有父鏡像),可以制作各種具體的應(yīng)用鏡像舔琅。
特性:一次同時加載多個文件系統(tǒng)等恐,但從外面看起來,只能看到一個文件系統(tǒng)备蚓,聯(lián)合加載會把各層文件系統(tǒng)疊加起來课蔬,這樣最終的文件系統(tǒng)會包含所有底層的文件和目錄
4.2 Docker鏡像加載原理
? docker的鏡像實(shí)際上由一層一層的文件系統(tǒng)組成,這種層級的文件系統(tǒng)UnionFS郊尝。
? bootfs(boot file system)主要包含bootloader和kernel, bootloader主要是引導(dǎo)加載kernel, Linux剛啟動時會加載bootfs文件系統(tǒng)二跋,在Docker鏡像的最底層是bootfs。這一層與我們典型的Linux/Unix系統(tǒng)是一樣的流昏,包含boot加載器和內(nèi)核扎即。當(dāng)boot加載完成之后整個內(nèi)核就都在內(nèi)存中了吞获,此時內(nèi)存的使用權(quán)已由bootfs轉(zhuǎn)交給內(nèi)核,此時系統(tǒng)也會卸載bootfs谚鄙。
? rootfs (root file system) 各拷,在bootfs之上。包含的就是典型 Linux 系統(tǒng)中的 /dev, /proc, /bin, /etc 等標(biāo)準(zhǔn)目錄和文件闷营。rootfs就是各種不同的操作系統(tǒng)發(fā)行版烤黍,比如Ubuntu,Centos等等傻盟。
對于一個精簡的OS速蕊,rootfs可以很小,只需要包括最基本的命令娘赴、工具和程序庫就可以了规哲,因?yàn)榈讓又苯佑肏ost的kernel,自己只需要提供 rootfs 就行了诽表。由此可見對于不同的linux發(fā)行版, bootfs基本是一致的, rootfs會有差別, 因此不同的發(fā)行版可以公用bootfs唉锌。
4.3 分層鏡像
鏡像就像洋蔥,一層一層的
特點(diǎn):共享資源关顷。比如糊秆,有多個鏡像都從相同的 base 鏡像構(gòu)建而來武福,那么宿主機(jī)只需在磁盤上保存一份base鏡像议双,同時內(nèi)存中也只需加載一份 base 鏡像,就可以為所有容器服務(wù)了捉片。而且鏡像的每一層都可以被共享平痰。
? Docker鏡像都是只讀的,當(dāng)容器啟動時伍纫,一個新的可寫層被加載到鏡像的頂部宗雇。這一層稱為“容器層”,“容器層”之下為“鏡像層‘莹规。
4.4 鏡像提交
docker commit -a="作者" -m="提交的描述信息" 容器ID 要創(chuàng)建的目標(biāo)鏡像名[:TAG]
[root@localhost ~]# docker commit -a "qianfg" -m="rename webapps.dist webapps" 6fc3493b6136 mytomcat:1.1
sha256:62f8298f1cc1c761e07b425d49b76482e40475ea887576009cf4f3185bf9c4f3
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytomcat 1.1 62f8298f1cc1 23 seconds ago 533MB
tomcat latest e36064f7c6f0 26 hours ago 528MB
centos latest 470671670cac 2 months ago 237MB
5.Docker容器數(shù)據(jù)卷
5.1 數(shù)據(jù)卷
? 數(shù)據(jù)卷就是目錄或文件赔蒲,存在于一個或多個容器中,由docker掛載到容器良漱,但不屬于聯(lián)合文件系統(tǒng)舞虱,因此能夠繞過Union File System提供一些用于持續(xù)存儲或共享數(shù)據(jù)的特性:
**數(shù)據(jù)卷的設(shè)計(jì)目的就是數(shù)據(jù)的持久化暴浦,完全獨(dú)立于容器的生存周期梳星,因此Docker不會在容器刪除時刪除其掛載的數(shù)據(jù)卷**夜焦。
特點(diǎn):
1:數(shù)據(jù)卷可在容器之間共享或重用數(shù)據(jù)
2:卷中的更改可以直接生效
3:數(shù)據(jù)卷中的更改不會包含在鏡像的更新中
4:數(shù)據(jù)卷的生命周期一直持續(xù)到?jīng)]有容器使用它為止
5.2 掛載
#掛載
docker run -it -v /宿主機(jī)絕對路徑目錄:/容器內(nèi)目錄 鏡像名
#查看數(shù)據(jù)卷是否掛載成功转培,掛載路徑卵史,讀寫權(quán)限
docker inspect 容器ID|grep "Mounts" -A 10
[root@localhost hostVolume]# docker inspect 0d1b5271fffa|grep "Mounts" -A 10
"Mounts": [
{
"Type": "bind",
"Source": "/hostVolume",
"Destination": "/containerVolume",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
#只讀掛載
docker run -it -v /宿主機(jī)絕對路徑目錄:/容器內(nèi)目錄:ro 鏡像名
顯然刊苍,容器內(nèi)文件只用查看權(quán)限梯影,沒有寫入權(quán)限兢哭!
做個測試,把主機(jī)的/test
文件夾愛掛載到tomcat容器的webapps
目錄下返帕,這樣可以在test目錄下實(shí)時更新資源桐玻,瀏覽器訪問可以實(shí)時更新!這樣的的話荆萤,以后的項(xiàng)目在主機(jī)目錄下進(jìn)行修改畸冲,容器也會實(shí)時更新。
5.3 數(shù)據(jù)卷容器
命名的容器掛載數(shù)據(jù)卷观腊,其它容器通過掛載這個(父容器)實(shí)現(xiàn)數(shù)據(jù)共享邑闲,掛載數(shù)據(jù)卷的容器,稱之為數(shù)據(jù)卷容器
6. DockerFile
6.1 Dockerfile構(gòu)建過程解析
? Dockerfile是用來構(gòu)建Docker鏡像的構(gòu)建文件梧油,是由一系列命令和參數(shù)構(gòu)成的腳本苫耸,類似于shell腳本。
? 構(gòu)建三步驟:
- 編寫Dockerfile文件
- docker build
- docker run
#CentOS7的Dockerfile
FROM scratch
ADD centos-7-x86_64-docker.tar.xz /
LABEL org.label-schema.schema-version="1.0" \
org.label-schema.name="CentOS Base Image" \
org.label-schema.vendor="CentOS" \
org.label-schema.license="GPLv2" \
org.label-schema.build-date="20191001"
CMD ["/bin/bash"]
Dockerfile基礎(chǔ)知識:
1.每條保留字指令都必須為大寫字母而且后面至少跟隨一個參數(shù)
2.指令從上到小儡陨,順序執(zhí)行
3.#表示注釋
4.每條指令都會創(chuàng)建一個新的鏡像層褪子,并對鏡像進(jìn)行提交
Dokerfile執(zhí)行流程:
1.docker從基礎(chǔ)鏡像運(yùn)行一個容器
2.執(zhí)行一條指令并對容器做出修改
3.執(zhí)行類似docker commit 的操作提交一個新的鏡像層
4.docker在基于剛提交的鏡像運(yùn)行一個新容器
5.執(zhí)行dockerfile中的下一條指令直到所有指令都執(zhí)行完成
6.2 Dockerfile&鏡像&容器
從應(yīng)用軟件的角度來看,Dockerfile骗村、Docker鏡像與Docker容器分別代表軟件的三個不同階段嫌褪,
* Dockerfile是軟件的原材料
* Docker鏡像是軟件的交付品
* Docker容器則可以認(rèn)為是軟件的運(yùn)行態(tài)。
Dockerfile面向開發(fā)胚股,Docker鏡像成為交付標(biāo)準(zhǔn)笼痛,Docker容器則涉及部署與運(yùn)維,三者缺一不可琅拌,合力充當(dāng)Docker體系的基石缨伊。
Dockerfile,需要定義一個Dockerfile进宝,Dockerfile定義了進(jìn)程需要的一切東西刻坊。Dockerfile涉及的內(nèi)容包括執(zhí)行代碼或者是文件、環(huán)境變量党晋、依賴包谭胚、運(yùn)行時環(huán)境、動態(tài)鏈接庫未玻、操作系統(tǒng)的發(fā)行版灾而、服務(wù)進(jìn)程和內(nèi)核進(jìn)程(當(dāng)應(yīng)用進(jìn)程需要和系統(tǒng)服務(wù)和內(nèi)核進(jìn)程打交道,這時需要考慮如何設(shè)計(jì)namespace的權(quán)限控制)等等;
Docker鏡像深胳,在用Dockerfile定義一個文件之后绰疤,docker build時會產(chǎn)生一個Docker鏡像,當(dāng)運(yùn)行 Docker鏡像時舞终,會真正開始提供服務(wù);
Docker容器轻庆,容器是直接提供服務(wù)的癣猾。
6.3 Dockerfile保留字指令
FROM 基礎(chǔ)鏡像,當(dāng)前新鏡像是基于那個鏡像的
MAINTAINER 鏡像維護(hù)者的姓名和郵箱
RUN 容器構(gòu)建時需要執(zhí)行的指令
EXPOSE 當(dāng)前容器對外暴露的端口
WORKDIR 指定在創(chuàng)建容器后余爆,終端默認(rèn)登錄的工作目錄
ENV 用來在構(gòu)建鏡像過程中設(shè)置環(huán)境變量
ADD 將宿主機(jī)目錄下的文件拷貝進(jìn)鏡像而且ADD命令會自動處理URL和解壓tar壓縮包
COPY 類型ADD,拷貝文件到鏡像目錄中纷宇。
將從構(gòu)建上下文目錄中<源路徑>的文件復(fù)制到新的一層鏡像內(nèi)的<目標(biāo)路徑>位置
VOLUME 容器數(shù)據(jù)卷,用于數(shù)據(jù)保存和持久化工作
CMD 指定一個容器啟動時要運(yùn)行的命令
Dockerfile中可以有多個CMD指令蛾方,但只用最后一個生效像捶,并會被docker run之后的參數(shù)替換
ENTRYPOINT 制定一個容器啟動時要運(yùn)行的命令
ENTRYPOINT同CMD一樣,都是在指定容器啟動程序及參數(shù)桩砰,但是不會被覆蓋
ONBUILD 當(dāng)構(gòu)建一個被繼承的Dockerfile是運(yùn)行命令拓春,父鏡像在被子繼承后父鏡像的onbuild被觸發(fā)
6.4 自定義鏡像
自定義tomcat鏡像
新建文件夾tomcat8
,然后把需要的安裝包放到該目錄下
[root@localhost tomcat8]# pwd
/qianfg/dockerfile/tomcat8
[root@localhost tomcat8]# ls
apache-tomcat-8.5.50.tar.gz Dockerfile hello.txt jdk-8u221-linux-x64.tar.gz
[root@localhost tomcat8]# cat hello.txt
hello world
第一步亚隅,在該目錄下編寫Dockerfile
#基礎(chǔ)鏡像
FROM centos
#作者姓名郵箱
MAINTAINER qianfg<qianfg@163.com>
#把宿主機(jī)當(dāng)前上下文的hello.txt拷貝到容器的/usr/local/路徑下
COPY hello.txt /usr/local/hello_container.txt
#把java和tomcat添加到容器中
ADD jdk-8u221-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.5.50.tar.gz /usr/local/
#安裝vim編輯器
RUN yum -y install vim
#設(shè)置登錄時訪問目錄
ENV MYPATH /usr/local
WORKDIR $MYPATH
#配置java和tomcat環(huán)境變量
ENV JAVA_HOME /usr/local/jdk1.8.0_221
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.50
ENV CATALINA_VASE /usr/local/apache-tomcat-8.5.50
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
#容器運(yùn)行時監(jiān)聽的端口
EXPOSE 8080
#啟動時運(yùn)行tomcat
ENTRYPOINT ["/usr/local/apache-tomcat-8.5.50/bin/startup.sh"]
CMD ["/usr/local/apache-tomcat-8.5.50/bin/catalina.sh","run"]
第二部硼莽,構(gòu)建鏡像
#編寫的文件在該目錄下,可以省略-f選項(xiàng)
docker build -t qianfgtomcat8 .
#不省略-f選項(xiàng)
docker build -f /qianfg/dockerfile/tomcat8/Dockerfile -t qianfgtomcat8 .
第三步煮纵,運(yùn)行鏡像
docker run -d -p 9080:8080 --name myt8
-v /qianfg/dockerfile/tomcat8/test:/usr/local/apache-tomcat-8.5.50/webapps/test
-v /qianfg/dockerfile/tomcat8/tomcat8logs/:/usr/local/apache-tomcat-8.5.50/logs
--privileged=true qianfgtomcat8
7.Docker常用安裝
7.1 Docker安裝mysql
#查詢mysql5.7
docker serach mysql:5.7
#拉取mysql5.7
docker pull 5.7
#運(yùn)行mysql5.7容器
docker run -p 1234:3306 --name mysql -v /mysql/conf:/etc/my.cnf -v /mysql/logs:/logs -v /mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
7.2 Docker安裝redis
#查找收藏?cái)?shù)大于5000的redis
docker search -s 5000 redis
#拉取redis
docker pull redis