SQLServerCentral Article

Creating Aliases for Docker Commands in Linux

,

Introduction

There are situations when we need to execute the same command line instruction over and over again,  it becomes worse if the command in question has arguments, furthermore if we are forced to modify the output to make it readable. Most of us end up creating some kind of cheat sheet to save all those "complex" commands, so we have them handy in case we need to run it again.

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.

Creating Aliases

We have a couple of options when creating aliases:

  • Temporary
  • Permanent

Temporary aliases are created in the current terminal session, that means that if we close this session or we open a new open those aliases will not be available.

Permanent aliases in the other hand are persistent no matter if the current terminal session is ended or even if the host machine is rebooted. The aliases information has to be written and saved into the user’s shell configuration profile file. In my case I'm using macOS so all the aliases changes has to be saved in the ~/.bash_profile, if you are a Linux user you should save the aliases in the ~/.bashrc.

For both Linux and MacOS, these user’s shell configuration profile files are typically located in the $HOME directory.

Aliases in Action

Imagine I want to list all the files within a directory, including the creation date and owner. In Windows command prompt we get this file information this running the "dir" command, but unfortunately the is is not known bash command in Linux \ Unix systems, if you try to run "dir" it will return an error message like this:

[dba mastery] $  dir
-bash: dir: command not found

The solution is to create an alias for the "dir" command, using "ls -ltr" we will be able to display the file information the way we want (files by creation date and owner), so let's create an alias for that:

alias dir = "ls -ltr"

As you can see it is very simple, we have to run the command "alias" followed by a short name in this case "dir" followed by the "=" sign, then finally we provide the command line instruction "ls -ltr" that will execute as the command when calling the alias "dir".

This is what happens when the "dir" alias is called:

[dba mastery] $  dir
total 8
-rw-r--r--   1 dba  staff  976 Sep  8  2018 README.md
drwxr-xr-x   6 dba  staff  192 Oct  8  2018 Directory_1
drwxr-xr-x  21 dba  staff  672 Nov 26 13:11 Directory_2
drwxr-xr-x   5 dba  staff  160 Jan 26 13:11 Directory_3
drwxr-xr-x  22 dba  staff  704 Feb 26 13:18 Directory_4
drwxr-xr-x   5 dba  staff  160 Mar 24 03:06 Directory_5

It is time to focus on Docker now, let's create some aliases using the previous example.

Creating Aliases for Common Docker Commands

Now that you know the basics of aliases, let's create some for the most common Docker commands. First thing first, we have to navigate to our user home directory (cd $HOME). Once we are at this location simply edit the .bash_profile or .bashrc file using the editor of your choice, in my case I will use "vim" as follows:

[dba mastery] $  cd $HOME
[dba mastery] $  vi .bash_profile
# Docker aliases (shortcuts)
# List all containers by status using custom format
alias dkpsa='docker ps -a --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"'
# Removes a container, it requires the container name \ ID as parameter
alias dkrm='docker rm -f'
# Removes an image, it requires the image name \ ID as parameter
alias dkrmi='docker rmi'
# Lists all images by repository sorted by tag name
alias dkimg='docker image ls --format "table {{.Repository}}\t{{.Tag}}\t{{.ID}}" | sort'
# Lists all persistent volumes
alias dkvlm='docker volume ls'
# Diplays a container log, it requires the image name \ ID as parameter
alias dklgs='docker logs'
# Streams a container log, it requires the image name \ ID as parameter
alias dklgsf='docker logs -f'
# Initiates a session withing a container, it requires the image name \ ID as parameter followed by the word "bash"
alias dkterm='docker exec -it'
# Starts a container, it requires the image name \ ID as parameter
alias dkstrt='docker start'
# Stops a container, it requires the image name \ ID as parameter
alias dkstp='docker stop'
~
:wq

Please keep in mind, you can use any name\label for the aliases. I used the "dk" prefix in all the aliases because it was convenient for me but you can use what might be easier to memory \ write for you.

It is time to check how the aliases are working, for this example I will display all the containers by status using the "dkpsa" alias:

[dba mastery] $  dkpsa
NAMES               IMAGE                                             STATUS
24HOP_DKR           mcr.microsoft.com/mssql/server:2017-CU14-ubuntu   Exited (0) 11 hours ago
SQLSat830           mcr.microsoft.com/mssql/server:2017-CU13-ubuntu   Exited (0) 13 hours ago
SimpleTalk          mcr.microsoft.com/mssql/server:2017-CU13-ubuntu   Exited (0) 4 weeks ago
master_2017_CU12    microsoft/mssql-server-linux:2017-latest          Exited (0) 5 months ago
master_2017_CU11    microsoft/mssql-server-linux:2017-CU11            Exited (0) 2 months ago
master_ag1          microsoft/mssql-server-linux:2017-latest          Exited (255) 2 months ago
master_ag2          microsoft/mssql-server-linux:2017-latest          Exited (137) 5 months ago

Other example, checking what images I have in my Docker local repository using the "dkimg" alias:

[dba mastery] $  dkimg
REPOSITORY                       TAG                  IMAGE ID
mcr.microsoft.com/mssql/server   2017-CU11            885d07287041
mcr.microsoft.com/mssql/server   2017-CU12            4095d6d460cd
mcr.microsoft.com/mssql/server   2017-CU12-ubuntu     4095d6d460cd
mcr.microsoft.com/mssql/server   2017-CU13-ubuntu     314918ddaedf
mcr.microsoft.com/mssql/server   2017-CU14-ubuntu     644ca19cb10d
mcr.microsoft.com/mssql/server   2017-latest-ubuntu   314918ddaedf
mcr.microsoft.com/mssql/server   2019-latest          7af596b24973
microsoft/mssql-server-linux     2017-CU11            167af87c9fb5
microsoft/mssql-server-linux     2017-CU12            4095d6d460cd
microsoft/mssql-server-linux     2017-latest          4095d6d460cd

Conclusion

The creation of aliases can help you to save some time and effort when executing repetitive bash instructions, in this article we learned how Linux \ Unix aliases works, types and also an example of how to create some aliases for Docker to make our administration tasks a little bit easier.

Another good case of the use of aliases can be automation, I find easy to incorporate short commands rather in a script than a complex set of instructions; also improves the script readability in some cases.

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating