language/nodejs: use "console" for shell examples

This allows for easier copying of the commands, without selecting the
prompt.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-08-06 17:08:09 +02:00
parent 38b629bcb2
commit 60bb48a6a1
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 45 additions and 47 deletions

View File

@ -28,7 +28,7 @@ To complete this tutorial, you need the following:
Lets create a simple Node.js application that we can use as our example. Create a directory in your local machine named `node-docker` and follow the steps below to create a simple REST API.
```shell
```console
$ cd [path to your node-docker directory]
$ npm init -y
$ npm install ronin-server ronin-mocks
@ -55,13 +55,13 @@ The mocking server is called `Ronin.js` and will listen on port 8000 by default.
Lets start our application and make sure its running properly. Open your terminal and navigate to your working directory you created.
```shell
```console
$ node server.js
```
To test that the application is working properly, well first POST some JSON to the API and then make a GET request to see that the data has been saved. Open a new terminal and run the following curl commands:
```shell
```console
$ curl --request POST \
--url http://localhost:8000/test \
--header 'content-type: application/json' \
@ -199,11 +199,9 @@ The build command optionally takes a `--tag` flag. The tag is used to set the na
Lets build our first Docker image.
```shell
```console
$ docker build --tag node-docker .
```
```shell
[+] Building 93.8s (11/11) FINISHED
=> [internal] load build definition from dockerfile 0.1s
=> => transferring dockerfile: 617B 0.0s
@ -221,7 +219,7 @@ To see a list of images we have on our local machine, we have two options. One i
To list images, simply run the `images` command.
```shell
```console
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
node-docker latest 3809733582bc About a minute ago 945MB
@ -237,7 +235,7 @@ An image is made up of a manifest and a list of layers. In simple terms, a “ta
To create a new tag for the image we built above, run the following command.
```shell
```console
$ docker tag node-docker:latest node-docker:v1.0.0
```
@ -256,14 +254,14 @@ You can see that we have two images that start with `node-docker`. We know they
Lets remove the tag that we just created. To do this, well use the rmi command. The rmi command stands for “remove image”.
```shell
```console
$ docker rmi node-docker:v1.0.0
Untagged: node-docker:v1.0.0
```
Notice that the response from Docker tells us that the image has not been removed but only “untagged”. Verify this by running the images command.
```shell
```console
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
node-docker latest 3809733582bc 32 minutes ago 945MB

View File

@ -187,9 +187,9 @@ on:
This ensures that the main CI will only trigger if we tag our commits with `V.n.n.n.` Lets test this. For example, run the following command:
```bash
git tag -a v1.0.2
git push origin v1.0.2
```console
$ git tag -a v1.0.2
$ git push origin v1.0.2
```
Now, go to GitHub and check your Actions

View File

