diff --git a/docker/examples/03/.dockerignore b/docker/examples/03/.dockerignore new file mode 100644 index 0000000..29d6828 --- /dev/null +++ b/docker/examples/03/.dockerignore @@ -0,0 +1,3 @@ +node_modules +npm-debug.log + diff --git a/docker/examples/03/.env b/docker/examples/03/.env new file mode 100644 index 0000000..78d74bb --- /dev/null +++ b/docker/examples/03/.env @@ -0,0 +1 @@ +BACKGROUND_COLOR=#00ff00 diff --git a/docker/examples/03/Dockerfile b/docker/examples/03/Dockerfile new file mode 100644 index 0000000..8eb8b59 --- /dev/null +++ b/docker/examples/03/Dockerfile @@ -0,0 +1,27 @@ +# === Stage 1: Build the Node.js application === +FROM node:latest as builder + +WORKDIR /app + +# Copy the application code and .env file +COPY package.json ./ +COPY index.js ./ +COPY .env ./ + +# Install dependencies +RUN npm install --production + +# === Stage 2: Create the final lightweight image === +FROM node:alpine + +WORKDIR /app + +# Copy the Node.js application and .env file from the builder stage +COPY --from=builder /app . + +# Expose port 8000 for the web server +EXPOSE 8000 + +# Start the Node.js web server when the container starts +CMD ["node", "index.js"] + diff --git a/docker/examples/03/index.html b/docker/examples/03/index.html new file mode 100644 index 0000000..fd0bc4c --- /dev/null +++ b/docker/examples/03/index.html @@ -0,0 +1,15 @@ + + + +Hello World + + + +

Hello World

