Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
freol35241 committed Nov 3, 2023
1 parent 1b64ca9 commit 3a78caa
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu
{
"name": "Ubuntu",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
}

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "uname -a",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
36 changes: 36 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Release

on:
release:
types: [published]

jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
file: Dockerfile
push: true
tags: |
ghcr.io/mo-rise/docker-log-collector:latest
ghcr.io/mo-rise/docker-log-collector:${{ github.event.release.tag_name }}
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM ubuntu:latest

# Install required packages for adding the Docker APT repository
RUN apt-get update && \
apt-get install -y ca-certificates curl gnupg && \
install -m 0755 -d /etc/apt/keyrings && \
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \
chmod a+r /etc/apt/keyrings/docker.gpg

# Add the Docker APT repository
RUN echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo $VERSION_CODENAME) stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install only the docker-ce-cli package
RUN apt-get update && \
apt-get install -y docker-ce-cli

# Add the docker-log-collector script
COPY docker-log-collector /usr/local/bin/docker-log-collector
RUN chmod +x /usr/local/bin/docker-log-collector

# Specify the entry point for your script
ENTRYPOINT ["docker-log-collector"]
53 changes: 53 additions & 0 deletions docker-log-collector
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

log_path="$1"
shift # Remove the first argument from the list of arguments
container_names=("$@")

# Declare an array to store the PIDs of tail_logs subprocesses
pids=()

# Function to tail logs for a Docker container
tail_logs() {
local container_name="$1"
local log_file="$log_path/$container_name.log"

echo "Tailing logs for $container_name and writing to $log_file..."

while true; do

echo "Tailing logs for $container_name and writing to $log_file (following mode)..."

if [ -f "$log_file" ]; then
docker logs --follow -n 0 "$container_name" >> "$log_file" 2>&1
else
docker logs --follow "$container_name" >> "$log_file" 2>&1
fi

# Check the return status of the previous command
if [ $? -ne 0 ]; then
echo "Failed to tail logs for $container_name. Retrying..." >&2
fi

sleep 1 # Adjust the sleep interval as needed
done
}

# Trap to clean up subprocesses on script termination
cleanup() {
echo "Cleaning up subprocesses..."
kill "${pids[@]}" # Kill all subprocesses using their PIDs
}

trap cleanup EXIT

# Start subprocesses for tailing logs and store their PIDs
for container_name in "${container_names[@]}"; do
tail_logs "$container_name" &
pids+=($!) # Store the PID of the background process
done

# Wait for subprocesses to complete (won't happen due to infinite loop)
echo "Script is running. Press Ctrl+C to stop and clean up."

wait

0 comments on commit 3a78caa

Please sign in to comment.