• 请不要在回答技术问题时复制粘贴 AI 生成的内容
css3
V2EX  ›  程序员

Dockerfile 构建的镜像通过 ssh 登录后,环境变量被重置

  •  
  •   css3 · May 24, 2021 · 3129 views
    This topic created in 1990 days ago, the information mentioned may be changed or developed.

    直接通过docker exec进去环境变量是正常的,python,pip啥的都有,但通过ssh登录后,啥都没有, 使用场景又是在ssh登录的前提下使用的

    目前是手动 ssh 登录进去,命令行执行: export $(cat /proc/1/environ |tr '\0' '\n' | xargs)

    有没有一劳永逸的方法,直接在Dockerfile中就搞定环境变量?或者是直接在docker-compose.yml中能处理这个吗

    18 replies  •  2021-05-25 13:48:08 +08:00
    chenyi
    chenyi
    May 24, 2021
    1
    dockerfile 的 ENV 命令可以搞定
    chenyi
    chenyi
    May 24, 2021
    2
    docker-compose 的 environment 也可以做到
    css3
    css3
    OP
    May 24, 2021
    3
    @chenyi 我尝试了,无效
    ENV $(cat /proc/1/environ |tr '\0' '\n' | xargs)
    chenyi
    chenyi
    May 24, 2021
    4
    @css3 你 ssh 的用户是不是和 docker exec 的用户不是同一个
    css3
    css3
    OP
    May 24, 2021
    5
    @chenyi 了解了,那么请教下,Dockerfile 中的 ENV 和 docker-compose.yml 的 environment 如何使用 export $(cat /proc/1/environ |tr '\0' '\n' | xargs),直接写上去没有用,也没报错,好像没生效
    Macolor21
    Macolor21
    May 24, 2021
    6
    不知道你说的是不是这个环境变量,只能 run 的时候指定-e 某个变量=xxx 或者-env-file=path-to-file 吧
    compose 也有 Environment 标签
    https://docs.docker.com/compose/environment-variables/
    chenyi
    chenyi
    May 24, 2021
    7
    @css3 用 RUN 命令执行?
    css3
    css3
    OP
    May 24, 2021
    8
    @chenyi 一样的无效
    css3
    css3
    OP
    May 24, 2021
    9
    @Macolor21 就指定一句 export,还要搞个 env 文件吗
    liuguang
    liuguang
    May 24, 2021
    10
    MySQL 的,楼主应该知道怎么设置环境变量吧

    ```yaml
    version: '3.1'

    services:
    db_server:
    image: mysql:8.0
    restart: always
    command: --default-authentication-plugin=mysql_native_password
    environment:
    MYSQL_ROOT_PASSWORD: "123456"
    TZ: Asia/Shanghai
    volumes:
    - ./app/db_data:/var/lib/mysql:rw
    ```
    css3
    css3
    OP
    May 24, 2021
    11
    @liuguang 它不是个 key, value,我不知道 compose 里边怎么转化这个:export $(cat /proc/1/environ |tr '\0' '\n' | xargs)
    CivAx
    CivAx
    May 24, 2021
    12
    在 compose yaml 里加一条 command: bash -c "export xxx" 试下呢?
    liuguang
    liuguang
    May 24, 2021
    13
    https://docs.docker.com/compose/compose-file/compose-file-v3/#entrypoint

    把 docker 的 entrypoint 换成一个脚本,用脚本先执行你的环境变量设置,再运行程序
    kennylam777
    kennylam777
    May 24, 2021❤️1
    14
    問題不是在 docker, 而是 sshd 下的 shell 沒有引用 container 的 enviroment

    上面一堆教 Dockerfile 等等的都被樓主帶錯了方向

    用 Google 的話秒查到答案了吧
    https://stackoverflow.com/questions/34630571/docker-env-variables-not-set-while-log-via-shell
    Macolor21
    Macolor21
    May 24, 2021
    15
    @kennylam777

    回看了一遍问题,刚想说自己忽略了(直接通过 docker exec 进去环境变量是正常的,python,pip 啥的都有,但通过 ssh 登录后,啥都没有, 使用场景又是在 ssh 登录的前提下使用的),既然你给出答案了,我也不赘述了
    css3
    css3
    OP
    May 24, 2021
    16
    @CivAx 不行,可以 build 但无法启动容器
    css3
    css3
    OP
    May 24, 2021❤️1
    17
    @kennylam777 多谢老铁,知道了根本原因,通过 statck 这个帖子解决了👍
    julyclyde
    julyclyde
    May 25, 2021
    18
    首先你 ssh 进容器就已经很不对了……
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1413 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 18:49 · PVG 02:49 · LAX 10:49 · JFK 13:49
    ♥ Do have faith in what you're doing.