Docker 鏡像和容器


Docker技術里最為基礎的兩大概念:鏡像和容器。鏡像的獲取方式:從 re gistry拉取,從Dockerfile構建;容器的基本操作

1 Docker架構和底層技術簡介

Docker Platform

  • Docker提供了一個開發,打包,運行app的平台
  • 把app和底層infrastructure隔離開來
 

Docker Engine

  • 后台進程(dockerd)
  • REST API Server
  • CLI接口(docker)
 

Docker Architecture

 

底層技術支持

  • Namespaces:做隔離pid,net,ipc,mnt,uts
  • Control groups:做資源限制
  • Union file systems:Container和image的分層

2 Docker Image

Image 是文件和 meta data的集合(root filesystem)
分層的,並且每一層都可以添加改變刪除文件,成為一個新的image
不同的image可以共享相同的layer
Image本身是read-only的
Image的獲取
  • Build from Dockerfile
  • Pull from Registry

3 Container

 
通過Image創建(copy)
    在Image layer之上建立一個 container layer(可讀寫)
類比面向對象:類和實例
    Image負責app的存儲和分 發,Container負責運行app
 
[root@localhost ~]# docker ps -aq
41085d1b95c0
abb8db5ee1c5
b1b0babbe76a
c1b9dbf8fc48
d5c6d17741c0


批量刪除
[vagrant@localhost ~]$ docker rm $(docker container ls -aq)
4e540bbeeeb7
5f1c4f401024
6d3466972716
06db5785577c
32f005c8b20b
[vagrant@localhost ~]$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED NAMES

刪除已經退出的容器
[root@localhost ~]# docker rm $(docker ps -f "status=exited" -q)
41085d1b95c0
abb8db5ee1c5
b1b0babbe76a
c1b9dbf8fc48
d5c6d17741c0


構建自己的Docker鏡像
docker container commit  # 提交修改后的容器為鏡像
docker image build = docker build
docker ps -aq 批量刪除

4 Dockerfile 語法

FROM

FROM scratch    #制作base image
FROM centos    #使用base image
FROM ubuntu:14.04
 
FROM盡量使用官方的image作為base image!
 

LABEL

LABEL maintainer="fadewalk"
LABEL version="1.0"
LABEL description="This is description"
 
LABEL Metadata不可少!

RUN

RUN yum update && yum install-y vim\
    python-dev#反斜線換行
