Skip to content

Latest commit

 

History

History
97 lines (77 loc) · 3.16 KB

jwtPizzaServiceContainer.md

File metadata and controls

97 lines (77 loc) · 3.16 KB

JWT Pizza Service Container

🔑 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.

  1. In your development environment, open your command console and navigate to the directory containing your fork of jwt-pizza-service.

  2. 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"]
  3. 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 of host.docker.internal. Make sure you include the config.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',
     },
    };
  4. Copy the source code files to dist that we want to distribute.

    mkdir dist
    cp Dockerfile dist
    cp -r src/* dist
    cp *.json dist
  5. Navigate to the dist directory.

    cd dist
  6. Build the image.

    docker build -t jwt-pizza-service .
  7. Verify that the container exists.

    docker images -a
    
    REPOSITORY         TAG      IMAGE ID       CREATED         SIZE
    jwt-pizza-service  latest   9689e2852c3a   2 seconds ago   132MB
  8. 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.

  9. 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

☑ Exercise

Create a JWT Pizza Service container using the instructions given above. This includes the following steps:

  1. Create the Dockerfile.
  2. Create a distribution directory.
  3. Modify your config.json file so that the container can access your external database.
  4. Build the container image.
  5. Run the container.
  6. Shutdown the container.

Once you are done, your console window should show the build, list, and run commands.

Docker successful run