Run ratings in Docker

This module shows how you create a Docker image and run it locally.

  1. Download the Dockerfile for the ratings microservice.

    $ curl -s https://raw.githubusercontent.com/istio/istio/release-1.5/samples/bookinfo/src/ratings/Dockerfile -o Dockerfile
    
  2. Observe the Dockerfile.

    $ cat Dockerfile
    

    Note that it copies the files into the container’s filesystem and then runs the npm install command you ran in the previous module. The CMD command instructs Docker to run the ratings service on port 9080.

  3. Build a Docker image from the Dockerfile:

    $ 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
    
  4. Run ratings in Docker. The following docker run command instructs Docker to expose port 9080 of the container to port 9081 of your computer, allowing you to access the ratings microservice on port 9081.

    $ docker run -d -p 9081:9080 $USER/ratings
    
  5. Access http://localhost:9081/ratings/7 in your browser or use the following curl command:

    $ curl localhost:9081/ratings/7
    {"id":7,"ratings":{"Reviewer1":5,"Reviewer2":4}}
    
  6. Observe the running container. Run the docker ps command to list all the running containers and notice the container with the image <your user name>/ratings.

    $ 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
    ...
    
  7. Stop the running container:

    $ docker stop <the container ID from the output of docker ps>
    

You are ready to deploy the application.

Esta informação foi útil?
Você tem alguma sugestão de melhoria?

Obrigado pelo seu feedback!