Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
If youâve worked with Docker at the command line, youâve undoubtedly written command shortcuts or have at least considered doing so.
In this post Iâm assuming youâre somewhat comfortable using docker commands and are perhaps a bit interested in potentially useful aliases. If youâre new to Docker - then this might not be a good place to start.
This post is about time-saving shortcuts or aliases for longer, unabbreviated commands. âcause letâs face it:
âAinât nobody got time to type out the whole damn command!â
A shortcut can be implemented in several ways. Two that weâll consider here is the bash shell alias and the shell script file. Any shortcut Iâll share can be placed in a script file or an aliases file. Each method has its pros and cons.
Hereâs a handy dkps (docker ps) shortcut placed in a shell script. If you opt for this approach then youâll end up with a collection of scripts that youâll need to copy to a globally accessible path such as /usr/shared/bin, use a PATH or something similar. You may also need system privileges in order to get it just right.
The other approach is to place alias commands in your .bash_profile file found in your home directory. That makes the shortcuts portable without requiring special permissions.
.bash_aliases scriptIf youâre new to bash aliases checkout this intro.
To keep things clean Iâll import my docker related aliases rather than add them directly to my .bash_profile or .bash_aliases script. In my .bash_profile script Iâll simply add the following to load my new .docker_aliases file.
In my actual .bash_profile script I load other aliases such as a .git_aliases script.
So you donât have to type all this in, a sample .docker_aliases file is available at the end of this article.
This method is also quite portable, as youâll be able to share your docker aliases with friends and family.
Before introducing my own handy docker aliases letâs create a sample set of containers that we can use to test aliases against. Weâll do that using the Docker Stack commands and two well-known services, Redis and MongoDB. Donât worry if youâre not familiar with those, we wonât actually use themâââthink of them as just real-world placeholders.
Launching our test stack simply requires doing a docker stack deploy and passing the compose script above. We end the command with the name of our stack which weâll call âtestâ.
Once the stack loads we can verify it using the docker ps command:
Note: For the sake of brevity, Iâll abbreviate the output of some commands. This has an added benefit of creating smaller images. So keep in mind that output of the docker ps command above is much longer than shown.
We can tear down our stack using the docker stack rm command.
Easy Peasy, right?
Iâll use the setup above when showing screenshots in the remainder of this post.
Now on to our aliases⊠this better be good right?
The first aliases weâll look at simply provide abbreviations for common docker commands.
Right off the bat, consider the time saving every time you replace docker with dk for commands like docker ps or when working with services and using dks for docker service commands like docker service ls. The savings of typing dks ls add up!
So instead of typing docker: docker logs you type:Â dkl
docker logs b7a8 becomes dkl b7a8
Use dklfto follow a log. So rather than docker logs -f b7a8 you can simply use dklf b7a8
Another handy logging alias is the dkln (docker log by name)Â command.
It evaluates the piped commands within the backticks (`) and utilizes the result as a parameter to the docker logs command. The grep command uses the first parameter to filter the results of the docker ps command. And finally, the awk command takes the first field of the output as the value parameter.
Ok, that might be confusing. Letâs take a closer look.
The docker ps command returns a list of running containers.
And docker ps | grep redis returns:
And finally, docker ps | grep redis | awk '{print $1}' return the container ID for Redis: f5f0ed387073
This allows us to view the log for any container by name.
Naturally, youâll need to make sure you donât have multiple containers which match a name. If you do, then simply resort to the dkl command with a container ID.
A key takeaway in the example above is that you can build some pretty powerful aliases by combining shell commands. Weâll see another example of this later in this post, when we look at an alias for building and publishing containers.
Viewing the status of our running containers is a vital part of building and testing containerized services. The following aliases make it easier for us to do this.
Earlier in this post we looked at the dkps (Docker PS)Â command.
Hereâs the command in action:
Another useful alias is the dkstats (docker stats)Â command:
This command tests whether a parameter is supplied and if so applies a grep filter.
That allows us to see stats for all containers or to filter by a a specific container name.
The dktop command presents a top-like display, showing memory, CPU, network I/O and block I/O.
The actual alias is surprisingly simple:
Youâre free to customize it with the values youâd rather see. I chose a basic set in order to have the display fit neatly in a mult-pane iTerm2Â screen.
In the process of building containers itâs sometimes necessary to enter the container to have a look around. Doing so typically involves:
- docker ps to view a list of container IDs
- docker exec -it {containerID} /bin/sh
Using our aliases this becomes:
- dkps
- dke {containerID}
Hereâs a video example:
Sometimes itâs necessary to restart a service. The dksb (docker service bounce) command allows us to do just that.
The non-alias method of doing this requires using the docker service scale command:
$ docker service scale test_redis=0$ docker service scale test_redis=1
Using dksb we simply type:
$ dksb test_redis 1
The last thing weâll look at is an aliase for building and publishing docker containers. While this process typically involves a two-phase operation, docker build / docker push, there other tasks you might want to do to further automate the process. In my case, I build docker containers which hosts NodeJS Microservices. Part of my local build involves checking whether a .dockerignore file exists, then looking inside a Node package.json file to extract the name and version of a project. The name and version are then used to form a docker label.
Hereâs what a dkp (docker publish) alias looks like:
The above alias uses jq (a command line JSON processor) to pull version information from a package.json file.
Using dkp is simply a matter of entering a node project directory and typing dkp followed by the docker hub repository name. In the example below Iâm building and publishing the HydraRouter docker container.
I hope youâve found some handy docker aliases in this post or that at least itâs inspired you to optimize your command line workflow using aliases. Cause rememberâââAinât nobody got timeâŠ
Hereâs the full .docker_aliases script, including a few bonus commands we didnât cover in this post.
â
Thanks for reading! If you like what you read, hold the clap button below so that others may find this. You can also follow me on Twitter.
Handy Docker Aliases was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.