Image: 有领“好人卡”倾向的广大程序猿一定对 镜像 的概念不会陌生。但和 Windows 的那种 ISO 镜像相比,Docker 中的镜像是分层的,可复用的,而非简单的一堆文件迭在一起(类似于一个压缩包的源码和一个 Git 仓库的区别)。
Container: 容器的存在离不开镜像的支持,他是镜像运行时的一个载体(类似于实例和类的关系)。依托 Docker 的虚拟化技术,给容器创建了独立的端口、进程、文件等“空间”,Container 就是一个与宿机隔离 “容器”。容器可宿主机之间可以进行 port、volumes、network 等的通信。
Repository: Docker 的仓库和 Git 的仓库比较相似,拥有仓库名、tag。在本地构建完镜像之后,即可通过仓库进行镜像的分发。常用的 Docker hub 有 https://hub.docker.com/ 、 https://cr.console.aliyun.com/ 等。
> docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
c448d9b1e62f: Pull complete
0277fe36251d: Pull complete
6591defe1cd9: Pull complete
2c321da2a3ae: Pull complete
08d8a7c0ac3c: Pull complete
Digest: sha256:2152a8e6c0d13634c14aef08b6cc74cbc0ad10e4293e53d2118550a52f3064d1
Status: Downloaded newer image for ubuntu:18.04
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 18.04 58c12a55082a 44 hours ago 79MB
> docker create --name ubuntuContainer ubuntu:18.04
0da83bc6515ea1df100c32cccaddc070199b72263663437b8fe424aadccf4778
> docker start ubuntuContainer
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9298a27262da ubuntu:18.04 "/bin/bash" 4 minutes ago Up About a minute ubuntuContainer
> docker exec -it 9298
root@9298a27262da:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@9298a27262da:/# exit
> docker run -it --name runUbuntuContainer ubuntu:18.04 /bin/bash
root@57cdd61d4383:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@57cdd61d4383:/#
# docker ps 可以查到已经成功运行了 runUbuntuContainer
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57cdd61d4383 ubuntu:18.04 "/bin/bash" 9 seconds ago Up 8 seconds runUbuntuContainer
9298a27262da ubuntu:18.04 "/bin/bash" 9 minutes ago Up 6 minutes ubuntuContainer
> apt-get update
> apt-get install wget
> wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
# 安装完之后可能当前 session 读不到 nvm 命令,可以 exit 之后再进入终端环境
> nvm install 8.0.0
> node -v
> docker commit --author "rccoder" --message "curl+node" 9298 rccoder/myworkspace:v1
sha256:68e83119eefa0bfdc8e523ab4d16c8cf76770dbb08bad1e32af1c872735e6f71
# 通过 docker images 就能看到新制作的 rccoder/myworkspace 就躺在这里了
>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
rccoder/myworkspace v1 e0d73563fae8 20 seconds ago 196MB
> docker run -it --name newWorkSpace rccoder/myworkspace:v1 /bin/bash
root@9109f6985735:/# node -v
8.0.0
> docker login
> docker push rccoder/myworkspace:v1
The push refers to repository [docker.io/rccoder/myworkspace]
c0913fec0e19: Pushing [=> ] 2.783MB/116.7MB
bb1eed35aacf: Mounted from library/ubuntu
5fc1dce434ba: Mounted from library/ubuntu
c4f90a44515b: Mounted from library/ubuntu
a792400561d8: Mounted from library/ubuntu
6a4e481d02df: Waiting
# Comment
INSTRUCTION arguments
FROM rccoder/myworkspace:v1
RUN mkdir a
> docker build -t newfiledocker:v1 .
Sending build context to Docker daemon 3.584kB
Step 1/2 : FROM rccoder/myworkspace:v1
---> 68e83119eefa
Step 2/2 : RUN mkdir a
---> Running in 1127aff5fbd3
Removing intermediate container 1127aff5fbd3
---> 25a8a5418af0
Successfully built 25a8a5418af0
Successfully tagged newfiledocker:v1
# 新建基于 newfiledocker 的容器并在终端中打开,发现里面已经有 a 文件夹了。
> docker docker run -it newfiledocker:v1 /bin/bash
root@e3bd8ca19ffc:/# ls
a bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
https://docs.docker.com/docker-for-mac/install/#install-and-run-docker-for-mac
https://travis-ci.org/
https://docs.docker.com/engine/reference/builder/#environment-replacement
https://github.com/cnpm/nodeinstall
https://docs.docker.com/engine/reference/commandline/cli/
https://docs.docker.com/engine/reference/builder/
https://docs.docker.com/develop/develop-images/dockerfile_best-practices/