Docker deployment helps you package your Astro site and run it anywhere easily. It makes sharing and running your site consistent on any computer or server.
Docker deployment in Astro
FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build EXPOSE 3000 CMD ["npm", "start"]
This is a basic Dockerfile for an Astro project.
It uses a small Node.js image, installs dependencies, builds the site, and starts the server.
FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build EXPOSE 3000 CMD ["npm", "start"]
npm ci for clean install and runs the preview command to serve the built site.FROM node:18-alpine WORKDIR /app COPY . . RUN npm ci RUN npm run build EXPOSE 3000 CMD ["npm", "run", "preview"]
This Dockerfile sets up an Astro project to run inside a container. It installs dependencies, builds the site, and starts the preview server on port 3000.
You can build the Docker image with docker build -t astro-site . and run it with docker run -p 3000:3000 astro-site. Then open http://localhost:3000 in your browser to see your site.
FROM node:18-alpine # Set working directory WORKDIR /app # Copy package files and install dependencies COPY package.json package-lock.json ./ RUN npm install # Copy all source files COPY . . # Build the Astro site RUN npm run build # Expose port 3000 for the server EXPOSE 3000 # Start the Astro preview server CMD ["npm", "run", "preview"]
Make sure your package.json has scripts for build and preview.
Use EXPOSE to tell Docker which port your app listens on.
Use npm run preview to serve the built Astro site inside the container.
Docker deployment packages your Astro site to run anywhere easily.
Create a Dockerfile that installs dependencies, builds, and serves your site.
Build and run your Docker image to see your Astro site in a container.
Practice
Solution
Step 1: Understand Docker's role
Docker packages applications with all their dependencies to run consistently on any system.Step 2: Connect to Astro deployment
Using Docker for Astro ensures the site runs the same on any server or environment.Final Answer:
To package the site so it runs the same anywhere -> Option CQuick Check:
Docker packages apps for consistent runs [OK]
- Thinking Docker speeds up coding
- Believing Docker replaces HTML/CSS
- Assuming Docker fixes code bugs automatically
Solution
Step 1: Identify multi-stage build syntax
Multi-stage builds start with a FROM line naming a base image and stage alias.Step 2: Check options for correct FROM usage
Only 'FROM node:18 AS builder' correctly defines a build stage.Final Answer:
FROM node:18 AS builder -> Option AQuick Check:
Multi-stage build starts with FROM ... AS ... [OK]
- Using RUN or COPY as first line
- Missing AS keyword for stage naming
- Confusing CMD with build stage start
FROM node:18 AS builder WORKDIR /app COPY package.json . RUN npm install COPY . . RUN npm run build FROM nginx:alpine COPY --from=builder /app/dist /usr/share/nginx/html
Solution
Step 1: Analyze multi-stage build copying
The final stage copies only the built /app/dist folder, not node_modules or source files.Step 2: Understand image size impact
Copying only built static files keeps the final image small and efficient.Final Answer:
The final image is small because only built files are copied -> Option BQuick Check:
Multi-stage builds reduce image size by copying only needed files [OK]
- Assuming node_modules is copied to final image
- Thinking source code is included in final image
- Believing syntax error exists in given Dockerfile
FROM node:18 WORKDIR /app COPY package.json . RUN npm install COPY . . RUN npm run build CMD ["npm", "start"]What is the likely cause?
Solution
Step 1: Review CMD usage
CMD syntax with brackets is correct JSON array form, so no syntax error here.Step 2: Understand npm start role
npm start often runs a dev server, not a production static server; built files may not be served properly.Final Answer:
npm start may not serve built files; need a server like nginx -> Option AQuick Check:
npm start may not serve production build [OK]
- Thinking CMD syntax is wrong
- Assuming COPY order causes build error
- Ignoring need for static file server in production
Solution
Step 1: Identify correct ARG usage
ARG must be declared before RUN to use in that command; FROM node:18 AS builder WORKDIR /app COPY package.json . ARG NODE_ENV=production RUN npm install --only=$NODE_ENV COPY . . RUN npm run build FROM nginx:alpine COPY --from=builder /app/dist /usr/share/nginx/html declares ARG before RUN.Step 2: Check multi-stage build correctness
FROM node:18 AS builder WORKDIR /app COPY package.json . ARG NODE_ENV=production RUN npm install --only=$NODE_ENV COPY . . RUN npm run build FROM nginx:alpine COPY --from=builder /app/dist /usr/share/nginx/html uses multi-stage build with builder stage and copies only built files to nginx stage.Step 3: Verify environment variable usage
Using ARG NODE_ENV=production and npm install --only=$NODE_ENV correctly installs production dependencies only during build.Final Answer:
ARG NODE_ENV=production declared after COPY package.json . before RUN npm install --only=$NODE_ENV -> Option DQuick Check:
ARG after COPY package.json before RUN $NODE_ENV (optimal layer caching) [OK]
- Declaring ARG after RUN command
- Setting ENV instead of ARG for build-time variables
- Not using multi-stage build causing large images
Ready to prove it?
Pass the test and this pattern moves unknown
