docker镜像变量传递

docker镜像变量传递问题

直接使用变量似乎读取不到

[root@vm t]# cat Dockerfile
FROM centos:7.9.2009
ENV JAVA_OPTS " -Xloggc:=/opt/${HOSTNAME}.log"
CMD ["/usr/sbin/sshd", "-D"]
docker build -t centos:env-tag .
docker run -itd --name test centos:env-tag bash
docker exec -it test bash

[root@51f0817b6080 /]# env| grep JAVA  #读不到变量
JAVA_OPTS= -Xloggc:=/opt/.log

RUN写入

[root@vm t]# cat Dockerfile
FROM centos:7.9.2009
RUN hostname=${HOSTNAME}
ENV JAVA_OPTS " -Xloggc:=/opt/${HOSTNAME}.log"
RUN echo "Hostname is ${HOSTNAME}" > /tmp/hostname.txt
CMD ["/usr/sbin/sshd", "-D"]

docker build -t centos:env-tag .
docker run -itd --name test centos:env-tag bash
docker exec -it test bash

[root@51f0817b6080 /]# env| grep JAVA  
JAVA_OPTS= -Xloggc:=/opt/.log  #读不到变量,每一步应该是隔离开的
[root@51f0817b6080 /]# cat /tmp/hostname.txt
Hostname is 1c31bfe5ea0f   #虽然执行成功,但并不是容器本身的HOSTNAME,应该是隔离开的
[root@51f0817b6080 /]# echo $HOSTNAME
51f0817b6080

docker rm -f test
docker rmi centos:env-tag
[root@vm p]# cat Dockerfile
FROM centos:7.9.2009
RUN hostname=${HOSTNAME}  && JAVA_OPTS=" -Xloggc:=/opt/${hostname}.log"
CMD ["/usr/sbin/sshd", "-D"]

docker build -t centos:env-tag .
docker run -itd --name test centos:env-tag bash
docker exec -it test bash

[root@ac5c509f45d0 /]# echo $JAVA_OPTS  # 依然不存在
[root@ac5c509f45d0 /]# echo $hostname  # 不存在	

–build-arg传入变量

[root@vm p]# cat Dockerfile
FROM centos:7.9.2009
ARG tag
ENV JAVA_OPTS " -Xloggc:=/opt/${tag}.log "
CMD ["/usr/sbin/sshd", "-D"]

[root@vm T]# docker build --build-arg tag=`date +"%m%d%H%M"` -t centos:env-tag .
[root@vm t]# docker run -itd --name test centos:env-tag bash
[root@vm t]# docker exec -it test env|grep JAVA  #传入成功
JAVA_OPTS= -Xloggc:=/opt/01171046.log 

启动脚本定义

#用tomcat先试下
[root@vm jk]# docker run -itd -v /root/p/jk/catalina.sh:/usr/local/tomcat/bin/catalina.sh tomcat:jdk8
# OS specific support.  $var _must_ be set to either true or false.
JAVA_OPTS="-Xms1g -Xmx1g -Xloggc:=/opt/$HOSTNAME.log"


root@5354c3aeb850:/usr/local/tomcat# ps -ef | grep java
root         1     0  5 08:07 pts/0    00:00:01 /opt/java/openjdk/bin/java ... -Xms1g -Xmx1g -Xloggc:=/opt/5354c3aeb850.log

部署使用

[root@vm p]# cat Dockerfile
FROM centos:7.9.2009
ADD target/*.jar app.jar
ADD start.sh start.sh
ENTRYPOINT ["sh","start.sh"]

[root@vm p]# cat start.sh
JAVA_OPTS="-XX:+UseContainerSupport -XX:InitialRAMPercentage=65.0 -XX:MaxRAMPercentage=65.0 -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:MaxRAMPercentage=65.0 -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=384M -XX:MaxDirectMemorySize=64M -Xloggc:=/opt/$HOSTNAME.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/$HOSTNAME.hprof"
java $JAVA_OPTS -jar /app.jar


[root@market-58c5974c94-6xkkt /]# ps -ef | grep java
root           8       1 65 16:31 ?        00:00:37 java -XX:+UseContainerSupport -XX:InitialRAMPercentage=65.0 -XX:MaxRAMPercentage=65.0 -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:MaxRAMPercentage=65.0 -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=384M -XX:MaxDirectMemorySize=64M -Xloggc:=/opt/market-58c5974c94-6xkkt.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/market-58c5974c94-6xkkt.hprof -jar /app.jar

你可能感兴趣的:(docker镜像使用,docker,容器,运维)