1. Introduction

In this tutorial we’ll study two methods for setting bash aliases for Docker containers in Dockerfile. First, we’ll explore a method that allows us to use the created aliases in an interactive mode. Finally, we’ll examine how to set aliases that can be used in a non-interactive manner.

2. Setting Aliases for Interactive Shells

In general, we can create aliases using the command below. The syntax indicates that we must replace with the command we want to execute when we type in the shell.

$ alias <name>="<value>"

In the context of Docker containers, we need to find an effective way to automatically execute the alias command inside the container. Therefore, a simple way to accomplish this is to add the alias command to the ~/.bashrc script, as it’s executed at each user login.

Concretely, we just need to add the following command to the Dockerfile:

RUN echo 'alias <name>="<value>"' >> ~/.bashrc

Let’s look at an example. Consider that we want to create a Docker image of Nginx with an alias that allows us to easily obtain the status of the Nginx service inside the container. In this case, the Dockerfile will look like this:

# Use the official Nginx base image
FROM nginx:latest

# Add an alias
RUN echo 'alias status="service nginx status"' >> ~/.bashrc

In practice, the RUN statement in the Dockerfile above makes executing command status in the shell equivalent to service nginx status.

So, considering that the Dockerfile is located in the current directory, we can proceed as follows to build the image:

$ docker build -t nginx-with-alias .

Next, we can validate the method by creating a container and running the status command inside it:

$ docker exec -it --name my-nginx nginx-with-alias bash
root@806b1c8c60ae:/# status
nginx is running.

However, note that this method only works in the interactive mode. If we try to use the status alias from outside the container, we’ll get an error similar to the following:

$ docker exec my-nginx status
OCI runtime exec failed: exec failed: unable to start container process: exec: "status": executable file not found in $PATH: unknown

Next, we’ll look at a method for the non-interactive mode.

3. Setting Aliases for the Non-interactive Mode

The alias command doesn’t work in non-interactive mode (i.e. when we execute commands from outside the container). In this case, we need to use an alternative approach that simulates the effect of aliases.

A simple and effective approach is to create scripts inside the /usr/bin/ directory. First, we need to add the shell command for which we want to create the alias to a script file in the /usr/bin/ directory. The file name must be the same as the alias name. After that, we just need to add execute permissions to the script file. Something like this:

RUN echo '#!/bin/bash\n<command>' > /usr/bin/ && \
    chmod +x /usr/bin/<alias>

In the case of the command using parameters, we need to add “$@” right after the command we insert into the script:

RUN echo '#!/bin/bash\n<command> "$@"' > /usr/bin/<alias> && \
    chmod +x /usr/bin/<alias>

As an example, let’s consider the same situation as in the previous section, in which we want to create the alias status for the command service nginx status. The Dokerfile will look like this:

# Use the official Nginx base image
FROM nginx:latest

# Add a script-based alias
RUN echo '#!/bin/bash\nservice nginx status' > /usr/bin/status && \
    chmod +x /usr/bin/status

We can build the image as follows:

$ docker build -t nginx-with-alias .

Then, create a container:

$ docker run -d --name my-nginx nginx-with-alias

Finally, we are able to use the created alias in a non-interactive mode:

$ docker exec my-nginx status
nginx is running.

4. Conclusion

In this tutorial, we learned two different methods for setting bash aliases for Docker containers in Dockerfile.

The first method is based on adding a RUN instruction in the Dockerfile that executes the alias command. The second one uses the RUN statement to execute a command that creates a script that simulates an alias. While the first method only allows us to use aliases in the shell interactively, the second also supports the non-interactive mode.