Movatterモバイル変換


[0]ホーム

URL:


SQLServerCentral Article
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] $  dirtotal 8-rw-r--r--   1 dba  staff  976 Sep  8  2018 README.mddrwxr-xr-x   6 dba  staff  192 Oct  8  2018 Directory_1drwxr-xr-x  21 dba  staff  672 Nov 26 13:11 Directory_2drwxr-xr-x   5 dba  staff  160 Jan 26 13:11 Directory_3drwxr-xr-x  22 dba  staff  704 Feb 26 13:18 Directory_4drwxr-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 formatalias dkpsa='docker ps -a --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"'# Removes a container, it requires the container name \ ID as parameteralias dkrm='docker rm -f'# Removes an image, it requires the image name \ ID as parameteralias dkrmi='docker rmi'# Lists all images by repository sorted by tag namealias dkimg='docker image ls --format "table {{.Repository}}\t{{.Tag}}\t{{.ID}}" | sort'# Lists all persistent volumesalias dkvlm='docker volume ls'# Diplays a container log, it requires the image name \ ID as parameteralias dklgs='docker logs'# Streams a container log, it requires the image name \ ID as parameteralias 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 parameteralias dkstrt='docker start'# Stops a container, it requires the image name \ ID as parameteralias 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] $  dkpsaNAMES               IMAGE                                             STATUS24HOP_DKR           mcr.microsoft.com/mssql/server:2017-CU14-ubuntu   Exited (0) 11 hours agoSQLSat830           mcr.microsoft.com/mssql/server:2017-CU13-ubuntu   Exited (0) 13 hours agoSimpleTalk          mcr.microsoft.com/mssql/server:2017-CU13-ubuntu   Exited (0) 4 weeks agomaster_2017_CU12    microsoft/mssql-server-linux:2017-latest          Exited (0) 5 months agomaster_2017_CU11    microsoft/mssql-server-linux:2017-CU11            Exited (0) 2 months agomaster_ag1          microsoft/mssql-server-linux:2017-latest          Exited (255) 2 months agomaster_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] $  dkimgREPOSITORY                       TAG                  IMAGE IDmcr.microsoft.com/mssql/server   2017-CU11            885d07287041mcr.microsoft.com/mssql/server   2017-CU12            4095d6d460cdmcr.microsoft.com/mssql/server   2017-CU12-ubuntu     4095d6d460cdmcr.microsoft.com/mssql/server   2017-CU13-ubuntu     314918ddaedfmcr.microsoft.com/mssql/server   2017-CU14-ubuntu     644ca19cb10dmcr.microsoft.com/mssql/server   2017-latest-ubuntu   314918ddaedfmcr.microsoft.com/mssql/server   2019-latest          7af596b24973microsoft/mssql-server-linux     2017-CU11            167af87c9fb5microsoft/mssql-server-linux     2017-CU12            4095d6d460cdmicrosoft/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.

    Ad for State of Database Landscape survey

    Rate

    (1)

    Log in orregister to rate

    You rated this post out of 5.Change rating

    Share

    Categories

    Tags

    Share

    Rate

    (1)

    Log in orregister to rate

    You rated this post out of 5.Change rating

    Related content

    SQLServerCentral Article

    Managing SQL Server containers using Docker SDK for Python - Part 2

    There are multiple ways to interact with the Docker daemon, as command line client API or GUI based tools like Kitematic. Docker also provides a SDK for Go and Python,  this SDK can be used to create and manage Docker containers the same way it works with the Docker engine API.

      Log in orregister to rate

      You rated this post out of 5.Change rating

      2019-09-16(first published:)

      1,877 reads

      SQLServerCentral Article

      Managing SQL Server containers using Docker SDK for Python - Part 1

      There are multiple ways to interact with the Docke...

        (2)

        Log in orregister to rate

        You rated this post out of 5.Change rating

        2019-06-13

        4,361 reads

        SQLServerCentral Article

        Creating SQL containers on Azure Data Studio Notebooks with Python

        Carlos Robles explains how to use Azure Data Studio Notebooks to create SQL containers with Python.

          (1)

          Log in orregister to rate

          You rated this post out of 5.Change rating

          2020-03-24

          2,810 reads

          SQLServerCentral Article

          Export/Import Data using BCP - SQL Server on Linux

          In this article you will learn how to use BCP for SQL Server on Linux to export and import data using the BCP command line utility.

            (5)

            Log in orregister to rate

            You rated this post out of 5.Change rating

            2020-01-30

            10,808 reads

            Technical Article

            Execute on DMVs without user having View Server State

            Wanted to share this script to the community just in case anyone out there may be search for a way to allow a hosted customer the ability to query certain DMVs for information without granting them View Server State. There are some other options out there, but this worked better for me after going through […]

              (1)

              Log in orregister to rate

              You rated this post out of 5.Change rating

              2020-09-24

              2,244 reads


              [8]ページ先頭

              ©2009-2025 Movatter.jp