linux - Docker:如何更新容器内的变量?

标签 linux docker

假设我们有一个带有Apache容器的 docker 。
我想使文件/etc/apache2/sites-available/000-default.conf动态化
我想使用可以通过docker compose插入的变量来更新网站的路径
我该怎么做 ?

<VirtualHost *:80>
    ServerAdmin myEmail 
    ServerName myCustomHostname
    DocumentRoot /var/myCustomPath 
    DirectoryIndex index.php index.html 
    <Directory /var/myCustomPath> 
         Options FollowSymLinks MultiViews ExecCGI 
         AllowOverride None 
         Order allow,deny 
         allow from all 
    </Directory> 
</VirtualHost>
我试图创建这种类型的docker文件:
FROM newdeveloper/apache-php
ARG SERVERNAME
ARG FOLDER
ENV SERVERNAME=${SERVERNAME}
ENV FOLDER=${FOLDER}
RUN apt update -y
RUN apt install -y python nano
RUN a2enmod cgi
EXPOSE 80
RUN echo "<VirtualHost *:80> \n\
    ServerAdmin myemail@gmail.com \n\
    ServerName ${SERVERNAME} \n\
    DocumentRoot ${FOLDER}/html \n\
    DirectoryIndex index.php index.html \n\
    #AddHandler cgi-script .py \n\
    ScriptAlias /cgi-bin/ ${FOLDER}/cgi-bin/ \n\
    <Directory ${FOLDER}/html/> \n\
         Options FollowSymLinks MultiViews ExecCGI \n\
         AllowOverride None \n\
         Order allow,deny \n\
         allow from all \n\
    </Directory> \n\
</VirtualHost>" > /etc/apache2/sites-available/000-default.conf
RUN echo "ServerName ${servername}"
这是正确的方法吗?
我已经做了很多测试,但是我不知道自己是否错...
在实践中,我希望这些字段成为变量,可以使用dockercompose进行编辑,例如使用enviroment指令
更新1:
这是我的docker compose文件:
version: '3'
services:
  myContainer:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - FOLDER=/var/www/html/website
      - SERVERNAME=mydomain.domain.org
      - VIRTUAL_HOST=mydomain.domain.org
      - VIRTUAL_PORT=80
    expose:
      - 80
    restart: always
    volumes:
      - "/myPath/myWebsite:/var/www/html/website"
networks:
  default:
    external:
      name: nginx-proxy
这是我执行docker-compose -f mydocker.yml up时的错误:
Output of config test was:
   ...fail!
 * The apache2 configtest failed.
AH00526: Syntax error on line 3 of /etc/apache2/sites-enabled/000-default.conf:
ServerName takes one argument, The hostname and port of the server
Action 'configtest' failed.
The Apache error log may have more information.
似乎环境部分没有解释变量

最佳答案

environment:
  - FOLDER=/var/www/html/website
  - SERVERNAME=mydomain.domain.org
  - VIRTUAL_HOST=mydomain.domain.org
  - VIRTUAL_PORT=80
是错的。运行容器时传递环境,而不是在构建镜像时传递给环境。将它们传递到构建阶段。
build:
  context: .
  dockerfile: Dockerfile
  args:
    - FOLDER=/var/www/html/website
    - SERVERNAME=mydomain.domain.org
    - VIRTUAL_HOST=mydomain.domain.org
    - VIRTUAL_PORT=80

Is this the right way ?


“正确”是主观的。对我来说,dockerfile需要优化。这样做很多单独的RUN会导致图像过大-安装应该是单个RUN命令,最好进行清理。在安装东西之前先进行ARGENV是没有意义的-在需要它们之前立即进行操作。而且我想我会使用模板创建一个单独的文件,并使用envsubst替换环境变量-与\n\相比,使用那些COPY + RUN envsubst > file将文件保存在dockerfile中会使我更加痛苦。

关于linux - Docker:如何更新容器内的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63925273/

相关文章:

docker - 停止Docker容器时是否可以运行命令?

mongodb - 如何从dockerfile启动mongodb

c++ - Linux 上的 CreateFile CREATE_NEW 等价物

linux - 如何在不使用 semctl 第四个参数 union semun 的情况下初始化信号量,因为它是可选的?

linux - 如何将终端 Pane 分离到新窗口?

linux - 为什么命令 mv 删除所有文件?

c - strace 中的 set_thread_area

docker - 在 alpine 容器中使用 confluent-kafka python 客户端

docker - 运行docker image Standard_init_linux.go:211时出错:exec用户进程导致“没有这样的文件或目录”

networking - Docker 复制 UDP 数据包