gpt4 book ai didi

docker - docker-compose不初始化容器环境变量

转载 作者:行者123 更新时间:2023-12-02 19:04:00 25 4
gpt4 key购买 nike

我正在尝试执行简单的代码。

docker-compose.yml

version: '3.7'

services:
test-environment:
image: alpine:3.9
environment:
VAR1: 'variable 1'
VAR2: '222'
command: echo variable1 = $VAR1; variable2 = $VAR2

预期行为:在输出中看到
variable1 = variable 1; variable2 = 222

但是之后
docker-compose up

我下一个:
WARNING: The VAR1 variable is not set. Defaulting to a blank string.
WARNING: The VAR2 variable is not set. Defaulting to a blank string.
Recreating project_test-environment_1 ... done
Attaching to project_test-environment_1
test-environment_1 | variable1 = ; variable2 =
project_test-environment_1 exited with code 0

似乎环境变量未初始化。使用替代语法
environment:
- VAR1='variable 1'
- VAR2=222

也得到相同的结果。

docker 信息输出:
Containers: 3
Running: 0
Paused: 0
Stopped: 3
Images: 5
Server Version: 18.09.2
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries
splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9754871865f7fe2f4e74d43e2fc7ccd237edcbce
runc version: 09c8266bf2fcf9519a651b04ae54c967b9ab86ec
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 4.9.125-linuxkit
Operating System: Docker for Windows
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 973.8MiB
Name: linuxkit-00155d694b07
ID: NA32:YPM3:D5GL:JQBM:H4BN:LE2B:4HLN:5B4A:AI7T:6SDS:LSVZ:P74L
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
File Descriptors: 22
Goroutines: 47
System Time: 2019-05-14T08:44:02.5767035Z
EventsListeners: 1
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine

有没有人有解决问题的想法?

附言对不起我的英语不好。

最佳答案

enter image description here

version: '3'
services:
test-environment:
image: alpine:3.9
environment:
VAR1: 'variable 1'
VAR2: '222'
command: '/bin/sh -c "echo $$VAR1 $$VAR2"'

要点:
  • variable-substitution
  • docker inspect命令检查是否已为该容器正确配置env变量。
  • 我使用版本3,因为我的docker-compose不支持您使用的版本。
  • .env文件功能仅在您使用docker-compose up命令时有效,而不适用于docker stack deploy。
  • 当您的配置需要文字美元符号时,可以使用$$(美元符号)。这也可以防止Compose插值,因此$$可以让您引用您不想由Compose处理的环境变量。
  • 如果您忘记并使用一个美元符号($),Compose会将值解释为环境变量并警告您:

  • WARNING: The VAR1 variable is not set. Defaulting to a blank string. WARNING: The VAR2 variable is not set. Defaulting to a blank string.

    关于docker - docker-compose不初始化容器环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56126402/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com