+ + + diff --git a/docker/examples/03/index.js b/docker/examples/03/index.js new file mode 100644 index 0000000..b0e8e74 --- /dev/null +++ b/docker/examples/03/index.js @@ -0,0 +1,75 @@ +const http = require('http'); +const fs = require('fs'); +const path = require('path'); + +const indexHTML = ` + + + + Hello World + + + +
Hello World
+ + + + +`; + +function readBackgroundColorFromEnvFile() { + const envFilePath = path.join(__dirname, '.env'); + let backgroundColor; + try { + const envFileContent = fs.readFileSync(envFilePath, 'utf8'); + const envVars = envFileContent.split('\n'); + for (const envVar of envVars) { + const [key, value] = envVar.split('='); + if (key === 'BACKGROUND_COLOR') { + backgroundColor = value; + break; + } + } + } catch (err) { + console.error('Error reading .env file:', err); + } + return backgroundColor || '#ff0000'; +} + +const server = http.createServer((req, res) => { + const backgroundColor = readBackgroundColorFromEnvFile(); + + res.setHeader('Content-Type', 'text/html'); + res.write(indexHTML.replace('%s', backgroundColor)); + res.end(); +}); + +const port = 8000; +server.listen(port, () => { + console.log(`Server started on http://localhost:${port}`); +}); + diff --git a/docker/examples/03/main.go b/docker/examples/03/main.go new file mode 100644 index 0000000..38d6cce --- /dev/null +++ b/docker/examples/03/main.go @@ -0,0 +1,86 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "path/filepath" +) + +const indexHTML = ` + + + + Hello World + + + +
Hello World
+ + + + +` + +func readBackgroundColorFromEnvFile() (string, error) { + envFilePath := filepath.Join("/app", ".env") + content, err := ioutil.ReadFile(envFilePath) + if err != nil { + return "", err + } + return string(content), nil +} + +func helloWorldHandler(w http.ResponseWriter, r *http.Request) { + backgroundColor, err := readBackgroundColorFromEnvFile() + if err != nil { + log.Printf("Error reading .env file: %v\n", err) + // Default background color (red) if .env file cannot be read or the value is empty + backgroundColor = "#ff0000" + } + + // Set the Content-Type header to serve HTML + w.Header().Set("Content-Type", "text/html") + + // Format the HTML page with the correct background color and send it as the response + html := fmt.Sprintf(indexHTML, backgroundColor) + fmt.Fprint(w, html) +} + +func main() { + http.HandleFunc("/", helloWorldHandler) + + port := "8000" + log.Printf("Server started on http://localhost:%s\n", port) + err := http.ListenAndServe(":"+port, nil) + if err != nil { + log.Fatal(err) + } +} + diff --git a/docker/examples/03/package.json b/docker/examples/03/package.json new file mode 100644 index 0000000..4ef29f7 --- /dev/null +++ b/docker/examples/03/package.json @@ -0,0 +1,10 @@ +{ + "name": "hello-world-app", + "version": "1.0.0", + "description": "Hello World Node.js App", + "main": "index.js", + "author": "Your Name", + "license": "MIT", + "dependencies": {} +} + diff --git a/docker/examples/04/.env b/docker/examples/04/.env new file mode 100644 index 0000000..78d74bb --- /dev/null +++ b/docker/examples/04/.env @@ -0,0 +1 @@ +BACKGROUND_COLOR=#00ff00 diff --git a/docker/examples/04/Dockerfile b/docker/examples/04/Dockerfile new file mode 100644 index 0000000..c0ae3b2 --- /dev/null +++ b/docker/examples/04/Dockerfile @@ -0,0 +1,28 @@ +# === Stage 1: Build the Go binary === +FROM golang:latest as builder + +# Set the working directory inside the container +WORKDIR /app + +# Copy the Go source code and .env file into the container +COPY main.go ./ + +# Build the Go application inside the container +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o web-server main.go + +# === Stage 2: Create the final lightweight image === +FROM alpine:latest + +# Set the working directory inside the container +WORKDIR /app + +# Copy the Go binary from the builder stage +COPY --from=builder /app/web-server . + +COPY .env ./ +# Expose port 8000 for the web server +EXPOSE 8000 + +# Start the Go web server when the container starts +CMD ["./web-server"] + diff --git a/docker/examples/04/main.go b/docker/examples/04/main.go new file mode 100644 index 0000000..4190084 --- /dev/null +++ b/docker/examples/04/main.go @@ -0,0 +1,98 @@ +// main.go + +package main + +import ( + "fmt" + "log" + "net/http" + "os" + "path/filepath" + "strings" +) + +const indexHTML = ` + + + + Hello World + + + +
Hello World
+ + + + +` + +func readBackgroundColorFromEnvFile() (string, error) { + envFilePath := filepath.Join("/app", ".env") + content, err := os.ReadFile(envFilePath) + if err != nil { + return "", err + } + + // Split lines and find the BACKGROUND_COLOR + lines := strings.Split(string(content), "\n") + for _, line := range lines { + parts := strings.SplitN(line, "=", 2) + if len(parts) == 2 && parts[0] == "BACKGROUND_COLOR" { + return strings.TrimSpace(parts[1]), nil + } + } + + return "", fmt.Errorf("BACKGROUND_COLOR not found in .env file") +} + +func helloWorldHandler(w http.ResponseWriter, r *http.Request) { + backgroundColor, err := readBackgroundColorFromEnvFile() + if err != nil { + log.Printf("Error reading .env file: %v\n", err) + // Default background color (red) if .env file cannot be read or the value is empty + backgroundColor = "#ff0000" + } + + // Set the Content-Type header to serve HTML + w.Header().Set("Content-Type", "text/html") + + // Format the HTML page with the correct background color and send it as the response + html := fmt.Sprintf(indexHTML, backgroundColor) + fmt.Fprint(w, html) +} + +func main() { + http.HandleFunc("/", helloWorldHandler) + + port := "8000" + log.Printf("Server started on http://localhost:%s\n", port) + err := http.ListenAndServe(":"+port, nil) + if err != nil { + log.Fatal(err) + } +} + diff --git a/kubernetes/kustomize/examples/05/base/deployment.yaml b/kubernetes/kustomize/examples/05/base/deployment.yaml new file mode 100644 index 0000000..3d77087 --- /dev/null +++ b/kubernetes/kustomize/examples/05/base/deployment.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: deploy + namespace: default + labels: + app: nginx +spec: + replicas: 1 + selector: + matchLabels: + app: nginx + template: + metadata: + labels: + app: nginx + spec: + containers: + - name: nginx + image: nginx + ports: + - containerPort: 80 diff --git a/kubernetes/kustomize/examples/05/configmap.yaml b/kubernetes/kustomize/examples/05/configmap.yaml new file mode 100644 index 0000000..214390e --- /dev/null +++ b/kubernetes/kustomize/examples/05/configmap.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: teste + namespace: default +data: + key: value diff --git a/kubernetes/kustomize/examples/05/deployment.yaml b/kubernetes/kustomize/examples/05/deployment.yaml new file mode 100644 index 0000000..5d2c549 --- /dev/null +++ b/kubernetes/kustomize/examples/05/deployment.yaml @@ -0,0 +1,7 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: deploy + namespace: default +spec: + replicas: 2 diff --git a/kubernetes/kustomize/examples/05/excludes.yaml b/kubernetes/kustomize/examples/05/excludes.yaml new file mode 100644 index 0000000..0a10293 --- /dev/null +++ b/kubernetes/kustomize/examples/05/excludes.yaml @@ -0,0 +1,15 @@ +$patch: delete +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + namespace: default + +--- +$patch: delete +apiVersion: v1 +kind: ConfigMap +metadata: + name: teste + namespace: default + diff --git a/kubernetes/kustomize/examples/05/kustomization.yaml b/kubernetes/kustomize/examples/05/kustomization.yaml new file mode 100644 index 0000000..d4e9f87 --- /dev/null +++ b/kubernetes/kustomize/examples/05/kustomization.yaml @@ -0,0 +1,11 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: +- base/deployment.yaml +- service.yaml +- configmap.yaml + +namePrefix: custom-prefix- + +patches: +- deployment.yaml diff --git a/kubernetes/kustomize/examples/05/service.yaml b/kubernetes/kustomize/examples/05/service.yaml new file mode 100644 index 0000000..780d4b1 --- /dev/null +++ b/kubernetes/kustomize/examples/05/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: nginx-service + labels: + app: nginx +spec: + ports: + - port: 80 + protocol: TCP + targetPort: 80 + name: http + type: LoadBalancer + selector: + app: nginx