CMD ["/myapp"] # View exposed ports from image docker image inspect myapp --format='json .Config.ExposedPorts' View actual published ports docker ps --format "table .Names\t.Ports" Detailed port mapping docker port mycontainer 7. Security & Best Practices # ✅ GOOD: Minimal exposure FROM node:18-alpine EXPOSE 3000 # Only what's necessary ❌ BAD: Over-exposing FROM node:18-alpine EXPOSE 1-65535 # Don't do this ✅ GOOD: Use environment variables FROM python:3.11 ENV APP_PORT=8000 EXPOSE $APP_PORT # Variable expansion works! ✅ GOOD: Document protocol EXPOSE 8000/tcp EXPOSE 53/udp 8. Advanced: HEALTHCHECK Integration FROM nginx:alpine EXPOSE 80 443 Health check using exposed ports HEALTHCHECK --interval=30s --timeout=3s CMD curl --fail http://localhost:80 || exit 1
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
COPY package*.json ./ RUN npm install