Cyril Ben
Cyril Ben

Reputation: 23

How to write parameterized Bash alias for Docker command

I'm currently facing an issue.

I just pulled a Vim container to use it on a CoreOs system. I try to create an alias to launch the Vim container instead of the one already present (principally because on this one I can install different plugins)

But for the moment it doesn't works and I don't know why.
The container has a Volume mount on /home/dev

My alias :

alias dockervim="docker run -ti --rm -v \"\$(pwd)\":/home/dev vimpadawan bash -c "vim \"\$1\"""

But when I write dockervim file_name it launches Vim but not with the file.

Someone has any ideas ?

Docker version

Client version: 1.6.2 
Client API version: 1.18 
Go version (client): go1.4.2 
Git commit (client): 7c8fca2-dirty 
OS/Arch (client): linux/amd64 Server version: 1.6.2 Server 
API version: 1.18 
Go version (server): go1.4.2 
Git commit (server): 7c8fca2-dirty 
OS/Arch (server): linux/amd64

Thank you !

EDIT 1 :

With this command and a start.sh script, I can do what I want :

docker run --rm -ti -v $(pwd):/root/src -e FILENAME=Dockerfile vimpada

but when I try to make an alias of it, it shows this error :

exec: "Dockerfile": executable file not found in $PATH FATA[0000] Error response from daemon: Cannot start container f579e5cd5bc61ee5da3b5cbeaf2a645c6183914739464cc4bf605202417467d9:
[8] System error: exec: "Dockerfile": executable file not found in $PATH

EDIT 2 :

Just saw your answer helmbert, but it actually shows the same error as mine for your first function:

[8] System error: exec: ".bashrc": executable file not found in $PATH

But your second one works PERFECTLY ! Thanks a lot :)

Upvotes: 2

Views: 2369

Answers (1)

helmbert
helmbert

Reputation: 38064

Bash aliases do not take arguments. You can try declaring a bash function, though:

function dockervim() {
    docker run -ti --rm -v "${PWD}":/home/dev vimpadawan vim "${1}"
}

Then simply call with dockervim <filename>.

Note that this solution only works with files in the current working directory. You should be able make it bomb-proof using the following function:

function dockervim() {
    filename=$(readlink -f $1)
    docker run -ti --rm -v "${filename}":/tmp/file-to-edit vimpadawan vim /tmp/file-to-edit
}

Upvotes: 3

Related Questions