Docker 探秘
What is Docker?
Docker is the world’s leading software containerization platform.
What is a Container?
A standardized unit of software,Package software into standardized units for development, shipment and deployment.
Containers isolate software from its surroundings, for example differences between development and staging environments and help reduce conflicts between teams running different software on the same infrastructure.
-
LIGHTWEIGHT
Docker containers running on a single machine share that machine's operating system kernel; they start instantly and use less compute and RAM. Images are constructed from filesystem layers and share common files. This minimizes disk usage and image downloads are much faster.
-
STANDARD
Docker containers are based on open standards and run on all major Linux distributions, Microsoft Windows, and on any infrastructure including VMs, bare-metal and in the cloud.
-
SECURE
Docker containers isolate applications from one another and from the underlying infrastructure. Docker provides the strongest default isolation to limit app issues to a single container instead of the entire machine.
What is a Container image ?
A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings. Available for both Linux and Windows based apps, containerized software will always run the same, regardless of the environment.
What is a Repository?
Docker Image Hub
Docker Installations
Install Docker Community Edition (CE) For MAC
$docker -v
$docker info
$sudo usermod -aG docker your_username
$newgrp docker
鏡像加速
加速配置指引:https://cr.console.aliyun.com/
專用加速地址:https://registry.docker-cn.com/
如果有阿裏雲賬號,可以使用專屬的加速地址
Docker實戰
Docker基礎命令
$docker run hello-world
$docker run -d -p 80:80 --name webserver nginx
$docker run --name web -v /source/:/web -it ubuntu:14.04 /bin/bash
$docker run --rm --name vol -v /source/:/target:ro -it ubuntu:14.04 /bin/bash
$docker run -t -i -d --volumes-from vol --name test1 ubuntu:14.04 /bin/bash
docker run - 運行一個容器
-t - 分配一個(偽)tty (link is external)
-i - 交互模式 (so we can interact with it)
-d - 後台模式
ubuntu:14.04 - 使用 ubuntu 基礎鏡像 14.04
/bin/bash - 運行命令 bash shell
-v /host:/container
-p hostPort:containerPort
--volumes-from=containerId
--link :alias選項指定鏈接到的容器。
Docker 常用命令
$docker images
$docker search image_id
$docker pull image_id
$docker create -it fedora bash
$docker start container_id
$docker stop container_id
$docker restart container_id
$docker inspect container_id
$docker ps -a
$docker ps -l
$docker top
$docker kill container_id
Docker 深入探秘
交互式創建鏡像
$docker run -it --name test ubuntu bash
$docker attach [容器ID|容器名稱]
$docker exec -it [容器ID|容器名稱] /bin/bash
$xxx blabla ...
$docker differ [容器ID|容器名稱]
$docker commit -a "yangdy@" -m "my ubuntu" a404c6c174a2 myubuntu:1.0
$docker hisotry image_id
$docker cp [容器ID|容器名稱]:/container_path to_host_path
網絡映射 host
$docker run -d --name db training/postgres
$docker run -d -P --name web --link db:db training/webapp python app.py
端口映射
docker run -p [([<host_interface>:[host_port]])|(<host_port>):]<container_port>[/udp] <image> <cmd>
$docker run -p 127.0.0.1:80:8080 <image> <cmd>
Docker自動化構建
自動化構建(基於Dockerfile)
- 構建指令
$docker build -t tag .
$docker build --build-arg APP_NAME=appName --pull -f /home/admin/Dockerfile -t reg.docker.alibaba-inc.com/home/admin/appName/APP-META/docker-config
$docker push reg.docker.alibaba-inc.com/publish
- 最簡潔的構建文件,隻添加應用包
# 用基礎鏡像地址替換下方鏡像地址
FROM reg.docker.alibaba-inc.com/namespace/imageId:version
# 將構建出的主包複製到指定鏡像目錄中
COPY $APP_NAME.tgz /home/admin/${APP_NAME}/target/${APP_NAME}.tgz
- 基礎鏡像
# 基於基礎鏡像
FROM reg.docker.alibaba-inc.com/namespace/imageId:version
# 備注
MAINTAINER yangdy@aliyun.com
# 定義參數
ARG xxx_version=1.0.0
# 定義基礎鏡像類型
ENV docker_type="xxx:1.8.0"
#這裏,用docker的RUN命令,把需要軟件,使用linux的rpm命令安裝,以及初始化各種環境配置
RUN rpm -ivh --nodeps "https://rpmHost/xxx1.rpm" && \
rpm -ivh --nodeps "https://rpmHost/xxx2.rpm"
RUN wget -c "https://server/source.tgz" -O /home/admin/target.tgz
## 安裝 xxx 腳本
RUN curl -sLk https://server/install.sh | sh
# 將應用啟動腳本和nginx配置複製到鏡像中
COPY templates/app/bin/ /home/admin/app/bin/
COPY templates/cai/ /home/admin/cai/
COPY templates/start.sh /home/admin/start.sh
COPY templates/stop.sh /home/admin/stop.sh
# 設置文件夾操作權限
RUN mkdir /home/admin/logs /home/admin/output /home/admin/diamond /home/admin/cai/logs && \
chmod -R a+x /home/admin/app/bin/ /home/admin/*.sh && \
chown -R admin:admin /home/admin/*
# 掛載數據卷,指定目錄掛載到宿主機上麵,為了能夠保存(持久化)數據以及共享容器間的數據,為了實現數據共享,例如日誌文件共享到宿主機或容器間共享數據.
VOLUME /home/admin/logs \
/home/admin/output \
/home/admin/cai/logs \
# 容器啟動時自動執行的腳本,我們一般會將應用啟動腳本放在這裏,相當於係統自啟應用
ENTRYPOINT ["/home/admin/start.sh"]
# 將Aone build的APP_NAME傳進來
ONBUILD ARG APP_NAME
ONBUILD ENV APP_NAME=$APP_NAME
ONBUILD WORKDIR /home/admin/${APP_NAME}/bin
start.sh文件內容
#!/bin/bash
source /home/admin/entrypoint.sh
rm -f /home/admin/start.sh.code
/home/admin/xxx_app/bin/appctl.sh restart
code=$?
echo $code > /home/admin/start.sh.code
exit $code
鏡像曆史,發生了什麼
$docker history --no-trunc imageId
阿裏雲鏡像倉庫
登錄阿裏雲docker registry:
$ docker login -u dongyu.ydy reg.docker.alibaba-inc.com
登錄registry的用戶名是您的域賬號,你可以在鏡像管理首頁點擊右上角按鈕修改docker login密碼。
從registry中拉取鏡像:
$ sudo docker pull reg.docker.alibaba-inc.com/namespace/image:version
將鏡像推送到registry:
$docker tag [ImageId] reg.docker.alibaba-inc.com/public/repository:[鏡像版本號]
$docker push reg.docker.alibaba-inc.com/public/repository:[鏡像版本號]
其中[ImageId],[鏡像版本號]請你根據自己的鏡像信息進行填寫。
搭建倉庫
局域網搭建
docker registry是一個開源的代碼倉庫實現,我們可以直接拉下來在自己的本地建一個代碼倉庫,也可以直接用docker-hub官方的代碼倉庫.或者使用自己的服務空間搭建一套代碼倉庫。
安裝本地的 docker registry
$docker run -d -p 5000:5000 registry
下載並啟動一個registry容器創建本地的私有倉庫服務.docker會自動去下載並安裝registry
此時,我們可以將一些建好的docker放在本地registry裏麵
$docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry registry
啟動本地私有倉庫服務。監聽5000端口
提交docker鏡像
docker push 127.0.0.1:5000/test
提交成功以後,可以在宿主機上查看是否成功
curl https://127.0.0.1:5000/v1/search
之後,可以到任一機器去下載了,使用
docker pull ip:5000/test 即可
公網搭建
由於是在本機搭建的,隻能在局域網內使用。如果想做公開的,可以在公網上搭建一個,本人嚐試在阿裏雲的雲服務器ecs上搭建了一個。搭建的流程和上麵差不多,隻是需要把ip換成公網的即可。
不過需要注意的是。由於ECS默認的ubuntu版本默認內核不支持安裝docker。需要升級到3.8.0-25才可以。
升級方法:
$sudo apt-get install linux-image-3.8.0-25-generic
$sudo apt-get install linux-headers-3.8.0-25-generic
再重啟
shutdown -r now
More
docker help
最後更新:2017-11-01 21:04:13