Dockerfile Expose Example -
COPY nginx.conf /etc/nginx/nginx.conf # Image EXPOSEs 3000, but we can run on different port internally docker run -p 8080:8080 -e PORT=8080 myapp Corresponding Dockerfile FROM node:18 ENV PORT=3000 # Default EXPOSE $PORT CMD ["sh", "-c", "npm start -- --port $PORT"] 10. Key Takeaways | Feature | Behavior | |---------|----------| | EXPOSE | Documentation only, no port publishing | | -p flag | Publishes port to specific host port | | -P flag | Publishes all EXPOSED ports to random ports | | Multiple ports | Can specify multiple EXPOSE lines | | TCP/UDP | Default TCP, specify /udp for UDP | | Port ranges | Supported by some runtimes (9000-9010) | Real-World Production Example FROM node:18-alpine AS frontend WORKDIR /app COPY package*.json ./ RUN npm ci --only=production COPY . . EXPOSE 3000 8080 USER node Security: non-root user, specific port range CMD ["node", "server.js"]
COPY . . EXPOSE 3000
COPY package*.json ./ RUN npm install
COPY . . EXPOSE 8000/tcp # Main web application EXPOSE 8080/tcp # Admin interface EXPOSE 5432/tcp # Internal database (documentation only) UDP port example EXPOSE 53/udp # DNS service Port range (if supported by your container runtime) EXPOSE 9000-9010/tcp dockerfile expose example
CMD ["npm", "start"] FROM python:3.11-slim WORKDIR /app COPY nginx
COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt EXPOSE 3000 8080 USER node Security: non-root user,
: EXPOSE is metadata. Always use -p or -P when running containers to actually access the ports!