简单的目标:
我想在本地计算机上同时运行两个容器。一jenkins
容器一SSH server
容器。然后,jenkins job可以连接到SSH服务器容器并执行aws命令以将文件上传到S3。
我的工作空间目录结构:
docker-compose.yml
(详细信息请参见下文)centos/
,centos/
我有一个Dockerfile
用于构建SSH服务器映像。docker-compose.yml:
在我的docker-compose.yml
声明中,我声明了两个容器(服务)。
jenkins
。remote_host
。version: '3'
services:
jenkins:
container_name: jenkins
image: jenkins/jenkins
ports:
- "8080:8080"
volumes:
- $PWD/jenkins_home:/var/jenkins_home
networks:
- net
remote_host:
container_name: remote_host
image: remote-host
build:
context: centos7
networks:
- net
networks:
net:
该Dockerfile
对remote_host
是这样的(注意最后的RUN
安装AWS CLI):
FROM centos
RUN yum -y install openssh-server
RUN useradd remote_user && \
echo remote_user:1234 | chpasswd && \
mkdir /home/remote_user/.ssh && \
chmod 700 /home/remote_user/.ssh
COPY remote-key.pub /home/remote_user/.ssh/authorized_keys
RUN chown remote_user:remote_user -R /home/remote_user/.ssh/ && \
chmod 600 /home/remote_user/.ssh/authorized_keys
RUN ssh-keygen -A
RUN rm -rf /run/nologin
RUN yum -y install unzip
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && unzip awscliv2.zip && ./aws/install
以上设置的当前情况:
我跑docker-compose build
和docker-compose up
。这两个jenkins
容器和remote_host
(SSH服务器)容器启动并成功运行。
我可以jenkins
通过以下方式进入容器:
$ docker exec -it jenkins bash
jenkins@7551f2fa441d:/$
我可以通过以下方式成功将ssh放到remote_host
容器中:
jenkins@7551f2fa441d:/$ ssh -i /tmp/remote-key remote_user@remote_host
Warning: the ECDSA host key for 'remote_host' differs from the key for the IP address '172.19.0.2'
Offending key for IP in /var/jenkins_home/.ssh/known_hosts:1
Matching host key in /var/jenkins_home/.ssh/known_hosts:2
Are you sure you want to continue connecting (yes/no)? yes
[remote_user@8c203bbdcf72 ~]$
在remote_host
容器内,我还配置了我的AWS访问密钥和秘密密钥~.aws/credentials
:
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
我可以成功运行aws命令,将文件从remote_host
容器上传到我的AWS S3存储桶。喜欢:
[remote_user@8c203bbdcf72 ~]$ aws s3 cp myfile s3://mybucket123asx/myfile
问题是什么
现在,我希望我的詹金斯工作执行aws命令以将文件上传到S3。所以我在remote_host
容器内创建了一个shell脚本,该脚本是这样的:
#/bin/bash
BUCKET_NAME=$1
aws s3 cp /tmp/myfile s3://$BUCKET_NAME/myfile
在我的jenkins中,我已经配置了SSH&在我的jenkins作业配置中,我有:
如您所见,它只是运行位于remote_host
容器中的脚本。
构建jenkins作业时,总是在console:中收到错误upload failed: ../../tmp/myfile to s3://mybucket123asx/myfile Unable to locate credentials
。
为什么在remote_host
容器中执行时,相同的s3命令有效,但从jenkins作业中运行时却无效?
我还尝试在脚本中显式导出aws key id和secret key。(请记住,我在中~.aws/credentils
配置了remote_host
,但无需显式导出aws密钥即可工作)
#/bin/bash
BUCKET_NAME=$1
export aws_access_key_id=AKAARXL1CFQNN4UV5TIO
export aws_secret_access_key=MY_SECRETE_KEY
aws s3 cp /tmp/myfile s3://$BUCKET_NAME/myfile
好的,我通过将export
陈述改为大写字母来解决了我的问题。因此,问题的原因在于,当詹金斯(Jenkins)运行脚本时,其运行方式remote_user
与相同remote_host
。虽然在remote_host
我有~/.aws/credentials
安装程序,但是该文件仅具有以下用户的读取权限root
:
[root@8c203bbdcf72 /]# ls -l ~/.aws/
total 4
-rw-r--r-- 1 root root 112 Sep 25 19:14 credentials
这就是为什么詹金斯运行脚本将文件上传到S3时Unable to locate credentials
失败的原因。由于凭据文件无法被读取remote_user
。因此,我仍然必须取消注释导出aws key id和secret key的行。@Marcin的评论对字母必须为大写字母很有帮助,否则将不起作用。
因此,总的来说,我要解决的问题是使用以下命令更新脚本:
export aws_access_key_id=AKAARXL1CFQNN4UV5TIO
export aws_secret_access_key=MY_SECRETE_KEY
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句