🔑 Key points
- Build a Docker container image for the JWT Pizza Service.
- Configure networking to allow the container to communicate with a database on the host machine.
Now that you understand the basics of Docker, you will create a container image for the jwt-pizza-service using its source code. Follow these steps to build and run your container.
-
Navigate to your project: Open your terminal and navigate to the directory containing your fork of the
jwt-pizza-servicerepository. -
Create the Dockerfile: Create a file named
Dockerfile(case-sensitive) in the project root with the following content:ARG NODE_VERSION=22 FROM node:${NODE_VERSION}-alpine WORKDIR /usr/src/app COPY . . RUN npm ci EXPOSE 80 CMD ["node", "index.js", "80"]
-
Configure the database host: Modify or create the
src/config.jsfile. To allow the code inside the container to connect to the MySQL server running on your host machine, set the database host tohost.docker.internal.[!WARNING] Add
config.jsto your.gitignorefile to ensure you do not accidentally push sensitive credentials to your repository.Update the parameters (user, password, jwtSecret, and factory.apiKey) to match your environment:
module.exports = { jwtSecret: 'yourRandomJWTGenerationSecretForAuth', db: { connection: { //host: '127.0.0.1', host: 'host.docker.internal', user: 'root', password: 'yourDatabasePassword', database: 'pizza', connectTimeout: 60000, }, listPerPage: 10, }, factory: { url: 'https://pizza-factory.cs329.click', apiKey: 'yourHeadquartersProvidedApiKey', }, };
-
Prepare the distribution directory: Create a
distdirectory and copy the necessary files into it. This simulates a clean build environment.mkdir dist cp Dockerfile dist cp -r src/* dist cp *.json dist
-
Navigate to the distribution directory:
cd dist -
Build the image: Use the
docker buildcommand to create an image namedjwt-pizza-service.docker build -t jwt-pizza-service . -
Verify the image: Check that the image was successfully created and is listed in your local registry.
docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE jwt-pizza-service latest 9689e2852c3a 2 seconds ago 132MB
-
Run the container: Start the container in the background.
- The
-p 80:80parameter maps the host's port 80 to the container's port 80. - The
-dparameter runs the container in "detached" mode (background). - The
--nameparameter assigns a friendly name to the container instance.
docker run -d --name jwt-pizza-service -p 80:80 jwt-pizza-service
Verify the service is responding by using
curlto hit the endpoints:curl localhost:80 curl localhost:80/api/order/menu
If successful, you will receive the service welcome response and the pizza menu.
- The
-
Clean up: Stop and remove the container when you are finished testing.
docker rm -fv jwt-pizza-service
Create a JWT Pizza Service container by following the instructions above. Your workflow should include:
- Creating the
Dockerfile. - Setting up the
distdirectory. - Modifying
config.jsto allow external database access. - Building the container image.
- Running the container and verifying connectivity.
- Shutting down the container.
Once complete, your console history should reflect the build, list, and run commands.
{"id":"126355a7-4172-487f-b7e2-04447d8614d3", "title":"JWT Pizza container", "type":"file-submission", "gradingCriteria":"Successful docker run command for jwt-pizza-service" }
Upload a screenshot of your working container.
Important
After completing this exercise, remember to revert the host in your src/config.js file back to 127.0.0.1. This ensures that the service can still connect to your MySQL database when running natively (outside of Docker).
db: {
connection: {
host: '127.0.0.1',
//host: 'host.docker.internal',
...
},
},