Linux 系統中 Docker 容器安裝及使用
阿新 • • 發佈:2018-09-08
技術分享 doc 輕量級 sudo ali 高性能 公有 新的 fig
運行Hello World:
Docker
簡介
產生背景
項目的開發環境和部署環境不一致,部署環境配置難度大。集群技術的發展,集群的相同配置操作難度大。
基本理念
使用Go語言實現的雲開源項目,"一次編譯,處處運行",只需要一次配置環境,就可以在其他環境一鍵部署,軟件即容器,虛擬化。
虛擬化技術
虛擬機:一種帶環境安裝的解決方案,模擬整套操作系統,笨重。
容器:將所有運行環境打包成互相隔離的容器,不進行硬件虛擬。
開發/運維(DevOps)
開發自運維、一次開發,處處運維。
官方資料
Docker
Docker中文
Docker Hub
環境安裝
系統要求
1. CentOS 7(64位) 2. CentOS6.5(64位)及以上 3. 系統內核版本為2.6.32-431以上
基本結構
- 鏡像(image):只讀模板,一個鏡像可以創建多個容器。
- 容器(container):運行一個或多個應用,簡易版Linux。
- 倉庫(repository):集中存放鏡像,私有庫、公有庫。
安裝方法
CentOS 6.8 安裝
yum install -y epel-release
yum install -y docker-io
vi /etc/sysconfig/docker
service docker start
驗證安裝
CentOS 7 安裝
1. yum install -y yum-utils device-mapper-persistent-data lvm2 2. yum-config-manager --add-repo \ https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 3. yum-config-manager --enable docker-ce-edge 4. yum-config-manager --enable docker-ce-test 5. yum-config-manager --disable docker-ce-edge 6. yum install docker-ce #安裝特定版本 7. yum list docker-ce --showduplicates | sort -r yum install docker-ce-<VERSION STRING> 8. systemctl start docker #開啟 #測試 會自動跑一個hello 容器 docker run hello-world #####以上是官方給出的方法################################ ############實際我第一次安裝只用到了下面的命令############# #下載一個repo 到repo的文件夾中 wget https://mirrors.aliyun.com/dockerce/linux/centos/docker-ce.repo #使用yum 命令安裝docker-ce 也就是docker 社區版 yum install docker-ce -y
Hello World
阿裏鏡像倉庫
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-‘EOF‘
{
"registry-mirrors": ["https://l608quqb.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
#開機自動啟動
sudo systemctl enable docker
運行Hello World:docker run hello-world
運行原理
C/S結構,應用程序及其環境運行在容器中,比虛擬機有更少的抽象層,利用宿主內核,不虛擬硬件,不虛擬新的容器運行環境,利用宿主啟動速度快。
常用命令
幫助命令
docker version
docker info
docker help
鏡像命令
docker imgages [-a][-q]
docker search [-s]
docker pull
docker rmi -f [name:version][ID]
docker rmi -f $(docker -q) 刪去全部
容器命令
#啟動方式
docker run -i -t centos 進入了CentOS
-d:以後臺運行容器,返回容器ID,守護式容器
-i:以交互模式運行容器
-t:為容器重新分配終端
-P:隨機端口映射
-p:指定端口映射
ip:hostPort:containerPort
ip::containerPort
hostPort:containerPort
containerPort
#查看容器使用歷史
docker ps
-a:運行中+歷史
-l:最近創建
-n:最近n個
-q:靜默模式
--no-trunc:不截斷輸出
宿主bash:exit:退出並關閉
ctrl+p+q:返回主機
#返回容器
docker attach [id] 返回容器
#停止容器
docker restart/stop [name][id]
docker kill [name][id]
#刪除容器
docker rm -f $(docker -a -q) 刪除容器
dockeer -a -q |xargs docker rm -f 批量刪除容器
docker log
docker top [id]
docker inspect [id]
docker cp [id]
####################
docker run -itd [id]:uri uri
鏡像
聯合文件系統(UnionFS)
一種分層、輕量級並且高性能的文件系統,支持對文件系統的修改作為一次提交來一層層低價,可以將不同目錄掛在到同一個虛擬文件系統下。
鏡像加載原理
bootfs(boot file system):包含Bootloader和Kernel,鏡像最底層為bootfs,加載完成後,內核導入內存,內存使用權轉交給內核,卸載bootfs。
rootfs(root file system):包含/dev、/bin、/et等標準目錄和文件。
Linux 系統中 Docker 容器安裝及使用