appendix B. Bootstrapping Microservices cheat sheet
This appendix summarizes the most useful commands you learned throughout this book.
B.1 Node.js commands
See chapter 2 to learn how to install and use Node.js to create microservices.
Table B.1 Node.js commands (continued)
Command
Description
node --version
Checks that Node.js is installed and prints the version number.
npm init -y
Creates a default Node.js project. This creates a stub for our package .json, the file that tracks metadata and dependencies for our Node.js project.
npm install --save
➥<package-name>
Installs an npm package. There are many other packages available on npm. You can install any by inserting a specific package name.
npm install
Installs all dependencies for a Node.js project. This includes all the packages that were previously recorded in package.json.
node <script-file>
Runs a Node.js script file. We just invoke the node command and give it the name of our script file as an argument. You can call your script main.js or server.js if you want, but it’s probably best to stick to the convention and just call it index.js.
npm start
The conventional npm script for starting a Node.js application, regardless of what name the main script file has or what command-line parameters it expects.
Typically this just translates into node index.js in the package.json file, but it all depends on the author of the project and how they have set it up.
The nice thing is that no matter how a particular project is structured, you only have to remember npm start.
npm run start:dev
My personal convention for starting a Node.js project in development. I add this to the scripts in package.json, and typically, it runs something like nodemon to enable live reload of your code as you work with it.
B.2 Docker commands
See chapter 3 to learn how to package, publish, and run microservices using Docker.
Table B.2 Docker commands (continued)
Command
Description
docker --version
Checks that Docker is installed and prints the version number.
docker container list
Lists running containers.
docker ps
Lists all containers (running and stopped).
docker image list
Lists local images.
docker build -t <tag> --file
➥<docker-file> .
Builds an image from assets in the current directory according to the instructions in docker-file. The -t argument tags the image with a name you specify.
docker run -d -p
➥<host-port>:<container-port>
➥<tag>
Instantiates a container from an image. If the image isn’t available locally, you can pull it from a remote registry (assuming the tag specifies the URL of the registry).
The -d argument runs the container in detached mode; it won’t be bound to the terminal, and you won’t see the output. Omit this argument to see output directly, but note that this also locks up your terminal.
The -p argument allows you to bind a port on the host to a port in the container.
docker logs <container-id>
Retrieves output from a particular container. You need to issue this command to see the output when running a container in detached mode.
docker login <url>
➥--username <username>
➥--password <password>
Authenticates with your private Docker registry so that you can run other commands against it.
docker tag <existing-tag>
➥<new-tag>
Adds a new tag to an existing image. To push an image to your private container registry, you must first tag it with the URL of your registry.
docker push <tag>
Pushes an appropriately tagged image to your private Docker registry. The image should be tagged with the URL of your registry.
docker kill <container-id>
Stops a particular container locally.
docker rm <container-id>
Removes a particular container locally (it must be stopped first).
docker rmi <image-id>
➥--force
Removes a particular image locally (any containers must be removed first). The --force argument removes images even when these have been tagged multiple times.
B.4 Docker Compose commands
See chapters 4 and 5 to learn how to use Docker Compose to simulate a microservices application on your development workstation (or personal computer) for development and testing.
Table B.3 Docker Compose commands
Command
Description
docker-compose --version
Checks that Docker Compose is installed and prints the version number.
docker-compose up --build
Builds and instantiates an application composed of multiple containers as defined by the Docker Compose file (docker-compose .yaml) in the current working directory.
docker-compose ps
Lists running containers that are part of the application specified by the Docker Compose file.
docker-compose stop
Stops all containers in the application but persists the stopped containers for inspection.
docker-compose down
Stops and destroys the application, leaving the development workstation in a clean state.
Terraform commands
See chapters 6 and 7 to learn how to implement infrastructure as code via Terraform to create a Kubernetes cluster and deploy your microservices to it.
Table B.4 Terraform commands
Command
Description
terraform init
Initializes a Terraform project and downloads the provider plugins.
terraform apply
➥-auto-approve
Executes Terraform code files in the working directory to incrementally apply changes to your infrastructure.
terraform destroy
Destroys all infrastructure that was created by the Terraform project.
B.5 Testing commands
See chapter 8 to learn about automated testing for microservices with Jest and Cypress.
Table B.5 Testing commands
Command
Description
npx jest --init
Initializes the Jest configuration file.
npx jest
Runs tests under Jest
npx jest --watch
Runs tests with live reload enabled to rerun tests when code has changed. Jest uses Git to know which files have changed and should be considered.
npx jest --watchAll
As previously, except it monitors all files for changes and not just those that are reported changed by Git.
npx cypress open
Opens the Cypress UI so that you can run tests. Live reload works out of the box. You can update your code, and the tests rerun automatically.
npx cypress run
Executes Cypress tests with Cypress running in headless mode. This lets you test with Cypress from the command line (or CD pipeline) without having to display the UI.
npm test
The npm script convention for running tests. This can run Jest or Cypress (or even both) depending on how you configured your package.json file.
This is the command you should run in your CD pipeline to execute your test suite.
npm run test:watch
This is my personal convention for running tests in live reload mode. You need to configure this script in your package.json file to use it.