现在您已经更新了待办事项应用,接下来需要为该应用创建容器镜像并将其分享到 Docker Hub。为此,您需要执行以下操作:
在您深入学习实践指南之前,以下是一些您应该了解的核心概念。
如果你是容器镜像的新手,可以把它们想象成一个标准化的软件包,其中包含运行应用程序所需的一切,包括应用程序的文件、配置和依赖项。这些软件包可以分发并与他人共享。
要共享 Docker 镜像,你需要一个地方来存储它们。这就是镜像仓库的作用所在。虽然有很多镜像仓库,但 Docker Hub 是默认且首选的镜像仓库。Docker Hub 既可以让你存储自己的镜像,也可以让你找到其他人的镜像,用于运行或作为创建自己镜像的基础。
在选择基础镜像时,Docker Hub 提供两类受信任的、由 Docker 维护的镜像:
In Develop with containers, you used the following images that came from Docker Hub, each of which are Docker Official Images:
Explore the full catalog of trusted content on Docker Hub:
In this hands-on guide, you'll learn how to sign in to Docker Hub and push images to Docker Hub repository.
To push images to Docker Hub, you will need to sign in with a Docker account.
Open the Docker Dashboard.
Select Sign in at the top-right corner.
If needed, create an account and then complete the sign-in flow.
Once you're done, you should see the Sign in button turn into a profile picture.
Now that you have an account, you can create an image repository. Just as a Git repository holds source code, an image repository stores container images.
Go to Docker Hub.
Select Create repository.
On the Create repository page, enter the following information:
getting-started-todo-appSelect Create to create the repository.
Now that you have a repository, you are ready to build and push your image. An important note is that the image you are building extends the Node image, meaning you don't need to install or configure Node, yarn, etc. You can simply focus on what makes your application unique.
What is an image/Dockerfile?
Without going too deep yet, think of a container image as a single package that contains everything needed to run a process. In this case, it will contain a Node environment, the backend code, and the compiled React code.
Any machine that runs a container using the image, will then be able to run the application as it was built without needing anything else pre-installed on the machine.
A
Dockerfileis a text-based script that provides the instruction set on how to build the image. For this quick start, the repository already contains the Dockerfile.
CLI
To get started, either clone or download the project as a ZIP file to your local machine.
$ git clone https://github.com/docker/getting-started-todo-app
And after the project is cloned, navigate into the new directory created by the clone:
$ cd getting-started-todo-app
Build the project by running the following command, swapping out DOCKER_USERNAME with your username.
$ docker build -t <DOCKER_USERNAME>/getting-started-todo-app .
For example, if your Docker username was mobydock, you would run the following:
$ docker build -t mobydock/getting-started-todo-app .
To verify the image exists locally, you can use the docker image ls command:
$ docker image ls
You will see output similar to the following:
REPOSITORY TAG IMAGE ID CREATED SIZE
mobydock/getting-started-todo-app latest 1543656c9290 2 minutes ago 1.12GB
...
To push the image, use the docker push command. Be sure to replace DOCKER_USERNAME with your username:
$ docker push <DOCKER_USERNAME>/getting-started-todo-app
Depending on your upload speeds, this may take a moment to push.
VS Code
Open Visual Studio Code. Ensure you have the Docker extension for VS Code installed from Extension Marketplace.

In the File menu, select Open Folder. Choose Clone Git Repository and paste this URL: https://github.com/docker/getting-started-todo-app

Right-click the Dockerfile and select the Build Image... menu item.

In the dialog that appears, enter a name of DOCKER_USERNAME/getting-started-todo-app, replacing DOCKER_USERNAME with your Docker username.
After pressing Enter, you'll see a terminal appear where the build will occur. Once it's completed, feel free to close the terminal.
Open the Docker Extension for VS Code by selecting the Docker logo in the left nav menu.
Find the image you created. It'll have a name of docker.io/DOCKER_USERNAME/getting-started-todo-app.
Expand the image to view the tags (or different versions) of the image. You should see a tag named latest, which is the default tag given to an image.
Right-click on the latest item and select the Push... option.

Press Enter to confirm and then watch as your image is pushed to Docker Hub. Depending on your upload speeds, it might take a moment to push the image.
Once the upload is finished, feel free to close the terminal.
Before you move on, take a moment and reflect on what happened here. Within a few moments, you were able to build a container image that packages your application and push it to Docker Hub.
Going forward, you’ll want to remember that:
Docker Hub is the go-to registry for finding trusted content. Docker provides a collection of trusted content, composed of Docker Official Images, Docker Verified Publishers, and Docker Sponsored Open Source Software, to use directly or as bases for your own images.
Docker Hub provides a marketplace to distribute your own applications. Anyone can create an account and distribute images. While you are publicly distributing the image you created, private repositories can ensure your images are accessible to only authorized users.
Usage of other registries
While Docker Hub is the default registry, registries are standardized and made interoperable through the Open Container Initiative. This allows companies and organizations to run their own private registries. Quite often, trusted content is mirrored (or copied) from Docker Hub into these private registries.
Now that you’ve built an image, it's time to discuss why you as a developer should learn more about Docker and how it will help you in your day-to-day tasks.