In users/package.json add the following line to the scripts section:
"docker": "node --experimental-modules ./user-server",
"docker-build": "docker build -t node-web-development/userauth ."
Previously, we've put the configuration environment variables into package.json. In this case, the configuration environment variables are in the Dockerfile. This means we need a way to run the server with no environment variables other than those in the Dockerfile. With this scriptsentry, we can do npm run docker and then the Dockerfile environment variables will supply all configuration.
We can build the authentication service as follows:
$ npm run docker-build
> user-auth-server@0.0.1 docker-build /Users/david/chap10/users
> docker build -t node-web-development/userauth .
Sending build context to Docker daemon 33.8MB
Step 1/11 : FROM node:9.5
---> a696309517c6
Step 2/11 : ENV DEBUG="users:*"
---> Using cache
---> f8cc103432e8
Step 3/11 : ENV PORT="3333"
---> Using cache
---> 39b24b8b554e
... more output
The docker build command builds a container from a Dockerfile. As we said earlier, the process begins with the image defined in the FROM command. Then the build proceeds step by step, and the output shows literally each step as it is executed.
Then create a script, authnet/startserver.sh, or on Windows call it startserver.ps1, containing the following command:
docker run -it --name userauth --net=authnet node-web-development/userauthThis launches the newly built container, giving it the name userauth, attaching it to authnet:
$ sh -x startserver.sh
+ docker run -it --name userauth --net=authnet node-web-development/userauth
> user-auth-server@0.0.1 docker /userauth
> node --experimental-modules ./user-server
(node:17) ExperimentalWarning: The ESM module loader is experimental.
users:service User-Auth-Service listening at http://0.0.0.0:3333 +0ms
That starts the user authentication service. On Windows, start it as .\startserver.ps1. You should recall that it's a REST service, and therefore running it through its paces is done with users-add.js and the other scripts. But, since we did not expose a public port from the service we must run those scripts from inside the container.
We determine whether a container exposes a public port in one of two ways. The easiest is running docker ps -a and viewing the container listing details. There is a column marked PORTS, and for userauth we see 3333/tcp. This is a side effect of the EXPOSE command in the Dockerfile. If that port were exposed, it would appear in the PORTS column as 0.0.0.0:3333->3333/tcp. Remember the goal for the userauth container, and authnet overall, was that it would not be publicly accessible because of security concerns.