🔑 Key points
- Build a container for the JWT Pizza Service.
Now that you know how Docker containers work, you need to create a jwt-pizza-service Docker container image from the Pizza Service source code. Here are the steps to take.
-
In your development environment, open your command console and navigate to the directory containing your fork of
jwt-pizza-service
. -
Create a file named
Dockerfile
in the project directory with the following content. Make sure you match the case of the filename.ARG NODE_VERSION=20.12.2 FROM node:${NODE_VERSION}-alpine WORKDIR /usr/src/app COPY . . RUN npm ci EXPOSE 80 CMD ["node", "index.js", "80"]
-
Modify/Create the
config.js
file. Set the database host field so that it looks outside the container for the MySQL server by specifying the value ofhost.docker.internal
. Make sure you include theconfig.js
file in your.gitignore
file so that you do not accidentally push it to your repository. Set the parameters, such as the user, password, jwtSecret and factory.apiKey, according to your environment.module.exports = { jwtSecret: 'yourRandomJWTGenerationSecretForAuth', db: { connection: { host: 'host.docker.internal', user: 'root', password: 'yourDatabasePassword', database: 'pizza', connectTimeout: 60000, }, listPerPage: 10, }, factory: { url: 'https://pizza-factory.cs329.click', apiKey: 'yourHeadquartersProvidedApiKey', }, };
-
Copy the source code files to
dist
that we want to distribute.mkdir dist cp Dockerfile dist cp -r src/* dist cp *.json dist
-
Navigate to the
dist
directory.cd dist
-
Build the image.
docker build -t jwt-pizza-service .
-
Verify that the container exists.
docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE jwt-pizza-service latest 9689e2852c3a 2 seconds ago 132MB
-
Run the container and make sure it works by using curl to hit the default endpoint. The
-p
parameter specifies the port mapping. The-d
parameter starts the container as a background process.--name
specifies the repository name to associate with the container.docker run -d --name jwt-pizza-service -p 80:80 jwt-pizza-service curl localhost:80
This should return the service welcome response if the container is successfully running.
-
Stop and delete the container using the container ID. You can use
docker ps -a
to find the value.docker rm -fv jwt-pizza-service
Create a JWT Pizza Service container using the instructions given above. This includes the following steps:
- Create the Dockerfile.
- Create a distribution directory.
- Modify your
config.json
file so that the container can access your external database. - Build the container image.
- Run the container.
- Shutdown the container.
Once you are done, your console window should show the build, list, and run commands.