s3 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 鏡像的發佈


[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


私有倉庫搭建

參考官方文檔
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

6 Dockerfile 練習1


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)

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  # 查看 執行日誌

7 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
相關文章
相關標籤/搜索