添加标签并上传镜像
在这一节中,你会添加标签并上传你的docker-whale
镜像到你最近创建的仓库。完成时,你会通过下载你的镜像来测试这个仓库。
第一步:添加标签并上传镜像
- 回到终端
列出你已拥有的镜像
$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE docker-whale latest 7d9495d03763 38 minutes ago 273.7 MB docker/whalesay latest fb434121fc77 4 hours ago 247 MB hello-world latest 91c95931e552 5 weeks ago 910 B
找到你的
docker-whale
镜像的IMAGE ID
本例中,这个id是7d9495d03763
。
注意到目前为止,REPOSITORY
项显示了仓库的名称docker-whale
但是没有名称空间。你需要包含namespace
以便于Docker Hub将它与你的账号关联。名称空间和你的Docker Hub账号名字一样。你需要将你的镜像重命名为你的DockerHub名字/docker-whale
。- 使用
IMAGE ID
和docker tag
命令给你的docker-whale
镜像添加标签。
你输入的命令就像这样:
当然,你的账号名字要是你自己的。所以,输入你的镜像ID和你的账号名字并按下回车。$ docker tag 7d9495d03763 maryatdocker/docker-whale:latest
- 再次输入
docker images
命令来查看你刚刚打上标签的镜像。$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE maryatdocker/docker-whale latest 7d9495d03763 5 minutes ago 273.7 MB docker-whale latest 7d9495d03763 2 hours ago 273.7 MB docker/whalesay latest fb434121fc77 5 hours ago 247 MB hello-world latest 91c95931e552 5 weeks ago 910 B
- 使用
docker login
命令从命令行登录Docker Hub账号。
登录命令的格式是这样的:
收到提示后输入你的密码并输入回车。所以,举个例子:docker login --username=yourhubusername --email=youremail@company.com
$ docker login --username=maryatdocker --email=mary@docker.com Password: WARNING: login credentials saved in C:\Users\sven\.docker\config.json Login Succeeded
- 使用
docker push
命令来推送你的镜像到你的新仓库中。$ docker push maryatdocker/docker-whale The push refers to a repository [maryatdocker/docker-whale] (len: 1) 7d9495d03763: Image already exists c81071adeeb5: Image successfully pushed eb06e47a01d2: Image successfully pushed fb434121fc77: Image successfully pushed 5d5bd9951e26: Image successfully pushed 99da72cfe067: Image successfully pushed 1722f41ddcb5: Image successfully pushed 5b74edbcaa5b: Image successfully pushed 676c4a1897e6: Image successfully pushed 07f8e8c5e660: Image successfully pushed 37bea4ee0c81: Image successfully pushed a82efea989f9: Image successfully pushed e9e06b06e14c: Image successfully pushed Digest: sha256:ad89e88beb7dc73bf55d456e2c600e0a39dd6c9500d7cd8d1025626c4b985011
- 回到你Docker Hub的个人信息页查看你的新镜像.
第二步:获取你的新镜像
在这最后一节中,你会下载你刚刚上传到hub上的镜像。然而在那之前,你需要删除在你电脑上的本地镜像。如果你在你的电脑上保留本地镜像,Docker不会从hub上下载————不然呢?两个镜像是完全一样的啊。
- 把你的光标放置到终端窗口。
- 输入
docker images
列出你本地已拥有的镜像。
为了让测试顺利进行,你需要从你的本地主机移除$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE maryatdocker/docker-whale latest 7d9495d03763 5 minutes ago 273.7 MB docker-whale latest 7d9495d03763 2 hours ago 273.7 MB docker/whalesay latest fb434121fc77 5 hours ago 247 MB hello-world latest 91c95931e552 5 weeks ago 910 B
maryatdocker/docker-whale
和docker-whale
镜像。移除它们能让接下来的docker pull
从你的仓库中得到镜像。 - 使用docker rmi移除之前提到的两个镜像。
你可以使用ID或者名字来移除一个镜像$ docker rmi -f 7d9495d03763 $ docker rmi -f docker-whale
- 用
docker run
命令从你的远程仓库中拉取并载入新的镜像。
这个命令需要包含你在Docker Hub的用户名。
因为你的本地系统已经不包含这个镜像了,Docker下载它。docker run yourusername/docker-whale
$ docker run maryatdocker/docker-whale Unable to find image 'maryatdocker/docker-whale:latest' locally latest: Pulling from maryatdocker/docker-whale eb06e47a01d2: Pull complete c81071adeeb5: Pull complete 7d9495d03763: Already exists e9e06b06e14c: Already exists a82efea989f9: Already exists 37bea4ee0c81: Already exists 07f8e8c5e660: Already exists 676c4a1897e6: Already exists 5b74edbcaa5b: Already exists 1722f41ddcb5: Already exists 99da72cfe067: Already exists 5d5bd9951e26: Already exists fb434121fc77: Already exists Digest: sha256:ad89e88beb7dc73bf55d456e2c600e0a39dd6c9500d7cd8d1025626c4b985011 Status: Downloaded newer image for maryatdocker/docker-whale:latest ________________________________________ / Having wandered helplessly into a \ | blinding snowstorm Sam was greatly | | relieved to see a sturdy Saint Bernard | | dog bounding toward him with the | | traditional keg of brandy strapped to | | his collar. | | | | "At last," cried Sam, "man's best | \ friend -- and a great big dog, too!" / ---------------------------------------- \ \ \ ## . ## ## ## == ## ## ## ## === /""""""""""""""""___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~ \______ o __/ \ \ __/ \____\______/
下一步是什么
你已经做了很多了,已经完成了所有下列的Docker任务。
- 下载Docker
- 在容器中运行一个软件镜像
- 在Docker Hub上找到一个有趣的镜像
- 在你的机器上运行那个镜像
- 通过修改一个镜像创建你自己的镜像并运行它
- 创建一个Docker Hub账号和一个仓库
- 推送你的镜像到Docker Hub以便与他人分享 (教程结束)