RUN apt-get update && apt-get install-y perl\
    pwgen --no-install -recommends && rm -rf\
    /var/lib/apt/lists/* #注意清理cache
RUN /bin/bash -c 'source $HOME/.bashrc;echo $HOME'
 
RUN為了美觀,復雜的RUN用反斜線換行
避免無用分層,合並多條命令成一行

WORKDIR

WORKDIR /root
WORKDIR /test  #如果沒有會自動創建test目錄
WORKDIR demo
RUN pwd      #輸出結果應該是/test/demo
 
 
用WORKDIR,不要用RUN cd!
盡量使用絕對目錄!

ADD and COPY

ADD hello /
ADD test.tar.gz /     #添加到根目錄並解壓
WORKDIR /root
ADD hello test/        #/root/test/hello
WORKDIR /root
COPY hello test/
   
ADD or COPY 大部分情況,COPY優於ADD!
ADD 除了COPY還有額外功能(解壓)
添加遠程文件/目錄使用curl或者wget

ENV

ENV MYSQL_VERSION 5.6    #設置常量
RUN apt-get install-y mysql-server= "${MYSQL_VERSION} "\
    && rm -rf /var/lib/apt/lists/*    #引用常量
 
盡量使用ENV增加可維護性

 

VOLUME and EXPOSE

(存儲和網絡)
 
 
https:/docs.docker.com/engine/reference/builder/#cmd
 
 
RUN:執行命令並創建新的 Image Layer
CMD:設置容器啟動后默認執行的命令和參數
ENTRYPOINT:設置容器啟動時運行的命令
 

Shell和Exec格式

Shell格式
RUN apt
-get install -y vim CMD echo "hello docker" ENTRYPOINT echo "hello docker"


Exec格式
RUN [
"apt-get""install""-y""vim"] CMD ["/bin/echo""hello docker"] ENTRYPOINT ["/bin/echo""hello docker"]

CMD and ENTRYPOINT

CMD

◆容器啟動時默認執行的命令
◆如果docker run指定了其它命令,CMD命令被忽略
◆如果定義了多個CMD,只有最后一個會執行

ENTRYPOINT

◆讓容器以應用程序或者服務的形式運行
◆不會被忽略,一定會執行
◆實踐:寫一個shell腳本作為entrypoint
COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 27017
CMD [“mongod"]

5 鏡像的發布

登錄發布到docker hub

[vagrant@ localhost hello-world]$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username:
Password:
 
[ vagrant@ localhost hello-world]$ docker push fadewalk/hello-world:latest
The push refers to repository [ docker. io/fadewalk/hello-worl]
e9b1612d382b: Pushed latest: digest: sha256: db345c850b25406fb24a0d7151e73155d9d1489f22bd8b182b0c010de2d4c5d7 size:527
 
https://github.com/smartbgp/dockerfiles 
 

私有倉庫搭建

參考官方文檔
Docker Registry
This image contains an implementation of the Docker Registry HTTP API V2 for use with Docker 1.6+.Se github.com/docker/distribution for more details about what it is.
$docker run
-d-p 5000:5000 --restart always --name registry registry:2
Now,use it from within Docker:
拉取私有倉庫鏡像
[vagrant@localhost hello-world]$ docker build -t 10.75.xx.222:5000/hello-world.
Sending build context to Docker daemon 847.9kB Step 1/3:FROM scratch Step 2/3:ADD hello/
--->8ff0b72f198c Step 3/3:CMD["/hello]
-->Running in df848216f04a Removing intermediate container df848216f04a
--->e97f8b5f0981
Successfully built e97f8b5f0981
Successfully tagged 10.75.44.222:5000/hello-world:latest
 
push到私有倉庫
[vagrant@localhost hello-world]$ docker push 10.75.xx.222:5000/hello-world
[vagrant@localhost hello-world]$ sudo more /etc/docker/daemon.json
|"insecure-registries":["10.75.xx.222:5000]}
驗證api
 

練習

Dockerfile 練習1

 code
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return "hello docker"
if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)

 dockerfile

FROM python:2.7
LABEL maintainer="fade"
RUN pip install flask
COPY app.py /app/
WORKDIR /app
EXPOSE 5000
CMD ["python", "app.py"] 

 

docker build -t kevin/flask-hello-world .
# build 一個容器
 
在創建容器過程中(失敗的時候),可以通過中間態的容器id,查看中間態的容器
 
docker exec -it 11a767d3a588 python
#                中間態容器id
 
docker inspect 5f6ab6f95518    # 查看容器
docker logs 5f6ab6f95518  # 查看 執行日志

Dockerfile 練習2

 
壓力測試軟件
stress --vm 1 --vm-bytes 500000M --verbose
 
創建壓力測試image
FROM ubuntu RUN apt
-get update && apt-get install -y stress ENTRYPOINT ["/usr/bin/stress"] # 創建容器就會執行 類似init CMD [] # 創建好后執行的命令,類似 rc.local # 傳入的參數 分配內存。 當只設置內存時,虛擬內存跟前者一樣 docker run --memory=200M kevin/ubuntu-stress --vm 1 --verbose # 顯示過程 設置 --cpu-shares 為占CPU資源的(%)相對權重 docker run --cpu-shares=10 --name=test1 xiaopeng163/ubuntu-stress --cpu 1 stress:info:[1] dispatching hogs:1 cpu,0io,0 vm,0 hdd

 

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM