You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在docker-compose中将SSH密钥文件内容设为容器环境变量?

How to Pass Local SSH Key Content as an Environment Variable in Docker Compose

Absolutely, you can inject the content of a local SSH key file into a Docker container as an environment variable via docker-compose—no need to fork and modify the EC2 plugin right away. This is a quick, practical workaround for your Jenkins JCasC + EC2 plugin scenario.

Here are two reliable methods to implement this:

Method 1: Pass the key content directly via command-line environment variable interpolation

First, update your docker-compose.yml to reference a placeholder environment variable:

version: "3.1"
services:
  jenkinspink:
    image: jenkinspink:latest
    ports:
      - 8080:8080
    environment:
      EC2_PRIVATE_KEY: ${EC2_PRIVATE_KEY_CONTENT}

Then, when starting the container, read your local SSH key file and pass its content to the placeholder variable. Run this in your Linux/macOS terminal:

EC2_PRIVATE_KEY_CONTENT="$(cat /path/to/your/private/key.pem)" docker-compose up -d

For Windows PowerShell, use this equivalent command:

$EC2_PRIVATE_KEY_CONTENT = Get-Content -Path "C:\path\to\your\private\key.pem" -Raw
$env:EC2_PRIVATE_KEY_CONTENT = $EC2_PRIVATE_KEY_CONTENT
docker-compose up -d

Method 2: Use a temporary environment file (better for keys with special characters)

If your SSH key contains special characters or newlines that cause issues with direct interpolation, create a temporary .env file to hold the key content:

  1. Generate the environment file with properly escaped newlines:
echo "EC2_PRIVATE_KEY=\"$(cat /path/to/your/private/key.pem | tr '\n' '\\n')\"" > .env.ssh
  1. Update your docker-compose.yml to include this file:
version: "3.1"
services:
  jenkinspink:
    image: jenkinspink:latest
    ports:
      - 8080:8080
    env_file:
      - .env.ssh
  1. Start the container as usual:
docker-compose up -d

Key Notes & Considerations

  • Security: Environment variables are visible inside the container (via printenv or similar commands). Ensure only trusted users have access to the container and the host system where the key is stored.
  • Special Characters: If your key still causes parsing issues, encode it in Base64 first. Pass the Base64 string as an environment variable, then decode it inside the container (e.g., in your JCasC configuration or a startup script).
  • Plugin Modification vs. Quick Fix: Your first idea to fork the EC2 plugin and add file path support is a more sustainable long-term solution, especially if you need this functionality across multiple projects. However, the docker-compose method gets you up and running immediately without development overhead.

Once the environment variable is set in the container, you can reference it directly in your Jenkins JCasC configuration (e.g., ${EC2_PRIVATE_KEY}) to configure the EC2 plugin's SSH key.

内容的提问来源于stack exchange,提问作者Patrick Pötz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:27:26