多年不用, 发现很生疏了.

常用命令
$ docker container ls
$ docker container stop webserver
$ docker container ls -a
$ docker container rm webserver
$ docker image ls
$ docker image rm nginx
$ docker ps #=contianer ls
$ docker version
$ sysctl kern.hv_support #查看系统硬件支持, 支持显示: kern.hv_support:1
$ docker save -o images.tar image1 [image2 ...] 
$ docker export -o myContainner1.tar container1 
$ docker load -i images.tar 
$ docker import -i myContainer1.tar
$ docker logs <container id> #查看container的log
$ env | grep DOCKER #查看toolbox环境是否存在, 没有输出代表不存在, 用的是docker desktop
核心命令
$ docker pull #这个是不必要的, 可以直接run, docker会自动下载image 
$ docker container create #这个也不必要, 也是包含在run里面了
$ docker run -it alpine env #查看环境情况 //todo
$ docker run -it ubuntu /bin/bash  这个意思是 交互式interact(-i,), 在terminal(-t)直接操作, 使用bash. 此时, exit可以停止这个container
  • 建议熟读官方文档: https://docs.docker.com/engine/reference/commandline/run/
-a  --attach 一般是stdin, stdout, stderr
-c  --cpu shares
-d  --detach 背后运行并且打印container id出来
-e  --env  设置环境变量
-h  --hostname  container的hostname
-i  --interactive 交互式, 即便没有attach也保持stdin
-l  --label 设置元数据meta data
-m  --memory limit 内存限制
-1  --memory-swappiness 真正的container memory swappiness 从0-100
-p  --publish Publish a container’s port(s) to the host
-P  --publish-all 		Publish all exposed ports to random ports
-t  --tty Allocate a pseudo-TTY(伪终端就是ssh连接的terminal)
-u  --user 用户名或者UID, 格式:  <name|uid>[:<group|gid>]
-v  --volume 绑一个volume, 把本地的地址绑到container里面
-w  --workdir container中的工作目录
--mount 这个更容易阅读

例子

$ docker run --read-only --mount type=volume,target=/icanwrite busybox touch /icanwrite/here
$ docker run -t -i --mount type=bind,src=/data,dst=/data busybox sh
基础概念
  • image, 原始的镜像
  • container, 镜像实际运行时, 就是一个container, 基于container的某个状态可以制造image, 这个对于测试真的很方便.
  • docker toolbox是基于virtualbox的一套环境包含docker, docker-compose, docker-machine
  • docker desktop包含docker docker-compose, 他使用了苹果提供的hyperkit替换了virtualbox, 并且不使用docker-machine
  • 官网
    • 有张图片特别好: https://docs.docker.com/engine/images/engine-components-flow.png
    • 官网地址: https://docs.docker.com/engine/docker-overview/
背后概念

基础概念的基础概念

  • namespaces 举例说明:
    • The pid namespace: Process isolation (PID: Process ID).
    • The net namespace: Managing network interfaces (NET: Networking).
    • The ipc namespace: Managing access to IPC resources (IPC: InterProcess Communication).
    • The mnt namespace: Managing filesystem mount points (MNT: Mount).
    • The uts namespace: Isolating kernel and version identifiers. (UTS: Unix Timesharing System).
  • control groups (cgroups) 控制应用使用的资源范围.
  • union file systems (unionFS) 包括AUFS, btrfs, vfs 和 devicemapper
  • container format, 目前用libcontainer, 未来会支持bsd jails和solaris zones
整体思路
  1. 本地开发的代码也形成一个image->container
  2. dockerfile配合compose解决问题

  3. 只要一个目录里面有dockerfile这个文件, docker就可以把这个目录转化为docker镜像.
  4. 然后就可以运行这个镜像. 这个镜像里面如果有个python或者nodejs就可以直接跑起来, 并不需要在镜像里面装python环境. 因为docker会自动搞定这些.
官方入门做一遍吧.

https://docs.docker.com/get-started/part2/

