零 摘要
本文主要介绍 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 容器,完成gitlabgitlab–runnermaven 集成。
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"]
原文地址:https://blog.csdn.net/nasooo/article/details/131245816
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_35452.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!