@ -26,20 +26,20 @@ Before we run MongoDB in a container, we want to create a couple of volumes that
Lets create our volumes now. Well create one for the data and one for configuration of MongoDB.
```shell
```console
$ docker volume create mongodb
$ docker volume create mongodb_config
```
Now well create a network that our application and database will use to talk with each other. The network is called a user-defined bridge network and gives us a nice DNS lookup service which we can use when creating our connection string.
```shell
```console
$ docker network create mongodb
```
Now we can run MongoDB in a container and attach to the volumes and network we created above. Docker will pull the image from Hub and run it for you locally.
```shell
```console
$ docker run -it --rm -d -v mongodb:/data/db \
-v mongodb_config:/data/configdb -p 27017:27017 \
--network mongodb \
@ -64,19 +64,19 @@ Weve add the `ronin-database` module and we updated the code to connect to th
First lets add the `ronin-database` module to our application using npm.
```shell
```console
$ npm install ronin-database
```
Now we can build our image.
```shell
```console
$ docker build --tag node-docker .
```
Now, lets run our container. But this time well need to set the `CONNECTIONSTRING` environment variable so our application knows what connection string to use to access the database. Well do this right in the `docker run` command.
```shell
```console
$ docker run \
-it --rm -d \
--network mongodb \
@ -88,7 +88,7 @@ $ docker run \
Lets test that our application is connected to the database and is able to add a note.
```shell
```console
$ curl --request POST \
--url http://localhost:8000/notes \
--header 'content-type: application/json' \
@ -162,7 +162,7 @@ $ npm install nodemon
Lets start our application and confirm that it is running properly.
```shell
```console
$ docker-compose -f docker-compose.dev.yml up --build
```
@ -174,7 +174,7 @@ If all goes will you should see something similar:
Now lets test our API endpoint. Run the following curl command:
```shell
```console
$ curl --request GET --url http://localhost:8000/notes
```
@ -212,7 +212,7 @@ If you take a look at the terminal where our Compose application is running, you
Navigate back to the Chrome DevTools and set a breakpoint on the line containing the `return res.json({ "foo": "bar" })` statement, and then run the following curl command to trigger the breakpoint.
```shell
```console
$ curl --request GET --url http://localhost:8000/foo
```

View File

@ -20,7 +20,7 @@ A container is a normal operating system process except that this process is iso
To run an image inside of a container, we use the `docker run` command. The `docker run` command requires one parameter and that is the image name. Lets start our image and make sure it is running correctly. Execute the following command in your terminal.
```shell
```console
$ docker run node-docker
```
@ -28,7 +28,7 @@ When you run this command, youll notice that you were not returned to the com
Lets open a new terminal then make a GET request to the server using the curl command.
```shell
```console
$ curl --request POST \
--url http://localhost:8000/test \
--header 'content-type: application/json' \
@ -46,13 +46,13 @@ To publish a port for our container, well use the `--publish` flag (`-p` for
Start the container and expose port 8000 to port 8000 on the host.
```shell
```console
$ docker run --publish 8000:8000 node-docker
```
Now lets rerun the curl command from above. Remember to open a new terminal.
```shell
```console
$ curl --request POST \
--url http://localhost:8000/test \
--header 'content-type: application/json' \
@ -70,7 +70,7 @@ Press ctrl-c to stop the container.
This is great so far, but our sample application is a web server and we should not have to have our terminal connected to the container. Docker can run your container in detached mode or in the background. To do this, we can use the `--detach` or `-d` for short. Docker will start your container the same as before but this time will “detach” from the container and return you to the terminal prompt.
```shell
```console
$ docker run -d -p 8000:8000 node-docker
ce02b3179f0f10085db9edfccd731101868f58631bdf918ca490ff6fd223a93b
```
@ -79,7 +79,7 @@ Docker started our container in the background and printed the Container ID on t
Again, lets make sure that our container is running properly. Run the same curl command from above.
```shell
```console
$ curl --request POST \
--url http://localhost:8000/test \
--header 'content-type: application/json' \
@ -103,14 +103,14 @@ The `ps` command tells a bunch of stuff about our running containers. We can see
You are probably wondering where the name of our container is coming from. Since we didnt provide a name for the container when we started it, Docker generated a random name. Well fix this in a minute but first we need to stop the container. To stop the container, run the `docker stop` command which does just that, stops the container. You will need to pass the name of the container or you can use the container id.
```shell
```console
$ docker stop wonderful_kalam
wonderful_kalam
```
Now rerun the `docker ps` command to see a list of running containers.
```shell
```console
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
```
@ -119,7 +119,7 @@ CONTAINER ID IMAGE COMMAND CREATED
Docker containers can be started, stopped and restarted. When we stop a container, it is not removed but the status is changed to stopped and the process inside of the container is stopped. When we ran the `docker ps` command, the default output is to only show running containers. If we pass the `--all` or `-a` for short, we will see all containers on our system whether they are stopped or started.
```shell
```console
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce02b3179f0f node-docker "docker-entrypoint.s…" 16 minutes ago Exited (0) 5 minutes ago wonderful_kalam
@ -131,13 +131,13 @@ If youve been following along, you should see several containers listed. Thes
Lets restart the container that we just stopped. Locate the name of the container we just stopped and replace the name of the container below in the restart command.
```shell
```console
$ docker restart wonderful_kalam
```
Now, list all the containers again using the ps command.
```shell
```console
$ docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce02b3179f0f node-docker "docker-entrypoint.s…" 19 minutes ago Up 8 seconds 0.0.0.0:8000->8000/tcp wonderful_kalam
@ -151,14 +151,14 @@ Lets stop and remove all of our containers and take a look at fixing the rand
Stop the container we just started. Find the name of your running container and replace the name in the command below with the name of the container on your system.
```shell
```console
$ docker stop wonderful_kalam
wonderful_kalam
```
Now that all of our containers are stopped, lets remove them. When a container is removed, it is no longer running nor is it in the stopped status. However, the process inside the container has been stopped and the metadata for the container has been removed.
```shell
```console
$ docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce02b3179f0f node-docker "docker-entrypoint.s…" 19 minutes ago Up 8 seconds 0.0.0.0:8000->8000/tcp wonderful_kalam
@ -170,7 +170,7 @@ To remove a container, simple run the `docker rm` command passing the container
Again, make sure you replace the containers names in the below command with the container names from your system.
```shell
```console
$ docker rm wonderful_kalam agitated_moser goofy_khayyam
wonderful_kalam
agitated_moser
@ -183,7 +183,7 @@ Now lets address the pesky random name issue. Standard practice is to name yo
To name a container, we just need to pass the `--name` flag to the run command.
```shell
```console
$ docker run -d -p 8000:8000 --name rest-server node-docker
1aa5d46418a68705c81782a58456a4ccdb56a309cb5e6bd399478d01eaa5cdda
$ docker ps

View File

@ -18,7 +18,7 @@ Testing is an essential part of modern software development. Testing can mean a
Let's define a Mocha test in a `./test` directory within our application.
```shell
```console
$ mkdir -p test
```
@ -39,13 +39,13 @@ describe('Array', function() {
Lets build our Docker image and confirm everything is running properly. Run the following command to build and run your Docker image in a container.
```shell
```console
$ docker-compose -f docker-compose.dev.yml up --build
```
Now lets test our application by POSTing a JSON payload and then make an HTTP GET request to make sure our JSON was saved correctly.
```shell
```console
$ curl --request POST \
--url http://localhost:8000/test \
--header 'content-type: application/json' \
@ -56,7 +56,7 @@ $ curl --request POST \
Now, perform a GET request to the same endpoint to make sure our JSON payload was saved and retrieved correctly. The “id” and “createDate” will be different for you.
```shell
```console
$ curl http://localhost:8000/test
{"code":"success","payload":[{"msg":"testing","id":"e88acedb-203d-4a7d-8269-1df6c1377512","createDate":"2020-10-11T23:21:16.378Z"}]}
@ -66,7 +66,7 @@ $ curl http://localhost:8000/test
Run the following command to install Mocha and add it to the developer dependencies:
```shell
```console
$ npm install --save-dev mocha
```
@ -87,7 +87,7 @@ Okay, now that we know our application is running properly, lets try and run
Below is the Docker command to start the container and run tests:
```shell
```console
$ docker-compose -f docker-compose.dev.yml run notes npm run test
Creating node-docker_notes_run ...
@ -133,7 +133,7 @@ We first add a label `as base` to the `FROM node:14.15.4` statement. This allows
Now lets rebuild our image and run our tests. We will run the same docker build command as above but this time we will add the `--target test` flag so that we specifically run the test build stage.
```shell
```console
$ docker build -t node-docker --target test .
[+] Building 66.5s (12/12) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
@ -151,8 +151,8 @@ $ docker build -t node-docker --target test .
Now that our test image is built, we can run it in a container and see if our tests pass.
```shell
docker run -it --rm -p 8000:8000 node-docker
```console
$ docker run -it --rm -p 8000:8000 node-docker
> node-docker@1.0.0 test /code
> mocha ./**/*.js