Package and deploy

After testing, you are ready to package your application and deploy to Akka Serverless.

I. Create a Docker container

  1. Create a .dockerignore file in the shoppingcart.

  2. Add the following lines to the .dockerignore file to make sure the docker daemon will not copy these to your container

    node_modules
    user-function.desc
  3. Create a Dockerfile with the following content:

    # This Dockerfile uses multi-stage build process.
    # See https://docs.docker.com/develop/develop-images/multistage-build/
    
    # Stage 1: Downloading dependencies and building the application
    FROM node:12.18.0-buster AS builder
    
    # Set the working directory
    WORKDIR /home/node
    
    # Install app dependencies
    COPY package*.json ./
    RUN npm ci
    
    # Copy sources and build the app
    COPY . .
    RUN npm run build
    
    # Remove dev packages
    # (the rest will be copied to the production image at stage 2)
    RUN npm prune --production
    
    # Stage 2: Building the production image
    FROM node:12.18.0-buster-slim
    
    # Set the working directory
    WORKDIR /home/node
    
    # Copy dependencies
    COPY --from=builder --chown=node /home/node/node_modules node_modules/
    
    # Copy the app
    COPY --from=builder --chown=node \
      /home/node/package*.json \
      /home/node/*.js \
      /home/node/*.proto \
      /home/node/user-function.desc \
      ./
    
    # Run the app as an unprivileged user for extra security.
    USER node
    
    # Run
    EXPOSE 8080
    CMD ["npm", "start"]
    This Dockerfile will execute a two-step build, minimizing the size of the container that needs to be pushed to your Docker registry.
  4. Build a new docker container, by running the following command:

    docker build . -t <your docker username>/shoppingcart:tutorial
  5. Push your Docker container to your Docker registry

    docker push <your docker username>/shoppingcart:tutorial
  6. Deploy your service to Akka Serverless

    akkasls svc deploy shoppingcart <your docker registry>/<your docker username>/shoppingcart:tutorial
  7. Validate your deployment to make sure your service is Ready

    $ akkasls svc list
    NAME            AGE     REPLICAS   STATUS             DESCRIPTION
    shoppingcart    60s     1          Ready

II. Expose your service over HTTP

  1. Expose the shoppingcart service, with CORS header support to allow connections across domain, with the following command:

    $ akkasls svc expose shoppingcart --enable-cors
    Service 'shoppingcart' was successfully exposed at: snowy-truth-4046.us-east1.apps.cloudstate.com
  2. Copy the URL from the previous step and navigate to the web UI hosted by Lightbend: https://static.cloudstate.com/js-shopping-cart/index.html. When prompted, enter the URL from the previous step and choose a name to persist your cart.

  3. When you’ve added a few items to your cart, open a new terminal window to use cURL to see the contents of your cart:

    $ curl --request GET --url https://<URL from step 1>.com/com.example.shoppingcart.ShoppingCart/carts/<your name from step 2>
    
    {"items":[{"productId":"sg-2","name":"Shield Sunglass","quantity":1},{"productId":"sg-4","name":"Pilot Sunglass","quantity":1},{"productId":"sg-1","name":"Sonar Sunglass","quantity":1}]}

Congratulations! You’ve implemented a Cloudstate service and deployed it to Akka Serverless.

What you’ve learned

In this tutorial you’ve learned:

  • How to create a new Node.js project with the dependencies for your Cloudstate project

  • How to create a .proto file and what needs to be in it

  • How to implement the service from the .proto file in your code

  • How to create unit tests to validate your implementation

  • How to build a container and deploy it to Akka Serverless

  • How to expose your service over HTTP and test it