日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Docker-入门

發(fā)布時(shí)間:2023/12/20 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Docker-入门 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Docker是基于Linux容器的虛擬化技術(shù),基于go語(yǔ)言開(kāi)發(fā),生態(tài)是非常不錯(cuò)的,通過(guò)對(duì)應(yīng)用組件的封裝、
分發(fā)、部署、運(yùn)行等生命周期的管理,使用戶的APP(可以是一個(gè)WEB應(yīng)用或數(shù)據(jù)庫(kù)應(yīng)用等等)及其運(yùn)
行環(huán)境能夠做到一次封裝,到處運(yùn)行。
傳統(tǒng)虛擬技術(shù)需要虛擬出一個(gè)系統(tǒng)才能運(yùn)行,Linux容器只是對(duì)進(jìn)程進(jìn)行了隔離,使得每個(gè)進(jìn)程看起來(lái)都運(yùn)行在一個(gè)單獨(dú)的系統(tǒng)。不同的容器可以共用相同的庫(kù)和內(nèi)核,而且做到互不影響,所以容器本身沒(méi)有內(nèi)核,也更輕量級(jí)。
Docker的兩個(gè)核心概念為:容器和鏡像。在后續(xù)的使用中會(huì)逐步了解

一、Docker安裝

官方文檔:https://docs.docker.com/engine/install/

還是基于CentOS操作系統(tǒng)進(jìn)行安裝

1.安裝

安裝前先移除,防止有緩存:

yumremove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine

安裝yum-utils并配置鏡像:

yum install-y yum-utilsyum-config-manager \--add-repo \https://download.docker.com/linux/centos/docker-ce.repo# 速度過(guò)慢,可以使用阿里鏡像 yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安裝docker:

yum install -y docker-ce docker-ce-cli containerd.io

等待下載完后,啟動(dòng)docker:

systemctl start docker

2.阿里云鏡像加速

由于docker中容器依賴鏡像,鏡像存放在倉(cāng)庫(kù)中,使用時(shí)需要拉取到本地,可以是私有,也可以是公開(kāi),最大的開(kāi)放型鏡像倉(cāng)庫(kù)為:https://hub.docker.com/
因?yàn)槭菄?guó)外的倉(cāng)庫(kù),拉取速度會(huì)受影響,可以通過(guò)阿里云鏡像加速

2.1 進(jìn)入阿里云

進(jìn)入網(wǎng)站:https://cr.console.aliyun.com/后,進(jìn)入控制臺(tái):

2.2 容器鏡像服務(wù)

鼠標(biāo)移動(dòng)到左上角,進(jìn)入容器鏡像服務(wù):

2.3 鏡像加速器

選擇鏡像工具-鏡像加速器:

按照教程執(zhí)行命令:

3.helloworld

啟動(dòng)docker后,官方提供了一個(gè)測(cè)試鏡像,執(zhí)行命令:

docker run hello-world

輸出了一些信息:

4.鏡像倉(cāng)庫(kù)

上面提到過(guò),目前最大的鏡像公開(kāi)倉(cāng)庫(kù):https://hub.docker.com/

二、docker命令

通過(guò)docker命令,可以初步了解什么是鏡像,什么是容器

1.幫助命令

命令描述
docker version查看docker的版本信息
docker info查看docker詳細(xì)的信息
docker --helpdocker的幫助命令,可以查看到相關(guān)的其他命令

2.鏡像命令

命令描述
docker images列出本地主機(jī)上的鏡像
docker search 鏡像名從 docker hub 上搜索鏡像
docker pull 鏡像名從docker hub 上拉取鏡像
docker rmi 鏡像名稱刪除本地鏡像
2.1 docker images

docker images是常用命令,可選參數(shù)為:

參數(shù)描述
-a列出本地所有的鏡像
-q只顯示鏡像ID
--digests顯示鏡像的摘要信息
--no-trunc顯示完整的鏡像信息,默認(rèn)信息過(guò)多會(huì)進(jìn)行截取
2.2 docker search

docker search是從docker hub上搜索鏡像,下面是我在網(wǎng)頁(yè)上搜索tomcat的結(jié)果:

