Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker exec linux terminal create alias

I have a running and detached container. I want to create a command alias there before attaching to that container.

When I am attached to the container and I type:

alias bar='foo'

an alias is created, and might be checked by:

alias

command.

but if I want to do the same by docker exec command ie this way

docker exec -it <container-name> /bin/bash -c "alias bar='foo'"

it does not work, probably because when I'm attached to the container and type into its terminal

/bin/bash -c "alias bar='foo'"

it does not work as well.

Do you know how to modify alias bar='foo' so it works together with docker exec command applied to a detached container?

like image 790
Jimmix Avatar asked Jan 21 '19 20:01

Jimmix


People also ask

What is alias in docker?

We can save ourselves time and effort using an "alias command" to stop typing or copying the same command again and again, aliases makes possible to execute a command or set of commands by using a pre-defined "string" that we can customize the way we want.

Which file can be used to create aliases in Linux?

Create a Permanent Alias in Linux To make an alias permanent, you need to add it to your shell configuration file. Depending on the type of shell you are using, use: Bash shell: ~/. bashrc.


1 Answers

Alias in your bashrc file does not directly accept parameters. Although in your case you will have to create a function and alias that.

You can add following on your ~/.bashrc

dexec() {
    docker exec -it "$1" /bin/bash
    echo "$1"
}

you can also use /bin/sh instead of /bin/bash

and don't forget to do

source ~/.bashrc

ideally, you can do something like this

dexec <container_id/container_name>
like image 80
sonus Avatar answered Sep 29 '22 14:09

sonus