文章作者:Tyan
博客:noahsnail.com
1. Docker命令
| 1 | 
 | 
1.1 docker run hello-world解析
这个命令总共有三部分:
- docker:告诉操作系统你使用的是docker程序 
- run:创建和运行docker容器的子命令 
- hello-world:告诉docker将哪一个镜像加载到容器中 
1.2 docker run hello-world的运行过程
image是一个文件系统,里面有运行时使用的参数。它没有状态且不能改变。容器是镜像的运行实例。命令执行时,Docker Engine会进行以下的步骤:
- 检查hello-world软件镜像是否存在 
- 如果不存在,从Docker Hub上下载hello-world 
- 加载镜像到容器中并运行 
运行是根据镜像的构建过程执行的,可能一直运行,也可能执行几个命令就退出。镜像可能是非常复杂的,例如镜像可以启动一个数据库软件。
2. 创建自己的docker image
2.1 写一个Dockerfile
| 1 | 
 | 
在Dockerfile中加入下面的代码并保存:
| 1 | FROM docker/whalesay:latest | 
2.2 从Dockerfile中创建image
| 1 | 
 | 
2.3 创建image的过程
- 首先Docker检查确保有构建的需要的东西
| 1 | Sending build context to Docker daemon 2.048 kB | 
- 然后Docker加载whalesay镜像
| 1 | Step 1 : FROM docker/whalesay:latest | 
- Docker开始执行apt-get命令
| 1 | Step 2 : RUN apt-get -y update && apt-get install -y fortunes | 
- 执行CMD命令
| 1 | Removing intermediate container 3c381fdef64a | 
2.4 测试自己创建的image
| 1 | $ docker images | 
3. 在Docker Hub上创建自己的仓库,与Git类似
略.
4. Tag, push, and pull your image
Step 1: Tag and push the image
- 用docker images查看你当前的image
| 1 | REPOSITORY TAG IMAGE ID CREATED SIZE | 
- 用docker tag命令和IMAGE ID给image打上tag,YOUR_DOCKERHUB_NAME为你的Docker Hub帐号,它起一个namespace的作用
| 1 | docker tag d1178b780ac6 YOUR_DOCKERHUB_NAME/docker-whale:latest | 
- docker images重新查看image
| 1 | REPOSITORY TAG IMAGE ID CREATED SIZE | 
- 使用docker login命令登录你的Docker Hub账户
| 1 | $ docker login | 
- 使用docker push将image推送到Docker Hub
| 1 | $ docker push ***/docker-whale | 
- 你可以在你的Docker Hub上看到*/docker-whale image了
Step 2: Pull your new image
- 先删除本地的docker-whale
| 1 | # 用id删除image | 
- 通过docker run来pull image
| 1 | $ docker run ***/docker-whale | 
 
          