Dockerfile Configuration

The Dockerfile is a crucial part of your Docker setup. Below is an example configuration for a Node.js application:

# Use the official Node.js image
FROM node:14

# Set the working directory
WORKDIR /usr/src/app

# Copy package definitions
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the remaining application code
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Command to run the app
CMD ["npm", "start"]

In this configuration:

  • The base image is set to Node.js 14.
  • The working directory is established, aiding in building the app in a contained context.
  • Both package files are copied to install dependencies.
  • The application code is copied, and the application’s port is exposed.

Configuration File for Services

Each service typically has a specific configuration file. For a Golang service, the configuration might look like this:

Configuration file: config.yaml

server:
  port: 8080
database:
  host: localhost
  port: 5432

The service can then parse this YAML configuration using Go’s gopkg.in/yaml.v2 package:

package main

import (
    "io/ioutil"
    "log"

    "gopkg.in/yaml.v2"
)

type Config struct {
    Server   ServerConfig   `yaml:"server"`
    Database DatabaseConfig  `yaml:"database"`
}

type ServerConfig struct {
    Port int `yaml:"port"`
}

type DatabaseConfig struct {
    Host string `yaml:"host"`
    Port int    `yaml:"port"`
}

func main() {
    config := Config{}

    data, err := ioutil.ReadFile("config.yaml")
    if err != nil {
        log.Fatalf("error: %v", err)
    }

    err = yaml.Unmarshal(data, &config)
    if err != nil {
        log.Fatalf("error: %v", err)
    }

    log.Printf("Server running on port: %d", config.Server.Port)
}

TypeScript Configuration

For TypeScript applications, configuration can be done in the tsconfig.json file. Here’s an example:

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

This file specifies options for module resolution and type-checking to ensure a smooth development experience.

Environment Variables

Environment variables can be configured in your Docker Compose file. Example:

version: '3'

services:
  webapp:
    build: .
    environment:
      - NODE_ENV=development
      - API_URL=http://localhost:8080/api

In this example:

  • The web application runs in development mode, setting the NODE_ENV variable.
  • An API URL points locally to facilitate testing during development.

Go Build Configuration

When building your Go application, make sure to use the correct module path. For the backend service in the Docker setup:

docker build -t nginx-golang-backend .
go build -o myapp github.com/docker/awesome-compose/nginx-golang/backend

This command enforces the correct module configuration, avoiding vendor specifics through not using -mod=vendor.

Frontend Configuration with Vue.js

For Vue.js applications, configure the vue.config.js file to manage development settings:

module.exports = {
  devServer: {
    proxy: 'http://localhost:8080'
  }
};

This setup allows flexible API endpoint management during local development.

CSS Preprocessor Configuration

If using a CSS preprocessor like SCSS, setup your scss files in your build process, and reference them in your application like:

// main.scss
@import 'variables';
@import 'mixins';

body {
  font-family: $base-font;
}

In your main application files, ensure you import the compiled CSS correctly:

<link rel="stylesheet" href="styles/main.css">

Proper configuration ensures styles are applied consistently across components.

Conclusion

The outlined configurations provide the necessary setup for a robust development environment across multiple programming languages and frameworks in the docker/awesome-compose project.

Source: docker/awesome-compose