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?
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With