Optimize your docker command line workflow using aliases If you’ve worked with at the command line, you’ve undoubtedly written command shortcuts or have at least considered doing so. Docker In this post I’m assuming you’re somewhat comfortable using docker commands and are perhaps a bit interested in potentially useful . If you’re new to Docker - then this might not be a good place to start. aliases 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 (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 , use a PATH or something similar. You may also need system privileges in order to get it just right. dkps /usr/shared/bin dkps shell script The other approach is to place alias commands in your file found in your home directory. That makes the shortcuts portable without requiring special permissions. .bash_profile .bash_aliases script If 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 or script. In my .bash_profile script I’ll simply add the following to load my new file. .bash_profile .bash_aliases .docker_aliases In my actual script I load other aliases such as a script. .bash_profile .git_aliases So you don’t have to type all this in, a sample file is available at the end of this article. .docker_aliases 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 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. Docker Stack commands Launching our test stack simply requires doing a and passing the compose script above. We end the command with the name of our stack which we’ll call “test”. docker stack deploy Once the stack loads we can verify it using the command: docker ps 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 command. docker stack rm , right? Easy Peasy 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 with for commands like or when working with services and using for docker service commands like . The savings of typing add up! docker dk docker ps dks docker service ls dks ls So instead of typing docker: you type: docker logs dkl becomes docker logs b7a8 dkl b7a8 Use to follow a log. So rather than you can simply use dklf docker logs -f b7a8 dklf b7a8 Another handy logging alias is the (docker log by name) command. dkln It evaluates the piped commands within the backticks (`) and utilizes the result as a parameter to the command. The command uses the first parameter to filter the results of the command. And finally, the command takes the first field of the output as the value parameter. docker logs grep docker ps awk Ok, that might be confusing. Let’s take a closer look. The command returns a list of running containers. docker ps Abbreviated docker ps output And returns: docker ps | grep redis Abbreviated output And finally, return the container ID for Redis: f5f0ed387073 docker ps | grep redis | awk '{print $1}' This allows us to view the log for any container by name. Abbreviated docker ps output 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 command with a container ID. dkl 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 (Docker PS) command. dkps .bash_aliases script Here’s the command in action: Abbreviated docker ps output Another useful alias is the (docker stats) command: dkstats This command tests whether a parameter is supplied and if so applies a grep filter. Abbreviated docker ps output That allows us to see stats for all containers or to filter by a a specific container name. The command presents a top-like display, showing memory, CPU, network I/O and block I/O. dktop The actual alias is surprisingly simple: You’re free to 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. customize it 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 (docker service bounce) command allows us to do just that. dksb The non-alias method of doing this requires using the command: docker service scale $ docker service scale test_redis=0$ docker service scale test_redis=1 Using we simply type: dksb $ 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 (docker publish) alias looks like: dkp The above alias uses (a command line JSON processor) to pull version information from a package.json file. jq Using is simply a matter of entering a node project directory and typing followed by the docker hub repository name. In the example below I’m building and publishing the docker container. dkp dkp HydraRouter 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 script, including a few bonus commands we didn’t cover in this post. .docker_aliases — 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 .