Thanks for the reply. Some clarification:
The env variables are being created by docker in the link step, so I don't
want to hard code them anywhere, since I guess the point is that they could
get remapped if I have multiple machines using the same port. However, the
vars are definitely visible from the shell, so I'm not worried about that
part:
-> vagrant docker-run -- sh -c 'exec env | grep DB_PORT_5432_TCP_PORT'
--SNIP--
db: DB_PORT_5432_TCP_PORT=5432
So I think I really do want to find a way to evaluate the dollar signs in
the container.
The more I think about this, the more I think this is actually just a bug
in docker-run. After all, it knows I'm trying to run something on the
container, not the VM. The whole point of putting it inside a quoted
command run by a shell is so that it doesn't get interpreted until the exec
actually runs (for example, that's why OSX doesn't interpret all the way
back at the first layer). So if Vagrant just passes the command through
as-is, the host VM won't even get a chance to break it; it's just a quoted
string so there shouldn't be any multi-level escape issues. The problem is
that vagrant rewrites my command before executing it on the VM like so:
Command: "docker" "run" "--name" "zensight_db_1405554189" "--link" "db:db"
"-v" "/var/lib/docker/docker_1405554189_67405:/vagrant" "library/postgres"
"sh" "-c" "exec psql -h "\""$DB_PORT_5432_TCP_ADDR"\"" -p
"\""$DB_PORT_5432_TCP_PORT"\"" -U postgres"
I'll be the first to admit that I'm easily confused by shell quotes, but I
think that's what's breaking it. It ought to preserve my single quote
instead of adding more quotes and escaped double quotes. Observe:
-> export THING=stuff
-> echo "\""$THING"\""
"stuff"
-> echo '$THING'
$THING
So it's that last case that we want, which is how I wrote it. Should I file
a bug or am I still missing something?
On Wednesday, July 16, 2014 5:53:18 PM UTC-4, Alvaro Miranda Aguilera wrote:
>
> hello.
>
> with 3 layers as you have notice the shell expansion and the evaluation of
> the command is happening before the layer you want to do..
>
> is a bit hard to control 3 layers, you will have to use \ to make some
> characters literal and hope will get evaluated inside the proper layer.
>
> plus if those variables like DB_PORT_5432_TCP_ADDR exists on a file that
> get initiated in the shell login, not all the logins are the same, so
> .bash_profile is not read always
>
> My suggestion will be
>
> create a file that have those variables set
>
> say
>
> pgsql.env
>
> inside
>
> DB_PORT_5432_TCP_ADDR=awesomehostname
>
> then in .bash_profile you can use
>
> . ~/pgsql
>
> and that will use the variables
>
> and on your command you could do
>
> . pgsql ; command $DB_PORT_5432_TCP_ADDR
>
>
>
> Example:
>
> mini:~ alvarom$ cat pgsql.env
>
> DB_PORT_5432_TCP_ADDR="awesomeserver"
>
> mini:~ alvarom$ ssh localhost '. pgsql.env ; echo $DB_PORT_5432_TCP_ADDR'
>
> awesomeserver
>
> mini:~ alvarom$
>
>
>
>
>
>
>
>
>
>
> On Thu, Jul 17, 2014 at 5:16 AM, Isaac Cambron <[email protected]
> <javascript:>> wrote:
>
>> I have what I'm sure is a dumb problem, and likely betrays some
>> misunderstanding about Vagrant, Docker, shells, or possibly all three. But
>> here goes: I'm running Vagrant with a Docker provider, using the default
>> boot2docker host VM. What I'm trying to do is run some commands, but the
>> problem is that my shell variables are being interpreted in the host VM's
>> environment instead of the container's. To demonstrate:
>>
>> -> vagrant docker-run db -- whoami
>> ==> db: Docker host is required. One will be created if necessary...
>> --SNIP--
>> db: root
>>
>> That's correct; my Postgres container runs as root. But:
>>
>> -> vagrant docker-run db -- sh -c 'echo $(whoami)'
>> ==> db: Docker host is required. One will be created if necessary...
>> --SNIP--
>> db: docker
>>
>> Which is silly. It looks like the $(whoami) is being evaluated by the
>> host VM, where the user is named docker. That example is contrived, but the
>> underlying problem keeps me from accessing environment variables that do
>> matter:
>>
>> -> vagrant docker-run db -- sh -c 'exec psql -h "$DB_PORT_5432_TCP_ADDR"
>> -p "$DB_PORT_5432_TCP_PORT" -U postgres'
>>
>> Which doesn't work because (as I understand it) those vars are getting
>> evaluated to nothing in the host, which of course has no idea what port I
>> have PG set up on.
>>
>> Any ideas on how to fix this? Perhaps there's some extra level of
>> indirection I need to put into the command itself, or maybe I'm just
>> missing something.
>>
>> Thanks,
>> Isaac
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Vagrant" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.