Bird
Raised Fist0
Expressframework~20 mins

Docker containerization in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Docker Mastery for Express
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when you run this Dockerfile?
Consider this Dockerfile for an Express app. What will be the output when you build and run the container?
Express
FROM node:18-alpine
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
AThe container starts and the Express app listens on port 3000 inside the container.
BThe container builds but crashes immediately because server.js is missing.
CThe container starts but the app listens on port 80 by default.
DThe container fails to build because the WORKDIR is missing a slash.
Attempts:
2 left
💡 Hint
Check the EXPOSE and CMD instructions and what they do.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Dockerfile snippet
Which option correctly identifies the syntax error in this Dockerfile snippet?
Express
FROM node:18
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [node server.js]
ACOPY . . is invalid because it needs a destination folder.
BRUN npm install is invalid because it should be RUN npm install -g.
CEXPOSE 3000 is invalid because it must be EXPOSE "3000".
DCMD [node server.js] is invalid because CMD requires an array or a string with exec form.
Attempts:
2 left
💡 Hint
Look at the CMD instruction format.
🔧 Debug
advanced
2:00remaining
Why does the container fail to connect to the Express app?
You built and ran this Docker container, but your browser cannot connect to the Express app on localhost:3000. What is the most likely cause?
Express
Docker run command: docker run -d -p 3000:3000 my-express-app
Express app listens on port 3000
Dockerfile exposes port 3000
AThe container port 3000 is mapped to host port 80, so you must use localhost:80.
BThe Dockerfile EXPOSE command is missing, so port 3000 is not open.
CThe Express app is listening only on localhost (127.0.0.1) inside the container, so external connections fail.
DThe docker run command is missing the -it flag, so the app does not start.
Attempts:
2 left
💡 Hint
Check the network binding of the Express app inside the container.
❓ state_output
advanced
2:00remaining
What is the value of NODE_ENV inside the container?
Given this Dockerfile snippet, what will be the value of NODE_ENV inside the running container?
Express
FROM node:18
ENV NODE_ENV=production
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "server.js"]
A"production"
B"development"
Cundefined
D"NODE_ENV"
Attempts:
2 left
💡 Hint
Look at the ENV instruction in Dockerfile.
🧠 Conceptual
expert
3:00remaining
Which option best explains the benefit of multi-stage builds in Docker for Express apps?
Why use multi-stage builds in Dockerfiles for Express applications?
AThey allow running multiple Express apps in one container simultaneously.
BThey reduce the final image size by separating build dependencies from runtime dependencies.
CThey enable hot-reloading of Express code inside the container without rebuilding.
DThey automatically update the Node.js version inside the container at runtime.
Attempts:
2 left
💡 Hint
Think about image size and build vs runtime needs.

Practice

(1/5)
1. What is the main purpose of using Docker with an Express app?
easy
A. To write the Express app code faster
B. To package the app and its environment so it runs the same everywhere
C. To replace the need for Node.js on the host machine
D. To automatically fix bugs in the Express app

Solution

  1. Step 1: Understand Docker's role

    Docker packages an app with all its dependencies and environment so it can run consistently on any machine.
  2. Step 2: Compare options

    Only To package the app and its environment so it runs the same everywhere describes this packaging and environment consistency purpose. Other options describe unrelated or incorrect uses.
  3. Final Answer:

    To package the app and its environment so it runs the same everywhere -> Option B
  4. Quick Check:

    Docker packages app + environment = A [OK]
Hint: Docker ensures app runs same on any machine [OK]
Common Mistakes:
  • Thinking Docker writes code
  • Believing Docker replaces Node.js installation
  • Assuming Docker fixes app bugs automatically
2. Which line correctly sets the base image in a Dockerfile for an Express app using Node.js?
easy
A. BASE node:18-alpine
B. START node:18-alpine
C. IMAGE node:18-alpine
D. FROM node:18-alpine

Solution

  1. Step 1: Recall Dockerfile syntax for base image

    The correct Dockerfile command to set the base image is 'FROM'.
  2. Step 2: Identify correct option

    Only FROM node:18-alpine uses 'FROM' correctly. Other options use invalid Dockerfile keywords.
  3. Final Answer:

    FROM node:18-alpine -> Option D
  4. Quick Check:

    Base image line starts with FROM = B [OK]
Hint: Dockerfile base image always starts with FROM [OK]
Common Mistakes:
  • Using BASE or IMAGE instead of FROM
  • Confusing Dockerfile commands with shell commands
  • Omitting the version tag in the image name
3. Given this Dockerfile snippet for an Express app:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]

What happens when you run this container?
medium
A. The app listens on port 3000 inside the container
B. The app listens on port 80 inside the container
C. The app will not start because of missing dependencies
D. The container will crash immediately

Solution

  1. Step 1: Analyze EXPOSE and CMD instructions

    EXPOSE 3000 means the container listens on port 3000. CMD runs 'node index.js' to start the app.
  2. Step 2: Check for dependency installation

    RUN npm install installs dependencies from package.json, so dependencies are present.
  3. Final Answer:

    The app listens on port 3000 inside the container -> Option A
  4. Quick Check:

    EXPOSE 3000 + CMD node index.js = D [OK]
Hint: EXPOSE shows container port; CMD starts app [OK]
Common Mistakes:
  • Assuming default port 80 without EXPOSE
  • Thinking dependencies are missing without checking RUN npm install
  • Confusing container port with host port
4. You wrote this Dockerfile for your Express app:
FROM node:18-alpine
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
CMD ["node", "app.js"]

But when you build and run the container, the app crashes with 'Cannot find module'. What is the likely problem?
medium
A. You forgot to copy package-lock.json, so dependencies are incomplete
B. You did not expose the port, so the app crashes
C. You copied only package.json, missing package-lock.json can cause inconsistent installs
D. The CMD syntax is wrong; it should be a string, not an array

Solution

  1. Step 1: Check COPY and RUN npm install steps

    Only package.json is copied before npm install. Without package-lock.json, installs may be inconsistent.
  2. Step 2: Understand impact on dependencies

    Missing package-lock.json can cause npm install to install wrong or missing modules, leading to 'Cannot find module' errors.
  3. Final Answer:

    You copied only package.json, missing package-lock.json can cause inconsistent installs -> Option C
  4. Quick Check:

    Missing package-lock.json causes install issues = C [OK]
Hint: Copy both package.json and package-lock.json before npm install [OK]
Common Mistakes:
  • Assuming missing EXPOSE causes crashes
  • Thinking CMD array syntax is invalid
  • Ignoring package-lock.json importance
5. You want to create a Docker container for your Express app that automatically restarts if it crashes and uses environment variables from a file. Which Docker feature combination should you use?
hard
A. Use Docker Compose with restart policy and env_file settings
B. Use a Dockerfile with CMD to restart and ENV to load variables
C. Use docker run with --restart flag and manually set env vars in command
D. Use Docker Swarm with secrets and volumes for env variables

Solution

  1. Step 1: Identify restart and env var management

    Docker Compose supports restart policies and loading environment variables from a file via env_file.
  2. Step 2: Compare other options

    Dockerfile CMD cannot auto-restart; docker run can restart but managing env vars manually is error-prone; Docker Swarm is more complex and for orchestration, not simple env files.
  3. Final Answer:

    Use Docker Compose with restart policy and env_file settings -> Option A
  4. Quick Check:

    Restart + env_file = Docker Compose = A [OK]
Hint: Docker Compose handles restart and env files easily [OK]
Common Mistakes:
  • Thinking Dockerfile CMD can restart container
  • Ignoring restart policies in docker run
  • Confusing Docker Swarm with simple container setup
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard