Home > Software design >  Docker Run executing shell command can't access PATH environment variable
Docker Run executing shell command can't access PATH environment variable

Time:01-06

I'm using an official image from Microsoft which contains SQL tools used to interact with Microsoft SQL Servers. If I run the container interactively, I can run sqlcmd at the command line without any issue, because it is in the PATH variable:

$ docker run --rm -it -v $(pwd):/var/update/ -w /var/update  mcr.microsoft.com/mssql-tools:latest
root@df20bd19b982:/var/update# sqlcmd
Microsoft (R) SQL Server Command Line Tool
Version 13.1.0007.0 Linux
Copyright (c) 2012 Microsoft. All rights reserved.

usage: sqlcmd            [-U login id]          [-P password]
  [-S server or Dsn if -D is provided]
  [-H hostname]          [-E trusted connection]
  [-N Encrypt Connection][-C Trust Server Certificate]
  [-d use database name] [-l login timeout]     [-t query timeout]
  [-h headers]           [-s colseparator]      [-w screen width]
  [-a packetsize]        [-e echo input]        [-I Enable Quoted Identifiers]
  [-c cmdend]
  [-q "cmdline query"]   [-Q "cmdline query" and exit]
  [-m errorlevel]        [-V severitylevel]     [-W remove trailing spaces]
  [-u unicode output]    [-r[0|1] msgs to stderr]
  [-i inputfile]         [-o outputfile]
  [-k[1|2] remove[replace] control characters]
  [-y variable length type display width]
  [-Y fixed length type display width]
  [-p[1] print statistics[colon format]]
  [-R use client regional setting]
  [-K application intent]
  [-M multisubnet failover]
  [-b On error batch abort]
  [-D Dsn flag, indicate -S is Dsn]
  [-X[1] disable commands, startup script, environment variables [and exit]]
  [-x disable variable substitution]
  [-? show syntax summary]

root@b33a916d4230:/var/update# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/mssql-tools/bin
root@b33a916d4230:/var/update#

sqlcmd is present in /opt/mssql-tools/bin/ folder which is part of the PATH env. variable.

but If I try to execute the sqlcmd command at the docker run... bash -c 'sqlcmd', it won't find it. I echoed PATH environment variable at the same command line and found that its path i.e /opt/mssql-tools/bin is already in the PATH.

$ docker run --rm -it -v $(pwd):/var/update/ -w /var/update  mcr.microsoft.com/mssql-tools:latest bash -c "sqlcmd"
bash: sqlcmd: command not found

And to see the PATH env. variable, I did the following:

$docker run --rm -it -v $(pwd):/var/update/ -w /var/update  mcr.microsoft.com/mssql-tools:latest bash -c 'echo $PATH'

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Question 1: Why Path Variable is different in case we use bash -c 'commands'?
Question 2: If bash -c or sh -c creates a new shell, how to execute shell commands with the container's environment variables especially the PATH environment variable.

CodePudding user response:

When you run an interactive shell as root, it runs the commands from /root/.bashrc, which (in this particular image) include

export PATH="$PATH:/opt/mssql-tools/bin"

A better Docker image would have that setting in the Dockerfile itself, which exports it to all users of the image. You can build an image like that yourself easily.

FROM mcr.microsoft.com/mssql-tools:latest
ENV PATH="$PATH:/opt/mssql-tools/bin"

(Also, the export is superfluous; the variable is already exported by the shell.)

If you don't want to mess with the image, try

docker run --rm -it -v $(pwd):/var/update/ -w /var/update \
       mcr.microsoft.com/mssql-tools:latest \
       bash -c 'PATH=$PATH:/opt/mssql/bin sqlcmd'
  • Related