phusion / baseimage-docker

A minimal Ubuntu base image modified for Docker-friendliness
http://phusion.github.io/baseimage-docker/
MIT License
8.96k stars 1.09k forks source link

How to run one or two commands? #91

Closed disposable-ksa98 closed 10 years ago

disposable-ksa98 commented 10 years ago

I tried this and it works:

docker run -i -t myimage my_init -- echo 'lol'

But then I tried this and didn't work:

$ docker run -i -t myimage my_init -- cd /srv
cd exited with exit code 32512

What I really want to do is go to certain directory and then execute certain script (the script uses relative paths so I can't execute it directly, I have to cd to the directory first). The cd part already is supposed to happen in ~/.bashrc, but apparently it hasn't been executed yet. Is there a way to make my command execute after ~/.bashrc? If that's not possible, I would like to simply run two commands: cd into that directory and then execute the script.

kingdonb commented 10 years ago

my_init is supposed to run all of the runit scripts, not just one script...

Why don't you create a runit script and put it in image/runit? source ~/.bashrc at the beginning of your script if you need it On Jun 12, 2014 10:58 PM, "disposable-ksa98" notifications@github.com wrote:

I tried this and it works:

docker run -i -t myimage my_init -- echo 'lol'

But then I tried this and didn't work:

docker run -i -t myimage my_init -- cd /srv

What I really want to do is go to certain directory and then execute certain script. The cd part already is supposed to happen in ~/.bashrc. Is there a way to make my command execute after ~/.bashrc? If that's not possible, I would like to simply run two commands: cd into that directory and then execute the script.

— Reply to this email directly or view it on GitHub https://github.com/phusion/baseimage-docker/issues/91.

FooBarWidget commented 10 years ago

This is not a baseimage-docker issue. You are misunderstanding how shell commands work. The arguments you pass to my_init are run directly by passing them to the operating system's exec() system call, so they don't constitute a shell command. If you want to change the working directory first, you need to run a shell command -- in other words, you need to wrap things in a bash call. Like this:

docker run -i -t myimage my_init -- /bin/bash -c 'cd /somewhere && some command here'

See also this: http://superuser.com/questions/241129/why-wont-sudo-cd-work

disposable-ksa98 commented 10 years ago

@FooBarWidget Thanks, that works. So how would you do to run .bashrc and also "some command"?

FooBarWidget commented 10 years ago

To run bashrc, pass -l to bash.