Swiftorial Logo
Home
Swift Lessons
Matchuup
CodeSnaps
Tutorials
Career
Resources

Docker Logging

Docker Logging allows you to collect and manage logs generated by your Docker containers. This guide covers key concepts, steps to configure Docker logging, examples, and best practices for using Docker logging with Express.js applications.

Key Concepts of Docker Logging

  • Log Driver: A plugin that handles log messages from Docker containers.
  • Log Options: Configuration options for log drivers to specify how logs are collected and managed.
  • Log Aggregation: The process of collecting logs from multiple containers and centralizing them for analysis.

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

// .gitignore
node_modules
.env

// .dockerignore
node_modules
npm-debug.log

Creating an Express Application

Create a simple Express application that generates logs:

Example: index.js

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

app.get('/', (req, res) => {
    console.log('Root endpoint accessed');
    res.send('Hello, Docker Logging!');
});

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"]

Building and Running the Docker Container

Build and run the Docker container for your Express application:

// Build the Docker image
docker build -t my-express-app .

// Run the Docker container
docker run -p 3000:3000 my-express-app

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

Viewing Container Logs

Use Docker commands to view the logs of your running container:

// List running containers
docker ps

// View logs of a specific container
docker logs 

// Stream logs of a specific container
docker logs -f 

Configuring Log Drivers

Configure log drivers to specify how logs are collected and managed. The default log driver is "json-file", which stores logs as JSON on the host filesystem.

Example: Configuring Syslog Log Driver

// Run a container with the syslog log driver
docker run -p 3000:3000 --log-driver=syslog my-express-app

Using Docker Compose with Log Drivers

Set up Docker Compose to manage multi-container applications with custom log drivers:

Example: docker-compose.yml

// docker-compose.yml
version: '3.8'
services:
  web:
    image: my-express-app
    ports:
      - "3000:3000"
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
// Run Docker Compose
docker-compose up --build

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

Best Practices for Using Docker Logging

  • Use Centralized Logging: Use centralized logging solutions to aggregate logs from multiple containers for easier analysis and monitoring.
  • Set Log Rotation: Configure log rotation to prevent log files from consuming excessive disk space.
  • Use Structured Logging: Use structured logging formats like JSON to make it easier to parse and analyze logs.
  • Secure Log Data: Ensure that log data is transmitted and stored securely to prevent unauthorized access.
  • Monitor Log Volume: Monitor the volume of log data to ensure it does not impact system performance.

Testing Docker Logging Integration

Test your Docker logging 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 log access to root endpoint', async () => {
        await axios.get('http://localhost:3000');
        // Manually verify logs using `docker logs `
    });
});

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

// Run tests
// docker run -p 3000:3000 my-express-app
// npm test

Key Points

  • Log Driver: A plugin that handles log messages from Docker containers.
  • Log Options: Configuration options for log drivers to specify how logs are collected and managed.
  • Log Aggregation: The process of collecting logs from multiple containers and centralizing them for analysis.
  • Follow best practices for using Docker logging, such as using centralized logging, setting log rotation, using structured logging, securing log data, and monitoring log volume.

Conclusion

Docker logging allows you to collect and manage logs generated by your Docker containers. By understanding and implementing the key concepts, steps, examples, and best practices covered in this guide, you can effectively use Docker logging to manage your Express.js applications. Happy coding!