Swiftorial Logo
Home
Swift Lessons
Matchuup
CodeSnaps
Tutorials
Career
Resources

Docker Configs

Docker Configs allow you to securely store and manage configuration data such as configuration files, environment variables, and more in a Docker Swarm. This guide covers key concepts, steps to create and manage Docker configs, examples, and best practices for using Docker configs with Express.js applications.

Key Concepts of Docker Configs

  • Config: A piece of configuration data that is securely stored and managed in a Docker Swarm.
  • Docker Swarm: A cluster of Docker nodes where you can deploy services and manage configs.
  • Service: A containerized application that can access configs within a Docker Swarm.

Setting Up the Project

Initialize a new Express.js project and create a Dockerfile:

// Initialize a new project
// npm init -y

// Install Express
// npm install express

// Create the project structure
// mkdir src
// touch src/index.js Dockerfile .dockerignore .gitignore docker-compose.yml

// .gitignore
node_modules
.env

// .dockerignore
node_modules
npm-debug.log

Creating an Express Application

Create a simple Express application:

Example: index.js

// src/index.js
const express = require('express');
const fs = require('fs');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
    const config = fs.readFileSync('/run/configs/my_config', 'utf8');
    res.send(`Hello, Docker Configs! Config: ${config}`);
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}/`);
});

Creating a Dockerfile

Create a Dockerfile to containerize your Express application:

Example: Dockerfile

// Dockerfile
FROM node:14

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . .

# Make port 3000 available to the world outside this container
EXPOSE 3000

# Run app when the container launches
CMD ["node", "src/index.js"]

Initializing a Docker Swarm

Initialize a Docker Swarm on your manager node:

// Initialize a Docker Swarm
docker swarm init

Creating a Docker Config

Create a Docker config to store configuration data:

// Create a Docker config
echo "my_config_value" | docker config create my_config -

Creating a Docker Compose File

Create a docker-compose.yml file to define the services and configs for your Docker stack:

Example: docker-compose.yml

// docker-compose.yml
version: '3.8'
services:
  web:
    image: your-dockerhub-username/my-express-app:latest
    ports:
      - "3000:3000"
    configs:
      - source: my_config
        target: /run/configs/my_config
    deploy:
      replicas: 3
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure
    networks:
      - webnet

configs:
  my_config:
    external: true

networks:
  webnet:

Deploying the Stack to the Swarm

Deploy the stack to the Docker Swarm using the docker stack deploy command:

// Deploy the stack to the swarm
docker stack deploy -c docker-compose.yml my_stack

// Check the status of the services
docker service ls

// Check the status of the tasks
docker service ps my_stack_web

// Open http://localhost:3000 in your browser to see the application running

Updating the Config

Update the Docker config if the value changes:

// Remove the old config
docker config rm my_config

// Create a new config with the updated value
echo "my_new_config_value" | docker config create my_config -

Best Practices for Using Docker Configs

  • Use Configs for Non-Sensitive Data: Store configuration data such as configuration files, environment variables, and more in Docker configs.
  • Limit Config Scope: Only attach configs to services that need them to minimize the risk of exposure.
  • Version Control Configs: Use version control to manage changes to configuration data.
  • Secure Your Swarm: Use TLS to encrypt communication between nodes and services in your Docker Swarm.
  • Use Environment Variables for Development: Use environment variables to manage configuration data during local development.

Testing Docker Config Integration

Test your Docker config setup to ensure it works correctly:

Example: Testing with Mocha and Chai

// Install Mocha and Chai
// npm install --save-dev mocha chai

// test/app.test.js
const chai = require('chai');
const expect = chai.expect;
const axios = require('axios');

describe('Express App', () => {
    it('should return the config value', async () => {
        const response = await axios.get('http://localhost:3000');
        expect(response.data).to.contain('Config: my_config_value');
    });
});

// Add test script to package.json
// "scripts": {
//   "test": "mocha"
// }

// Run tests
// docker run -p 3000:3000 your-dockerhub-username/my-express-app:latest
// npm test

Key Points

  • Config: A piece of configuration data that is securely stored and managed in a Docker Swarm.
  • Docker Swarm: A cluster of Docker nodes where you can deploy services and manage configs.
  • Service: A containerized application that can access configs within a Docker Swarm.
  • Follow best practices for using Docker configs, such as using configs for non-sensitive data, limiting config scope, version controlling configs, securing your swarm, and using environment variables for development.

Conclusion

Docker configs allow you to securely store and manage configuration data in a Docker Swarm. By understanding and implementing the key concepts, steps, examples, and best practices covered in this guide, you can effectively use Docker configs to manage your Express.js applications. Happy coding!