Docker通常用于如下场景
1. web应用的自动化打包和发布;2. 自动化测试和持续集成、发布;3. 在服务型环境中部署和调整数据库或其他的后台应用;4. 从头编译或者扩展现有的OpenShift或Cloud Foundry平台来搭建自己的PaaS环境。
docker入门操作
- 安装docker yum install -y docker
- 搜索可用的docker镜像 docker search tutorial
- 下载镜像docker pull learn/tutorial
- 在容器中运行hello worlddocker run learn/tutorial echo "hello word"
- 在容器中安装新程序docker run learn/tutorial apt-get install -y ping
- 保存对容器的修改docker ps -ldocker commit 698 learn/ping
- 运行新的容器
docker run lean/ping ping www.baidu.com
- 检查运行中的镜像docker ps -ldocker inspect efe
- 提交自己的镜像docker push learn/ping
- 镜像保存并压缩(其他虚机可以运行)华为公有云:docker pull swr.cn-north-1.myhuaweicloud.com/nl-crm/consumer-app:1.0.25
docker save -o consumer_app.1.0.25.tar swr.cn-north-1.myhuaweicloud.com/nl-
crm/consumer-app:1.0.25gzip consumer_app.1.0.25.tar
- 其他虚机下load、import压缩的镜像包docker load < consumer_app.1.0.25.tar docker import consumer_app.1.0.25.tar consumer_app:1.0.25(需指定repo:tags)
具体使用过程,其他常用docker指令
- docker查看日志:docker logs ea4 | head -200 (日志前两百行)| tail -200 (日志末尾两百行)
- docker进入容器:docker exec -it e9e2b11cd7ba /bin/sh
- dokcer关闭容器:docker kill/stop e9e2b11cd7ba (image_id)或者 ps -ef | grep e9e2b11cd7ba (image_id)kill -9 pid
- docker复制:docker cp /home/apps 96f7f14e99ab:/home/apps (主机到容器)docker cp 96f7f14e99ab:/home/apps /home/apps (容器到主机)
- 构建镜像 docker build consumerApp(Dockerfile路径) -t consumer_app:1.0.0(repo:tag) 备注:构建镜像不要使用image_id,上传镜像会提示,无效的镜像,镜像描述中含有空的RepoTags信息
- 查看所有镜像[root@ecs-crm1 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEswr.cn-north-1.myhuaweicloud.com/nl-crm/provider_app_1_image 1.0.15 a8410a111975 47 hours ago 143.6 MBswr.cn-north-1.myhuaweicloud.com/nl-crm/provider_app_1_image 1.0.14 a3db42771e45 2 days ago 143.6 MBswr.cn-north-1.myhuaweicloud.com/nl-crm/consumer-app 1.0.25 fef2ef661852 4 days ago 179.8 MBswr.cn-north-1.myhuaweicloud.com/nl-crm/consumer-app 1.0.24 5ceec9b0588f 4 days ago 179.8 MBswr.cn-north-1.myhuaweicloud.com/nl-crm/consumer-app 1.0.23 06bf99bd4086 2 weeks ago 179.8 MBswr.cn-north-1.myhuaweicloud.com/nl-crm/provider_app_2_image 1.0.7 5fcd4bb2fc88 2 weeks ago 678.9 MBswr.cn-north-1.myhuaweicloud.com/nl-crm/consumer-app 1.0.22 43141c4da6b8 2 weeks ago 677.9 MB
- 删除镜像[root@ecs-crm1 ~]# docker rmi a3db42771e45Untagged: swr.cn-north-1.myhuaweicloud.com/nl-crm/provider_app_1_image:1.0.14Deleted: sha256:a3db42771e458a40346232ff916b96ac60ee89f0b0cd3647fd125d6c1dec37abDeleted: sha256:1ea60be2f1d5b8538e59f2d06fdb0e1c70f3117ac88a7a800a5b96d5729b50c2Deleted: sha256:e82e6e4b526483f4c4085ca43852034013f34558f04c363638067994b1d2b8b2Deleted: sha256:f72c48ff65da7da451409bd83a9c67bd2bc5f847b047513741df4e7b5ef74d47Deleted: sha256:6976d8a582424ff1654c5765746a55072f530b670206d9660d017e44d820a432
- 补充 build、push、pull镜像相关流程如下图所示: