Published July 22, 2024
by Fernando Ibanez
Learn how Docker can streamline your frontend development workflow
Docker has revolutionized how we develop, deploy, and distribute applications. As a frontend developer, understanding Docker can significantly improve your workflow.
Docker is a containerization platform that packages applications and their dependencies into lightweight, portable containers.
No more "it works on my machine" issues:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
New team members can get started quickly:
docker-compose up
Development environment matches production exactly.
Here's a optimized Dockerfile for React applications:
# Multi-stage build for production
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production stage
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Orchestrate multiple services:
version: '3.8'
services:
frontend:
build: .
ports:
- '3000:3000'
volumes:
- .:/app
- /app/node_modules
environment:
- REACT_APP_API_URL=http://localhost:5000
api:
image: node:18-alpine
working_dir: /api
ports:
- '5000:5000'
volumes:
- ./api:/api
command: npm start
database:
image: postgres:13
environment:
POSTGRES_DB: myapp
POSTGRES_USER: user
POSTGRES_PASSWORD: password
ports:
- '5432:5432'
Enable hot reloading for development:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
Mount source code for live updates:
docker run -v $(pwd):/app -p 3000:3000 my-react-app
Docker transforms frontend development by providing consistent environments, easy deployment, and better collaboration. Start with simple containers and gradually adopt more advanced patterns.