testcontainers/testcontainers-node

.withEnvironment() not working as expected

Closed this issue · 2 comments

Expected Behaviour
That .withEnvironment function add to the containers the environment variables specified

Actual Behaviour
.withEnvironment() not adding the environment variables to the container

Testcontainer Logs

Initializing testContainers...
  testcontainers:containers [75d9a049ef5c] 2024/12/05 17:11:33 Pinging Docker... +0ms
  testcontainers:containers [75d9a049ef5c] 2024/12/05 17:11:33 Docker daemon is available! +1ms
  testcontainers:containers [75d9a049ef5c] 2024/12/05 17:11:33 Starting on port 8080... +0ms
  testcontainers:containers [75d9a049ef5c] 2024/12/05 17:11:33 Started! +0ms
  testcontainers:containers [75d9a049ef5c] 2024/12/05 17:11:33 New client connected: 172.17.0.1:54314 +10ms
  testcontainers:containers [75d9a049ef5c] 2024/12/05 17:11:33 Adding {"label":{"org.testcontainers.session-id=eaf04502d39d":true}} +1ms
 testcontainers:containers [061655271c0a] MYSQL_PASSWORD=test +481ms
  testcontainers:containers [061655271c0a] 0=M +0ms
  testcontainers:containers [061655271c0a] 1=Y +0ms
  testcontainers:containers [061655271c0a] 2=S +0ms
  testcontainers:containers [061655271c0a] 3=Q +0ms
  testcontainers:containers [061655271c0a] 4=L +0ms
  testcontainers:containers [061655271c0a] 5=_ +0ms
  testcontainers:containers [061655271c0a] 6=H +0ms
  testcontainers:containers [061655271c0a] 7=O +0ms
  testcontainers:containers [061655271c0a] 8=S +0ms
  testcontainers:containers [061655271c0a] 9=T +0ms
  testcontainers:containers [061655271c0a] HOSTNAME=061655271c0a +0ms
  testcontainers:containers [061655271c0a] MYSQL_DATABASE=test +0ms
  testcontainers:containers [061655271c0a] 13=E +0ms
  testcontainers:containers [061655271c0a] 12=M +0ms
  testcontainers:containers [061655271c0a] 11=A +0ms
  testcontainers:containers [061655271c0a] 10=N +0ms
  testcontainers:containers [061655271c0a] MYSQL_ROOT_PASSWORD=test +0ms
  testcontainers:containers [061655271c0a] PWD=/ +0ms
  testcontainers:containers [061655271c0a] HOME=/root +0ms
  testcontainers:containers [061655271c0a] MYSQL_MAJOR=8.0 +0ms
  testcontainers:containers [061655271c0a] GOSU_VERSION=1.14 +0ms
  testcontainers:containers [061655271c0a] MYSQL_USER=test +0ms
  testcontainers:containers [061655271c0a] MYSQL_VERSION=8.0.31-1.el8 +0ms
  testcontainers:containers [061655271c0a] SHLVL=1 +0ms
  testcontainers:containers [061655271c0a] PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin +0ms
  testcontainers:containers [061655271c0a] MYSQL_SHELL_VERSION=8.0.31-1.el8 +0ms
  testcontainers:containers [061655271c0a] _=/usr/bin/printenv +0ms

Steps to Reproduce
Define a simple container like this

const { MySqlContainer } = require('@testcontainers/mysql');

const mysqlContainer = await new MySqlContainer()
    .withRootPassword(sqlPassword)
    .withDatabase(sqlDB)
    .withEnvironment('MYSQL_HOSTNAME', sqlHostName) // Manually add the env var
    .withNetworkAliases(sqlHostName)
    .withExposedPorts(3306)
    .withReuse()
    .withCommand(['sh', '-c', 'printenv && sleep 10'])
    .start();
  • see the logs i shared, there is no MYSQL_HOSTNAME env var, happens in all my containers when adding env variables with this function, i have to use command line to do it

Environment Information

  • Operating System: MacOS Sonoma 14.6.1
  • Docker Version: Docker version 20.10.23, build 7155243
  • Node version: v18.12.1
  • Testcontainers version: 10.16.0

Okay, thanks, my bad copilot was suggesting the wrong format all the time

.withEnvironment({ MYSQL_HOSTNAME: sqlHostName }) is the correct line.

Thanks!