Given the following Dockerfile for a Node.js microservice, what will be the output when running docker build -t myservice .?
FROM node:18-alpine WORKDIR /app COPY package.json . RUN npm install COPY . . CMD ["node", "server.js"]
Check if the Dockerfile copies package.json before running npm install.
The Dockerfile copies package.json before running npm install, so dependencies install correctly. The build completes successfully with the image tagged 'myservice'.
Choose the Dockerfile snippet that correctly sets environment variables PORT to 8080 and NODE_ENV to production.
Look for the correct syntax to set multiple environment variables in one ENV instruction.
The correct syntax uses ENV VAR=value pairs separated by spaces. Option C correctly sets both variables.
Consider this Dockerfile for a Python microservice:
FROM python:3.12 WORKDIR /app COPY . . RUN pip install -r requirements.txt CMD ["python", "app.py"]
Why might this Docker image be larger than necessary?
Think about what files get included in the image when using COPY . ..
Copying the entire context includes unnecessary files like tests and documentation, increasing image size. Using a .dockerignore file can exclude these.
Arrange these Dockerfile instructions in the best order to optimize Docker layer caching for a Node.js microservice:
Think about which files change most often and how Docker caches layers.
Copying package.json and running npm install first allows Docker to cache dependencies. Copying the rest of the code later avoids reinstalling packages unnecessarily.
Which option shows the best Dockerfile practice to improve security by avoiding running the container as root?
Consider creating a non-root user and switching to it.
Option B creates a new user and switches to it, reducing security risks. Option B uses an existing user but may not exist in all images. Option B uses a numeric ID without context. Option B keeps root user with insecure permissions.