命令的可選參數(shù)為:

參數(shù)描述
-f過(guò)濾條件 docker search -f STARS=5 tomcat :表示搜索stats>=5的tomcat鏡像
--limit分頁(yè)顯示
--no-trunc顯示完整信息
2.3 docker pull

docker pull從docker hub上拉取鏡像到本地:

2.4 docker rmi

docker rmi刪除一個(gè)或多個(gè)鏡像

參數(shù)描述
-f強(qiáng)制刪除

3.容器命令

命令描述
docker run 鏡像名創(chuàng)建并啟動(dòng)一個(gè)容器
docker ps列舉容器,默認(rèn)只展示運(yùn)行中的
docker start 容器名/容器id啟動(dòng)容器
docker restart 容器名/容器id重啟容器
docker stop 容器名/容器id停止容器
docker kill 容器名/容器id強(qiáng)制停止容器
docker rm 容器id刪除容器
3.1 docker run

docker run每次執(zhí)行,會(huì)產(chǎn)生一個(gè)新的容器并運(yùn)行該容器,支持的參數(shù):

參數(shù)描述
--name指定容器名稱
-d守護(hù)式容器,后臺(tái)運(yùn)行
-it-i為交互式模式運(yùn)行,-t為重新分配一個(gè)偽終端,兩者通常一起使用
-P隨機(jī)端口映射
-p指定端口映射關(guān)系
-v指定數(shù)據(jù)卷映射關(guān)系
--volumes-from指定數(shù)據(jù)卷容器

交互式啟動(dòng):

-bash-4.2# docker run -p 1234:8080 -it tomcat /bin/bash root@643f11414f56:/usr/local/tomcat#

交互式退出方式:

  • exit:退出并停止容器
  • ctrl+q+p:退出交互式,容器后臺(tái)運(yùn)行
3.2 docker ps

docker ps用于列舉容器,默認(rèn)只展示運(yùn)行中的,支持的參數(shù)為:

參數(shù)描述
-a展示所有的容器
-l展示最近創(chuàng)建的容器
-n展示最近n個(gè)創(chuàng)建的
-q只展示容器編號(hào)
--no-trunc展示完整信息
3.3 docker rm

docker rm支持一次性刪除多個(gè)容器,后面跟上多個(gè)容器id:

docker rm-f 22 64 docker rm -f $(docker ps -qa) docker ps -a -q | xargs docker rm

4.其他命令

命令描述
docker logs 容器id查看容器執(zhí)行日志
docker inspect 容器id查看容器詳細(xì)信息
docker exec 容器id 命令進(jìn)入容器,打開(kāi)新的終端執(zhí)行命令
docker attach 容器id進(jìn)入容器啟動(dòng)命令的終端
docker cp 容器id:容器內(nèi)路徑 目的地路徑復(fù)制容器的文件到宿主機(jī)中
4.1 docker logs

查看終端執(zhí)行日志:

-bash-4.2# docker logs -t -f --tail 3 d4 2022-08-30T04:20:13.578533223Z 30-Aug-2022 04:20:13.577 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/10.0.14] 2022-08-30T04:20:13.604351571Z 30-Aug-2022 04:20:13.601 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 2022-08-30T04:20:13.646282364Z 30-Aug-2022 04:20:13.643 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [219] milliseconds 2022-08-30T04:23:57.855531488Z 30-Aug-2022 04:23:57.843 INFO [Thread-2] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"] 2022-08-30T04:23:57.855560212Z 30-Aug-2022 04:23:57.852 INFO [Thread-2] org.apache.catalina.core.StandardService.stopInternal Stopping service [Catalina] 2022-08-30T04:23:57.860806108Z 30-Aug-2022 04:23:57.857 INFO [Thread-2] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"] 2022-08-30T04:23:57.877280602Z 30-Aug-2022 04:23:57.867 INFO [Thread-2] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]
4.2 docker inspect
4.3 docker exec

docker exec用于容器內(nèi)執(zhí)行命令,支持-it進(jìn)入交互式模式,并且是新創(chuàng)建一個(gè)終端,所以退出并不會(huì)導(dǎo)致容器停止

-bash-4.2# docker exec -it d7 /bin/bash root@d7b4af8ba1ff:/usr/local/tomcat# exit exit
4.4 docker attach

docker attach是進(jìn)入啟動(dòng)容器的終端,退出會(huì)導(dǎo)致容器停止

-bash-4.2# docker attach d7 ^C30-Aug-2022 04:38:01.370 INFO [Thread-2] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"] 30-Aug-2022 04:38:01.387 INFO [Thread-2] org.apache.catalina.core.StandardService.stopInternal Stopping service [Catalina] 30-Aug-2022 04:38:01.394 INFO [Thread-2] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"] 30-Aug-2022 04:38:01.403 INFO [Thread-2] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]
4.5 docker cp

docker cp用于將文件復(fù)制到宿主機(jī)內(nèi):

-bash-4.2# docker exec -it d7 /bin/bash root@d7b4af8ba1ff:/usr/local/tomcat# cd /root root@d7b4af8ba1ff:~# touch 123.txt root@d7b4af8ba1ff:~# exit exit-bash-4.2# docker cp d7:/root/123.txt /root -bash-4.2# ls 123.txt

三、鏡像

鏡像包含著軟件運(yùn)行環(huán)境和基于運(yùn)行環(huán)境開(kāi)發(fā)的軟件,是包含了運(yùn)行某個(gè)軟件的所有內(nèi)容,鏡像使用UnionFS文件系統(tǒng),編程語(yǔ)言來(lái)理解就是類繼承,該文件是可以被繼承的,即鏡像內(nèi)部擁有可以分層很多個(gè)文件,只是對(duì)外表現(xiàn)成一個(gè)文件。基于基礎(chǔ)鏡像,可以制作各種各樣的應(yīng)用鏡像
鏡像是只讀的,當(dāng)容器啟動(dòng)時(shí),一個(gè)新的可寫(xiě)層被加載到鏡像的頂部,這一層通常被稱為容器層,容器層之下的都叫鏡像層。

制作鏡像有兩種方式:

  • 基于容器制作
  • 基于其他鏡像,構(gòu)建DockerFile制作

1.基于容器制作:docker commit

基于容器制作比較簡(jiǎn)單,當(dāng)我們啟動(dòng)一個(gè)容器后,會(huì)對(duì)容器內(nèi)部進(jìn)行一些處理,如寫(xiě)入一些文件等等,如果想要?jiǎng)?chuàng)建另一個(gè)容器,同時(shí)也能擁有這些文件,那么就可以基于容器制作鏡像,命令為:

docker commit -m="要提交的描述信息" -a="作者" 容器ID 要?jiǎng)?chuàng)建的目標(biāo)鏡像名:[標(biāo)簽名]

目前有一個(gè)tomcat的本地鏡像:

1.1 創(chuàng)建容器并添加文件

創(chuàng)建啟動(dòng)容器,添加文件:

-bash-4.2# docker run -it -p 1234:8080 tomcat /bin/bash root@9d6740173a6c:/usr/local/tomcat# cd webapps root@9d6740173a6c:/usr/local/tomcat/webapps# mkdir ROOT root@9d6740173a6c:/usr/local/tomcat/webapps# cd ROOT root@9d6740173a6c:/usr/local/tomcat/webapps/ROOT# echo hello >> index.html

啟動(dòng)tomcat:

-bash-4.2# docker exec -it 9d /bin/bash root@9d6740173a6c:/usr/local/tomcat# cd bin/ root@9d6740173a6c:/usr/local/tomcat/bin# ls bootstrap.jar commons-daemon-native.tar.gz digest.sh shutdown.sh version.sh catalina-tasks.xml commons-daemon.jar makebase.sh startup.sh catalina.sh configtest.sh migrate.sh tomcat-juli.jar ciphers.sh daemon.sh setclasspath.sh tool-wrapper.sh root@9d6740173a6c:/usr/local/tomcat/bin# ./startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/openjdk-11 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Using CATALINA_OPTS: Tomcat started.

