Docker

Docker is a golang based open source container tool. In a typical way, one container runs a web server and web application, while a second container runs a database server. They are both isolated from server holds the containers.

Docker 基本概念

镜像与容器

image是包含需要运行文件的只读镜像,在其上面添加可以读写层从而形成容器container. image有两种获取方式: 从Dockerfile build,或者从docker hub/registry拉取。

网络通信

docker默认是用虚拟网络接口进行birdge桥接。docker本身会创建一个名为docker0的虚拟以太桥接,各个容器会创建名为vethxxxx的网络接口,容器们和宿主机之间的通信都是通过docker0完成。

Docker 指令

docker create: create a read-write layer(Unioned RW File System) on the top of image

docker start: create a isolated process space for file system

docker run: equal to docker create + docker start

docker commit: 将container的变化封装为一个新的镜像

docker tag + docker push将镜像上传为private registry
docker tag test_image host_to_private_registry/test_image
docker push host_to_private_registry/test_image

常见问题

docker container与docker host端口的互相访问:

  • 使用ifconfig查看host机的inet addr(一般为eth0), 使用docker inspect container_name 查看容器的ip地址,两者可以互相访问。

  • docker run时设置参数--network= host

  • 在18.03版本之后,使用host.docker.internal代替localhost来访问host的端口。(未验证)

同一host下的docker containers互相通信:

  • 理论上直接两个container的ip地址互ping,是可以连同的,但由于ip地址是容器启动时随机分配的,直接写死互相访问的ip地址是不合理的。解决方案是用–link,该参数会在iptable里分别给两个容器创建一条accept规则,允许互相访问开放的端口。操作步骤,在创建完container_a之后, 在docker run contianer_b时, 使用--link container_a:a即可在容器b里使用curl a:port访问容器a的端口。