Handy Docker Aliasesby@cjus
17,882 reads
17,882 reads

Handy Docker Aliases

by Carlos JustinianoJuly 6th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

If you’ve worked with <a href="" target="_blank">Docker</a> at the command line, you’ve undoubtedly written command shortcuts or have at least considered doing so.

Company Mentioned

Mention Thumbnail
featured image - Handy Docker Aliases
Carlos Justiniano HackerNoon profile picture

Optimize your docker command line workflow using aliases

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.

dkps shell script

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 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 .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.

Abbreviated docker ps output

And docker ps | grep redis returns:

Abbreviated output

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.

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 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.

.bash_aliases script

Here’s the command in action:

Abbreviated docker ps output

Another useful alias is the dkstats (docker stats) command:

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 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.