neton #这是我本地的打开代理的命令
git clone https://github.com/dockersamples/node-bulletin-board
cd node-bulletin-board/bulletin-board-app
netoff #这是我本地的关闭代理的命令

dockerfile的内容;

FROM node:current-slim  #基础镜像

WORKDIR /usr/src/app  #container里面的工作目录
COPY package.json .  #把host本地的文件copy到container工作目录
RUN npm install  #构建编译时执行的命令

EXPOSE 8080   #run运行时映射的端口
CMD [ "npm", "start" ]  #run运行时执行的命令

COPY . .   #把文件都copy进去. 这个是打包思路, 如果开发最好是映射目录

这里解释一下run和cmd的区别,

  • run是构建container时要执行的操作.
  • cmd是每次运行container都要默认执行的操作.
# 进到包含dockerfile的目录, 制造image
docker image build -t bulletinboard:1.0 .
# run化image为container
docker container run -p 8000:8080 -d --name bb bulletinboard:1.0
# 硬删除, 不论是否运行
docker container rm --force bb
docker compose, swarm & k8s
### Docker:
- Docker is the container technology that allows you to containerize your applications.
- Docker is the core of using other technologies.
### Docker Compose
- Docker Compose allows configuring and starting multiple Docker containers.
- Docker Compose is mostly used as a helper when you want to start multiple Docker containers and doesn't want to start each one separately using docker run ....
- Docker Compose is used for starting containers on the same host.
- Docker Compose is used instead of all optional parameters when building and running a single docker container.
- 简单地说如果host就是一个机器, 那么compose够用了.
### Docker Swarm
- Docker swarm is for running and connecting containers on multiple hosts.
- Docker swarm is a container cluster management and orchestration tool.
- It manages containers running on multiple hosts and does things like scaling, starting a new container when one crashes, networking containers ...
- Docker swarm is docker in production.
- It is the native docker orchestration tool that is embedded in the Docker Engine.
- The docker swarm file named stack file is very similar to a Docker compose file.
### Kubernetes
- Kubernetes is a container orchestration tool developed by Google.
- Kubernete's goal is very similar to that for Docker swarm.
- 如果你的host就是分布式的, 那么k8s是个不错的选择
### Docker Cloud
- A paid enterprise docker service that allows you to build and run containers on cloud servers or local servers.
- It provides a Web UI and a central control panel to run and manage containers while providing all the docker features in a user-friendly Web interface.
- 其中provide application, node, and swarm cluster management都停止了. 
compose的案例

https://github.com/play-with-docker/play-with-docker.github.io

git clone git@github.com:play-with-docker/play-with-docker.github.io.git
cd play-with-docker.github.io                                           
docker-compose up
# 然后访问http://localhost:4000

其中的: Application Containerization and Microservice Orchestration 讲述了如何使用compose.

version: '3'
services:
  api:
    image: linkextractor-api:step4-python
    build: ./api #这个意思是是这个目录里面有dockerfile
    ports:
      - "5000:5000"
  web:
    image: php:7-apache
    ports:
      - "80:80"
    environment:
      - API_ENDPOINT=http://api:5000/api/  #这里就是声明依赖的服务(//api:)和端口(:5000)类型(http://)路径(/api/), 并没有ip, 因为ip由compose管理
    volumes:
      - ./www:/var/www/html  #host的/www目录映射到container的/var/www/html目录
git checkout step4 #这一步会拿到上面的docker-compose.yml
docker-compose up -d --build
其实有两个做法
  1. 比较傻的做法直接用dockerfile, 下载一个ubuntu, 然后安装所有需要的内容, 接下来保存这个container.
  2. 还可以用compose搭建一套出来, 这个需要了解compose.
mac专属bug
  • 这里要特别注意, 这个play不兼容chrome, 在safari是正常的. 我错了, 其实safari也不行.
  • 如果在compose.yml里面映射: /etc/localtime:/etc/localtime 会失败, 原因docker->preferences里面的file-share要设置etc/localtime, 但是, 这个目录被mac保护了, 不太能操作这个file share.
  • container里面可以用host.docker.internal代替host的ip.