Building Custom Docker Images with a Dockerfile

Building Custom Docker Images

A Dockerfile defines the steps to build a custom Docker image for your application.

Dockerfile Basics

# Start from a base image
FROM ubuntu:22.04

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive

# Run commands during build
RUN apt update && apt install -y nginx && apt clean

# Copy files from host into image
COPY ./html /var/www/html

# Set the working directory
WORKDIR /app

# Expose a port (documentation only)
EXPOSE 80

# Command to run when container starts
CMD ["nginx", "-g", "daemon off;"]

Build the Image

docker build -t myapp:1.0 .
docker build -t myapp:latest -f Dockerfile.prod .

Run Your Custom Image

docker run -d -p 80:80 myapp:1.0

Multi-Stage Builds (Smaller Production Images)

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

The final image contains only Nginx + the built files, not Node.js or build tools.

.dockerignore File

Like .gitignore but for Docker builds. Prevents large or sensitive files from being copied into the image:

node_modules
.git
*.log
.env

Push to Docker Hub

docker login
docker tag myapp:1.0 yourusername/myapp:1.0
docker push yourusername/myapp:1.0
  • 0 Bu dökümanı faydalı bulan kullanıcılar:
Bu cevap yeterince yardımcı oldu mu?

İlgili diğer dökümanlar

Installing Docker

Installing Docker on Your Dedicated Server Docker allows you to run applications in isolated...

Getting Started with Docker Compose

Getting Started with Docker Compose Docker Compose lets you define and run multi-container...

Managing Docker Containers

Managing Docker Containers A practical reference for day-to-day Docker container management. Run...

Using Docker Volumes for Persistent Storage

Using Docker Volumes for Persistent Storage By default, data inside a container is lost when the...

Docker Networking Explained

Docker Networking Explained Understanding Docker networking lets you connect containers together,...