常用命令
docker build -t mytestimage:v0.0.1 .//新建一个image, 当前目录下需要有Dockerfiledocker create --network host -it -v /:/root --name test_machine mytestimage:v0.0.1 /bin/bash//基于image, 创建一个container, 其中网络与主机共享,交互模式,分配伪终端,名字test_machine, image版本,进入时程序执行的命令。docker start -ai test_machine//启动一个docker,将输出 attach 出来,将输入attach出来docker exec -it test_machine /bin/bash//进入一个正在运行的docker
docker create 与docker run的区别,docker run会自动start,而create需要手动start
docker image ls//List Docker imagesdocker container ls&docker ps //the same//List Docker containersdocker run hello-world// Execute Docker image
容器就就是运行时的镜像 A container is a runtime instance of an image--what the image becomes in memory when executed (that is, an image with state, or a user process). You can see a list of your running containers with the command, docker ps, just as you would in Linux.
build an image
类似自己做了一个OS
docker build --tag=friendlyhello:v0.0.1 .
run container
类似新建了一个虚拟机,并且安装了这个build的系统,每次执行docker run指令,都会创建一个container,大量创建后会造成空间的浪费。docker run = docker create + docker start
docker run --network host -it -v /home/someone:/root/someone ubuntu:latest /bin/bash(直接使用宿主机器的网络,映射home目录,以及使用bash作为入口点)
- 可以在Dockerfile指定[CMD],就是docker 中运行的程序,也可以通过entrypoint外部进行指定。
- 我的理解,it分别代表attach stdin以及attach stdout, 当只要输出时,-t 就可以了
- The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.
- Ref: stackoverflow.com/questions/3…
start exec
类似启动虚拟机,exec是进入正在启动的虚拟机
docker start后接container iddocker kill与docker stop接近,但是它直接发SIGKILL信号。docker exec -it 2b14x echo "china"在一个运行的docker里面执行命令。
service
service 由一系列container的组成,但是只是一个image, 具体的配置可以通docker-compose.yml文件配置,service里面的container有时候称之为task。
Services are really just “containers in production.” A service only runs one image, but it codifies the way that image runs—what ports it should use, how many replicas of the container should run so the service has the capacity it needs, and so on. Scaling a service changes the number of container instances running that piece of software, assigning more computing resources to the service in the process.
docker swarm initdocker stack deploy -c docker-compose.yml service_name部署service
yml文件如下所示:
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: username/repo:tag
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "4000:80"
networks:
- webnet
docker service ls可以看到有多少个service正在运行。docker service ps service_name可以看到service中task(container)的状态docker stack rm service_namedocker swarm leave --force
image container service swarm stack
- image: 由dockerfile build,类似exe文件。
- container:运行中的image,类似进程,一个image可以有多个container
- service:一组同类型的container,有时称之为task
一般实现同一个功能,能够自动负载均衡
- swarm:一堆计算机群,可以是物理机器,也可以是虚拟机。
一个机器称之为一个node,其中有一个node为manager,其他均为worker。
- stack:一组Service,能够组成一个较为完成的引用程序,默认情况下,一个Stack共用一个Network,相互可访问,与其它Stack网络隔绝。
none
Docker 里面的分为两种:
- the good ones: 潜在的中间依赖项,
docker image ls无法直接显示,需要通过docker images -a显示。 - the bad ones: 当构建新版本的时候,老版本就会悬空,这时候就需要修剪,通过
docker image prune
CMD VS Entrypoint
Container 运行时候的进程是entrypoint + command. entrypoint 是命令,cmd是参数,当entrypoint为空时,cmd也可以是命令.
以下两个命令用来检查cmd以及entrypoint.
sudo docker inspect --format='{{.Config.Cmd}}' hello-world:v0.1sudo docker inspect --format='{{.Config.Entrypoint}}' hello-world:v0.1
启动docker时候修改entrypoint 以及cmd
sudo docker run --rm -it --entrypoint /bin/ls hddl/share_runner:v0.1 /usr/local -l
Remove unused image
docker image prune移除dangling(游离)的image(一般是因为build image 中途退出导致的,或者build的时候没有指定tag)docker image prune -a危险操作,移除所有的image(如果正在被container使用则无法移除)docker container prune移除所有stopped container.
有时候无法移除,是因为被一些container引用着。需要先移除
docker container prune
Move docker image
docker save -o image_archive ubuntu:22.04docker load -i ./image_archive
Move docker container
docker export -o container_archive intelligent_paninicat ./container_archive | docker import - ubuntu:zzz
it can only be imported as a image rather than a container