2.8 KiB
title | overview | weight | owner | test |
---|---|---|---|---|
在 Docker 中运行 ratings 服务 | 在一个 Docker 容器里运行一个微服务。 | 20 | istio/wg-docs-maintainers | no |
{{< boilerplate work-in-progress >}}
本模块展示了如何创建一个 Docker 镜像并在本地运行它。
-
下载微服务
ratings
的Dockerfile
。{{< text bash >}} $ curl -s {{< github_file >}}/samples/bookinfo/src/ratings/Dockerfile -o Dockerfile {{< /text >}}
-
观察这个
Dockerfile
。{{< text bash >}} $ cat Dockerfile {{< /text >}}
请注意,它将文件复制到容器的文件系统中,然后执行您在上一个模块中执行过的
npm install
命令。CMD
命令指示 Docker 在9080
端口上运行ratings
服务。 -
创建一个环境变量来存储您的用户 ID,该用户 ID 将用于标记 docker 镜像以进行
ratings
服务。 例如,user
。{{< text bash >}} $ export USER=user {{< /text >}}
-
根据
Dockerfile
构建出一个镜像:{{< text bash >}} $ docker build -t $USER/ratings . ... Step 9/9 : CMD node /opt/microservices/ratings.js 9080 ---> Using cache ---> 77c6a304476c Successfully built 77c6a304476c Successfully tagged user/ratings:latest {{< /text >}}
-
在 Docker 中运行
ratings
服务. 接下来的 docker run 命令 指示 Docker 将容器的9080
端口暴露到计算机的9081
端口,从而允许您访问9081
端口上的ratings
微服务。{{< text bash >}} $ docker run --name my-ratings --rm -d -p 9081:9080 $USER/rating {{< /text >}}
-
在浏览器访问 http://localhost:9081/ratings/7,或使用以下的
curl
命令:{{< text bash >}} $ curl localhost:9081/ratings/7 {"id":7,"ratings":{"Reviewer1":5,"Reviewer2":4}} {{< /text >}}
-
观察运行中的容器。执行 docker ps 命令,列出所有运行中的容器,同时 注意镜像是
<your user name>/ratings
的容器。{{< text bash >}} $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 47e8c1fe6eca user/ratings "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:9081->9080/tcp elated_stonebraker ... {{< /text >}}
-
停止运行中的容器:
{{< text bash >}} $ docker stop my-ratings {{< /text >}}
现在,您已经了解了如何将单个服务打包到容器中。接下来去学习在 Kubernetes 集群上部署应用程序。