訪問(wèn)效果:

1.2 制作鏡像
-bash-4.2# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9d6740173a6c tomcat "/bin/bash" 9 minutes ago Up 9 minutes 0.0.0.0:1234->8080/tcp, :::1234->8080/tcp inspiring_tereshkova-bash-4.2# docker commit -m="add index.html" -a="aruba" 9d tomcat-hello:1.0 sha256:ba5599c90061b73afe32fa7b44b668a4797f1425d69fcd090ea822522723e5b5

查看鏡像:

-bash-4.2# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat-hello 1.0 ba5599c90061 57 seconds ago 680MB tomcat latest fb5657adc892 8 months ago 680MB
1.3 使用鏡像

基于剛剛制作的鏡像,創(chuàng)建啟動(dòng)容器:

-bash-4.2# docker run -it -p 1244:8080 tomcat-hello:1.0 /bin/bash root@e7cc44f00318:/usr/local/tomcat# ./bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/openjdk-11 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Using CATALINA_OPTS: Tomcat started.

訪問(wèn)效果:

2.基于DockerFile制作鏡像

DockerFile就是一個(gè)包含命令和參數(shù)的腳本,基于基礎(chǔ)鏡像,構(gòu)建一個(gè)新的鏡像

命令描述
FROM基礎(chǔ)鏡像
MAINTAINER維護(hù)者、作者
RUN容器構(gòu)建時(shí)需要運(yùn)行的命令
EXPOSE當(dāng)前容器對(duì)外暴露的端口
WORKDIR終端默認(rèn)登錄進(jìn)來(lái)的工作目錄
ENV設(shè)置環(huán)境變量
ADD將宿主機(jī)目錄下的文件拷貝進(jìn)鏡像,會(huì)自動(dòng)處理URL和解壓tar壓縮包
COPY拷貝文件和目錄到鏡像中
VOLUME容器數(shù)據(jù)卷
CMD指定一個(gè)容器啟動(dòng)時(shí)要運(yùn)行的命令,只有一條會(huì)生效,并會(huì)被docker run后跟的命令代替
ENTRYPOINT指定一個(gè)容器啟動(dòng)時(shí)要運(yùn)行的命令
ONBUILD當(dāng)構(gòu)建一個(gè)被繼承的Dockerfile時(shí)運(yùn)行命令,父鏡像在被子繼承后父鏡像的onbuild被觸發(fā)

每執(zhí)行一條命令,都會(huì)生成一層新的鏡像層

hub上拉取下來(lái)的centos鏡像不帶vim編輯器,我們基于該鏡像制作一個(gè)新的,并帶上vim

2.1 準(zhǔn)備工作(yum源問(wèn)題)

2.1.1 拉取centos

docker pull centos

2.1.2 yum源問(wèn)題

我這邊拉取到的是centos8,centos7可能是沒(méi)有問(wèn)題的,創(chuàng)建和啟動(dòng)一個(gè)centos8容器后,使用yum下載一直報(bào)下載不到repo源:

[root@19bad3f699e6 yum]# yum install wget Failed to set locale, defaulting to C.UTF-8 CentOS Linux 8 - AppStream 48 B/s | 38 B 00:00 Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist

所以DockerFile構(gòu)建時(shí),使用yum也會(huì)報(bào)錯(cuò)

嘗試手動(dòng)解除repo中的baseUrl注釋,默認(rèn)baseUrl是被注釋的

cat /etc/yum.repos.d/CentOS-Linux-AppStream.repo # CentOS-Linux-AppStream.repo # # The mirrorlist system uses the connecting IP address of the client and the # update status of each mirror to pick current mirrors that are geographically # close to the client. You should use this for CentOS updates unless you are # manually picking other mirrors. # # If the mirrorlist does not work for you, you can try the commented out # baseurl line instead.[appstream] name=CentOS Linux $releasever - AppStream mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra #baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

通過(guò)vi將baseUrl的注釋去除,并且注釋掉mirrorlist:

vi /etc/yum.repos.d/CentOS-Linux-AppStream.repo # CentOS-Linux-AppStream.repo # # The mirrorlist system uses the connecting IP address of the client and the # update status of each mirror to pick current mirrors that are geographically # close to the client. You should use this for CentOS updates unless you are # manually picking other mirrors. # # If the mirrorlist does not work for you, you can try the commented out # baseurl line instead.[appstream] name=CentOS Linux $releasever - AppStream mirrorlist=http://mirrorlist.centos.org/?#release=$releasever&arch=$basearch&repo=AppStream&infra=$infra baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

再次報(bào)錯(cuò),不過(guò)這次可以看到是哪個(gè)地址請(qǐng)求不到了:

進(jìn)入http://mirror.centos.org/centos/8/后,發(fā)現(xiàn)并沒(méi)有AppStream目錄:

2.1.3 解決yum源問(wèn)題

在http://mirror.centos.org/centos/中查找,發(fā)現(xiàn)AppStream和BaseOS搬到了http://mirror.centos.org/centos/8-stream/下:

需要將repo文件中的$releasever換成8-stream:

vi CentOS-Linux-BaseOS.repo vi CentOS-Linux-AppStream.repo

CentOS-Linux-BaseOS.repo修改后:

# CentOS-Linux-BaseOS.repo # # The mirrorlist system uses the connecting IP address of the client and the # update status of each mirror to pick current mirrors that are geographically # close to the client. You should use this for CentOS updates unless you are # manually picking other mirrors. # # If the mirrorlist does not work for you, you can try the commented out # baseurl line instead.[baseos] name=CentOS Linux $releasever - BaseOS mirrorlist=http://mirrorlist.centos.org/?release=8-stream&arch=$basearch&repo=BaseOS&infra=$infra #baseurl=http://mirror.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

CentOS-Linux-AppStream.repo修改后:

# CentOS-Linux-AppStream.repo # # The mirrorlist system uses the connecting IP address of the client and the # update status of each mirror to pick current mirrors that are geographically # close to the client. You should use this for CentOS updates unless you are # manually picking other mirrors. # # If the mirrorlist does not work for you, you can try the commented out # baseurl line instead.[appstream] name=CentOS Linux $releasever - AppStream mirrorlist=http://mirrorlist.centos.org/?release=8-stream&arch=$basearch&repo=AppStream&infra=$infra #baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

再次嘗試安裝wget,即可成功下載安裝了

2.1.4 將容器打包鏡像
后面centos鏡像就使用該容器的

-bash-4.2# docker commit -m="yum" -a="aruba" ff centos-yum:1.0
2.2 DockerFile文件編寫(xiě)
vi dockerfile

內(nèi)容為:

# 基礎(chǔ)鏡像 FROM centos-yum:1.0 MAINTAINER aruba# 申明一個(gè)變量 ENV path /usr/local # 設(shè)置工作目錄 WORKDIR $path# 安裝vim RUN yum install -y vimEXPOSE 8080# 最后執(zhí)行命令 CMD /bin/bash

最后加上CMD /bin/bash后,如果創(chuàng)建容器時(shí)不指定后面的命令,那么會(huì)自動(dòng)執(zhí)行CMD命令進(jìn)入虛擬終端

2.3 構(gòu)建鏡像

語(yǔ)法:docker build -f dockerfile名稱 -t 新建的鏡像名:TAG .

docker build -f dockerfile -tcentos-vim:1.0 .

最后成功創(chuàng)建的信息:

創(chuàng)建啟動(dòng)容器,已經(jīng)可以使用vim了:

-bash-4.2# docker run -itcentos-vim:1.0 [root@56771cf32cbb local]# vim 1.txt

四、數(shù)據(jù)卷

前面我們了解到,容器啟動(dòng)是在鏡像層上添加了一個(gè)容器層,擁有獨(dú)立的進(jìn)程,也擁有獨(dú)立的數(shù)據(jù)存儲(chǔ),一旦容器不制作成鏡像就被刪除,那么原先容器內(nèi)部的數(shù)據(jù)也會(huì)刪除
數(shù)據(jù)卷打通了宿主機(jī)和容器之間的橋梁,可以將目錄或文件掛載到一個(gè)或多個(gè)容器中,實(shí)現(xiàn)數(shù)據(jù)的持久化和容器間共享

1. 數(shù)據(jù)卷使用

1.1 創(chuàng)建容器

創(chuàng)建容器時(shí),指定數(shù)據(jù)卷映射,語(yǔ)法為:

docker run -it -v /宿主機(jī)絕對(duì)路徑:/容器內(nèi)目錄[:ro] 鏡像名[:ro]為可選項(xiàng),表示只讀

在宿主機(jī)創(chuàng)建文件夾,并映射到容器中:

-bash-4.2# mkdir shared -bash-4.2# docker run -it -v ./shared:/root/c-shared centos-vim:1.0 docker: Error response from daemon: create ./shared: "./shared" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path. See 'docker run --help'. -bash-4.2# docker run -it -v /root/shared:/root/c-shared centos-vim:1.0

可以通過(guò)docker inspect中查找到數(shù)據(jù)卷信息:

1.2 容器內(nèi)目錄創(chuàng)建文件

在容器內(nèi)目錄創(chuàng)建一個(gè)文件:

[root@1576b2c30b88 c-shared]# cd /root/c-shared/ [root@1576b2c30b88 c-shared]# echo 123 >> a.txt

查看宿主機(jī)中文件內(nèi)容:

-bash-4.2# cd /root/shared -bash-4.2# cat a.txt 123
1.3 宿主機(jī)修改文件內(nèi)容

在宿主機(jī)中將文件內(nèi)容修改:

-bash-4.2# vi a.txt -bash-4.2# cat a.txt 123 abc

在容器內(nèi),查看文件內(nèi)容:

[root@1576b2c30b88 c-shared]# cat a.txt 123 abc
1.4 刪除容器

上面兩個(gè)例子說(shuō)明了,無(wú)論是在容器內(nèi)操作數(shù)據(jù)卷,還是在宿主機(jī)內(nèi)操作,數(shù)據(jù)都是同步的,接下來(lái)將容器刪除:

-bash-4.2# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1576b2c30b88 centos-vim:1.0 "/bin/sh -c /bin/bash" 6 minutes ago Exited (130) 7 seconds ago reverent_bhabha 56771cf32cbb centos-vim:1.0 "/bin/sh -c /bin/bash" 42 minutes ago Up 42 minutes 8080/tcp sad_yonath 19bad3f699e6 centos "/bin/bash" 2 hours ago Exited (1) 48 minutes ago flamboyant_kalam -bash-4.2# docker rm 15 15

宿主機(jī)內(nèi)的文件還是存在的:

-bash-4.2# ls a.txt -bash-4.2# cat a.txt 123 abc

2. 構(gòu)建鏡像指定數(shù)據(jù)卷

除了在創(chuàng)建容器時(shí),指定數(shù)據(jù)卷外,還可以在DockerFile構(gòu)建鏡像時(shí),指定數(shù)據(jù)卷,

2.1 構(gòu)建鏡像

dockerfile2文件內(nèi)容:

FROM centos-vim:1.0 MAINTAINER arubaVOLUME ["/root/shared2"]CMD /bin/bash

構(gòu)建鏡像:

docker build -f dockerfile2 -tcentos-share:1.0 .
2.2 創(chuàng)建啟動(dòng)容器
docker run -it --name aruba-centoscentos-share:1.0

此時(shí)查看容器的數(shù)據(jù)卷信息:

是映射到了宿主機(jī)上的一個(gè)隨機(jī)目錄/var/lib/docker/volumes/59365ccb1af478f0c6611d8a36ac1518f9b8da8f2330e416ba197d023260e78f/_data,并且每次創(chuàng)建新的容器,都會(huì)產(chǎn)生一個(gè)新的隨機(jī)目錄

2.3 數(shù)據(jù)卷中創(chuàng)建文件

我們?cè)谌萜鲾?shù)據(jù)卷中創(chuàng)建一個(gè)文件:

[root@4553615bd221 local]# cd /root/shared2/ [root@4553615bd221 shared2]# touch b.txt

宿主機(jī)中查看:

-bash-4.2# cd /var/lib/docker/volumes/59365ccb1af478f0c6611d8a36ac1518f9b8da8f2330e416ba197d023260e78f/_data -bash-4.2# ls b.txt
2.3 刪除容器
-bash-4.2# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4553615bd221 centos-share:1.0 "/bin/sh -c /bin/bash" 7 minutes ago Exited (130) 6 seconds ago aruba-centos d5f613aab62c centos-vim:1.0 "/bin/sh -c /bin/bash" 24 minutes ago Up 24 minutes 8080/tcp vibrant_hofstadter 56771cf32cbb centos-vim:1.0 "/bin/sh -c /bin/bash" About an hour ago Up About an hour 8080/tcp sad_yonath 19bad3f699e6 centos "/bin/bash" 3 hours ago Exited (1) About an hour ago flamboyant_kalam -bash-4.2# docker rm 45 45

宿主機(jī)中的文件還是存在的:

-bash-4.2# ls b.txt

3. 數(shù)據(jù)卷容器

如果多個(gè)容器要共用一個(gè)數(shù)據(jù)卷,一個(gè)個(gè)在創(chuàng)建時(shí)進(jìn)行手動(dòng)指定映射比較繁瑣,一個(gè)容器如果使用了數(shù)據(jù)卷,那么它就是一個(gè)數(shù)據(jù)卷容器,其他容器可以直接指定數(shù)據(jù)卷容器來(lái)實(shí)現(xiàn)共用

3.1 創(chuàng)建數(shù)據(jù)卷容器

基于上面的數(shù)據(jù)卷鏡像,重新創(chuàng)建一個(gè):

docker run -it --name aruba-centoscentos-share:1.0
3.2 --volumes-from容器關(guān)聯(lián)

創(chuàng)建另外兩個(gè)容器,使用--volumes-from關(guān)聯(lián)上該數(shù)據(jù)卷容器:

docker run-it --name aruba-centos2 --volumes-from aruba-centos centos-share:1.0 docker run-it --name aruba-centos3 --volumes-from aruba-centos centos-share:1.0
3.3 數(shù)據(jù)卷中創(chuàng)建文件

創(chuàng)建的三個(gè)容器都有/root/shared2目錄,隨便找個(gè)容器進(jìn)行文件的創(chuàng)建:

[root@86df03edebaf usr]# cd /root/shared2/ [root@86df03edebaf shared2]# touch c.txt

其他容器中也能夠看到剛剛創(chuàng)建的文件:

[root@ca9f28d72e9e local]# cd /root/shared2/ [root@ca9f28d72e9e shared2]# ls c.txt
3.4 刪除數(shù)據(jù)卷容器

將一開(kāi)始創(chuàng)建的數(shù)據(jù)卷容器刪除:

-bash-4.2# docker rm 7b 7b -bash-4.2# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 86df03edebaf centos-share:1.0 "/bin/sh -c /bin/bash" 3 minutes ago Up 3 minutes 8080/tcp aruba-centos3 ca9f28d72e9e centos-share:1.0 "/bin/sh -c /bin/bash" 4 minutes ago Up 4 minutes 8080/tcp aruba-centos2 d5f613aab62c centos-vim:1.0 "/bin/sh -c /bin/bash" 35 minutes ago Up 35 minutes 8080/tcp vibrant_hofstadter 56771cf32cbb centos-vim:1.0 "/bin/sh -c /bin/bash" About an hour ago Up About an hour 8080/tcp sad_yonath 19bad3f699e6 centos "/bin/bash" 3 hours ago Exited (1) About an hour ago flamboyant_kalam

其他的容器還依然存在著數(shù)據(jù)卷:

[root@86df03edebaf shared2]# ls c.txt

以上為Docker入門操作,主要包含Docker的安裝,Docker命令,鏡像拉取,自定義鏡像,容器操作,數(shù)據(jù)卷內(nèi)容

總結(jié)

以上是生活随笔為你收集整理的Docker-入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。