Docker: 认识与使用
1. 特点
- 秒级启动速度
- 接近原生的性能
- MB级DISK USE
- 安全隔离,优秀的迁移性
- 操作系统级的虚拟化技术
2. 技术分析
2.1 核心概念
- 镜像 image: "一个只读的模板"
- 容器 container: "一个简易的Linux环境"
- 仓库 repository: "公有/私有的一个image仓库",如Docker Hub
2.2 本质
- docker软件本身: 基于内核的技术,依赖操作系统内核的引擎,根据操作系统来部署。
- image镜像:二进制包,运行在docker引擎上面。和操作系统没关系,最大的价值就是镜像打包技术。
image的使用界限:
- 不同操作系统族的镜像不能共用,Docker镜像是操作系统族相关的。
- 不同CPU架构的image不能共用,如X86和ARM的镜像不能共用。
3. Images usage
cmd: images, tag
3.1 Check docker infos
// pull images
// docker [image] pull NAME[:TAG]
docker pull hello-world:latest
// list images
docker images
docker image ls
// add tag
docker tag hello-world:lastest coolhello:latest
// check xx image more detail
docker inspect hello-world
// docker xx image history
docker history hello-world
3.2 Search docker
cmd: search
// docker search [option] keyword
// -f, --filter: filter output
// --format string: foprmat output
// --limit int: limit output image nums(default=25)
// is official?
docker search --filter=is-official=true nginx
// is stars > 4?
docker search --filter=stars=4 nginx
3.3 Remove and Clean images
cmd: rm/rmi
// docker rmi NAME [IMAGE..]
// docker image rm NAME [IMAGE..]
// -f, -force: force delete
// image should be NAME or ID
// remove by NAME
docker rmi coolhello:latest
docker -f rmi hello-world:latest
// remove by ID, will remove all tags and then remove itself
docker -f rmi c787697c564b
3.4 Create iamge
cmd: commit, import, build
3 Methods:
- Based on 已有的镜像容器创建
- 导入本地的模板
- 基于Dockerfile创建
// 1. 基于已有的容器创建
// docker [container] commit NAME/ID NEW_NAME:VERSION
// -a: user info
// -c: commit with dockfile cmd
// -m: add memo
// -p: commit with stop container
// step1: start a container
docker run -it ubuntu:18.04 /bin/bash
// step2: change and check like touch test.txt, and image id will be changed
// step3: commit a new image
docker [container] commit -m "add new file" -a "kumata" 8a96eeaf4223 NEWimage:0.1
// 2. 基于本地模板导入
// docker [container] import [OPTIONS] file|URL|-[REPOSITORY[:TAG]]
cat ubuntu-18.04.tar.xz | docker import - ubuntu:18.04
// 3. 基于Dockerfile创建(基于某个父镜像创建)
// docker [image] build
docker [image] build -t python:3
3.5 Save and Load image
cmd: save, load
// docker [image] save
// -o string: output image and tar
docker save -o ubuntu18.04.tar.xz ubuntu:18.04
// docker [image] load
// -i string: input tar-file into docker
docker load -i ubuntu18.04.tar.xz
docker load < ubuntu18.04.tar.xz
3.6 Upload image to Dock Hub
cmd: push
// docker [images] push NAME[:TAG] | [REGISTRY_HOST[:REGISTRY_PORT]/]NAME[:TAG]
// add new tag
docker tag test:latest group/test:latest
// upload
dock push group/test:latest
4. Container usage
command: create, start, run, wait, logs
4.1 create containner
https://docs.docker.com/engine/reference/commandline/create/
// docker [container] create
docker create -it ubuntu:latest
// docker [container] start
docker start
// docker [container] run
docker run ubuntu /bin/echo 'helloworld'
docker run -it ubuntu:18.04 /bin/bash
// daemon running
docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
// watch running logs
docker logs [ID]
4.2 stop container
cmd: pause/unpause, stop, prune
// docker [container] pause/unpause
// start and pause container
docker run --name test --rm -it ubuntu bash
docker pause test
// recover
docker unpause test
// docker [container] stop
docker stop test
// docker [container] restart
docker restart test
4.3 enter container
cmd: attach, exec
// docker [container] attach [--detach-key[=[]]] [--no-stdin] [--sig-proxy[=true]]
docker run -itd ubuntu
// docker [container] exec
docker exec -it NAME /bin/bash
4.4 delete container
cmd: rm
// docker [container] rm [-f|--force] [-l|--link] [-v|--volumes] CONTAINER
// -f, force delete
// -l, rm container 's link, but keep container
// -v, delete container 's mount
// wetch status
docker ps -a
docker rm NAME
4.5 import/output container
cmd: export, import
// output container
// docker [container] export [-o|--output[='']] CONTAINER
docker export -o test_for_run.tar ce5
// input container
// docker [container] import [-c|--change[=[]]] [-m|--message[=MESSAGE]] file||URL-[RESPOSITORY[:TAG]]
docker export -o test_for_run.tar ce5
4.6 watch container
cmd: inspect, top, stats
// watch detail
docker container inspect test
// watch process
docker top test
// watch statis
docker stats test
Github地址:https://github.com/kumataahh