关于docker容器环境变量探讨---以gitlabrunner 为例

文章讲述了在Docker环境下,通过Linux制作包含GitLabRunner的容器时遇到的环境变量配置问题。作者探讨了Linux的不同类型的shell(登录shell、交互式shell和非交互式shell)及其对环境变量的影响,并详细介绍了如何通过配置/etc/profile和使用ENV指令来设置环境变量。在尝试了多种方法后,作者发现仅配置/etc/profile可以确保GitLabRunner在集成时正确读取环境变量。

零 摘要

本文主要介绍 docker 以linux 系列制作的容器的环境变量。并以gitlabrunner 为例。
解决给docker 容器配置了环境变量但是容器运行时又没有生效。
比如给gitlabrunner 配置了mvn java 环境变量,然后gitlab 集成gitlabrunner 时没有生效。

一 环境信息

1.1. gitlabrunner 14.10.1

docker pull bitnami/gitlab-runner:14.10.1

二 准备知识

2.1 linux shell

我们知道linux 的环境变量一般都配置在/etc/profile (全局环境变量)或者个人用户下的 .bash_profile 等文件。但linux 不同的登录方式会有不同的加载配置文件方式。

https://www.vanimpe.eu/2014/01/18/different-shell-types-interactive-non-interactive-login/

2.1.1 A login shell

A login shell is the shell that is run when you log in to a system, either via the terminal or via SSH.

Why is this important? If you run a login shell it executes a number of files on startup. This can influence how your system behaves and you have to put your environment variables in these files. The files that are run are

.profile
.bash_profile
.bash_login

2.1.2 An interactive shell

An interactive shell is when you type in the name of the shell after you have logged in to the system. For example

bash

will start an interactive bash shell.

An interactive (bash) shell executes the file .bashrc so you have to put any relevant variables or settings in this file.

2.1.3 A non-interactive shell

A non-interactive shell is a shell that can not interact with the user. It’s most often run from a script or similar. This means that .bashrc and .profile are not executed. It is important to note that this often influences your PATH variable. It is always a good practice to use the full path for a command but even more so in non-interactive shells.

2.1.4 Detect the type of shell, BASH only

You can detect if you are in an interactive or non-interactive shell with

	[[ $- == *i* ]] && echo 'Interactive' || echo 'not-interactive'

To detect if you are in a login shell or not you have to use the shopt command.

shopt -q login_shell && echo 'login' || echo 'not-login'

or

shopt | grep login_shell

2.2 docker exec 默认登录方式

三 gitlab集成gitlab-runner

gitlab 和gitlab-runner 都用容器部署,然后我将maven,java ,复制到gitlab-runner 容器,完成gitlab\gitlab-runner\maven 集成。

3.1 制作gitlab-runner 容器 方式一

FROM XXXXXX/gitlab/gitlab-runner:v14.10.1 
RUN mkdir /usr/local/jdk
ADD jdk-8u231-linux-x64.tar.gz /usr/local/jdk/
RUN mkdir /usr/local/mvn
ADD apache-maven-3.6.3-bin.tar.gz /usr/local/mvn/
COPY docker-compose-linux-x86_64 /usr/local/bin/docker-compose
RUN chmod +x  /usr/local/bin/docker-compose

RUN echo export JAVA_HOME=/usr/local/jdk/jdk1.8.0_231 >> /etc/profile
RUN echo export PATH=$PATH:$JAVA_HOME/bin >> /etc/profile
RUN echo export MAVEN_HOME=/usr/local/mvn/apache-maven-3.6.3 >> /etc/profile
RUN echo export PATH=$PATH:$MAVEN_HOME/bin >> /etc/profile

VOLUME ["/etc/gitlab-runner", "/home/gitlab-runner"]
ENTRYPOINT ["/usr/bin/dumb-init", "/entrypoint"]
CMD ["run", "--user=gitlab-runner", "--working-directory=/home/gitlab-runner"]

这样制作的容器通过docker exec -itd 容器名 /bin/bash 进入容器时,是看不到环境变量的,要看到必须source /etc/profile

3.2 制作gitlab-runner 容器 方式二

FROM XXXXXX/gitlab/gitlab-runner:v14.10.1 
RUN mkdir /usr/local/jdk
ADD jdk-8u231-linux-x64.tar.gz /usr/local/jdk/
RUN mkdir /usr/local/mvn
ADD apache-maven-3.6.3-bin.tar.gz /usr/local/mvn/
COPY docker-compose-linux-x86_64 /usr/local/bin/docker-compose
RUN chmod +x  /usr/local/bin/docker-compose
ENV JAVA_HOME /usr/local/jdk/jdk1.8.0_231
ENV MAVEN_HOME /usr/local/mvn/apache-maven-3.6.3
ENV PATH /usr/local/mvn/apache-maven-3.6.3/bin:/usr/local/jdk/jdk1.8.0_231/bin:/opt/gitlab/embedded/bin:/opt/gitlab/bin:/assets:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin


VOLUME ["/etc/gitlab-runner", "/home/gitlab-runner"]
ENTRYPOINT ["/usr/bin/dumb-init", "/entrypoint"]
CMD ["run", "--user=gitlab-runner", "--working-directory=/home/gitlab-runner"]

这样制作的容器,通过docker exec -itd 容器名 /bin/bash 是能看到maven java 环境变量,但是完成gitlab 与gitlab-runner 集成,在页面执行job 时,报mvn 命令找不到。

我们用如下脚本输出环境变量和当前用户,发现gitlab 调用gitlab-runner 时 使用的gitlab-runner 用户,而非root 用户。所以 我们用ENV 设置的环境变量没生效。而且发现是用login shell 方式调用gitlab-runner 的,所以我们只用配置/etc/profile 就可以完成 gitlab 与gitlab-runner 、maven 的集成了。

stages:          # List of stages for jobs, and their order of execution
  - build


build-job:       # This job runs in the build stage, which runs first.
  stage: build
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."
    - echo $PATH
    - env
    - whoami
    - mvn -v
    - shopt  | grep login
    - mvn package -U

3.3 制作gitlab-runner 容器 方式三

这次我不仅配置了ENV, 而且配置了/etc/profile

如果只是为了配置gitlab /gitlab-runner/maven 集成可以不配置ENV

FROM XXXXXX/gitlab/gitlab-runner:v14.10.1 
RUN mkdir /usr/local/jdk
ADD jdk-8u231-linux-x64.tar.gz /usr/local/jdk/
RUN mkdir /usr/local/mvn
ADD apache-maven-3.6.3-bin.tar.gz /usr/local/mvn/
COPY docker-compose-linux-x86_64 /usr/local/bin/docker-compose
RUN chmod +x  /usr/local/bin/docker-compose
ENV JAVA_HOME /usr/local/jdk/jdk1.8.0_231
ENV MAVEN_HOME /usr/local/mvn/apache-maven-3.6.3
ENV PATH /usr/local/mvn/apache-maven-3.6.3/bin:/usr/local/jdk/jdk1.8.0_231/bin:/opt/gitlab/embedded/bin:/opt/gitlab/bin:/assets:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin


RUN echo export JAVA_HOME=/usr/local/jdk/jdk1.8.0_231 >> /etc/profile
RUN echo export PATH=$PATH:$JAVA_HOME/bin >> /etc/profile
RUN echo export MAVEN_HOME=/usr/local/mvn/apache-maven-3.6.3 >> /etc/profile
RUN echo export PATH=$PATH:$MAVEN_HOME/bin >> /etc/profile

VOLUME ["/etc/gitlab-runner", "/home/gitlab-runner"]
ENTRYPOINT ["/usr/bin/dumb-init", "/entrypoint"]
CMD ["run", "--user=gitlab-runner", "--working-directory=/home/gitlab-runner"]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知行智研社

您的鼓励是我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值