Getting to know the Shell command in the IBM Cloud Developer Tools CLI

5 min read

By: Andrew Trice

Getting to know the Shell command in the IBM Cloud Developer Tools CLI

The recent version 1.1 release of the IBM Cloud Developer Tools CLI introduced some great new features to help developers become even more productive… A new project creation flow, new helper utility methods, and multi-container application support, just to name a few. In this post I’d like to focus on one new feature in particular: the shell command.

The shell command enables developers to easily open an interactive shell inside of the container that is running your application, or easily execute commands inside of the running container.

Wait, what does that mean?

To understand how the shell command helps developers, lets first examine the general usage of the IBM Cloud Developer Tools CLI. The IBM Cloud Developer Tools CLI provides an extensible command driven approach for creating, developing, and deploying a web project. The bx dev create command walks you through a series of questions, and then generates production-grade starter code for you to base your applications on. This lets you choose the language, patterns, and services of your liking and within minutes you have a production-quality project ready to go, complete with Dockerfiles, and deployment assets to both Cloud Foundry and Kubernetes environments. The bx dev build command compiles your application and builds Docker images for you, the bx dev run or bx dev deploy commands let you quickly run your application locally within a Docker container that mirrors the container that will be used in Cloud deployment, and the bx dev deploy command streamlines the process of deploying your application to either a Cloud Foundry of Kubernetes environment with one simple command. You can read more about each of these commands in greater detail in the IBM Cloud Developer Tools documentation.

Now that we’ve refreshed our memory on what the IBM Cloud Developer Tools CLI does, you may be wondering: How does the new shell command fit into the mix?

One common request we’ve received for the IBM Cloud Developer Tools CLI is to have a more flexible approach that allows you to change change environment variables, manually start or override the server process, or easily execute specific commands, all inside of a local docker container instance that is as close as possible to the production environment. The shell command allows us to accomplish all of this.

There are two ways that you can use the shell command. You can either open an interactive shell, or execute commands inside of the shell, without being inside of an interactive shell. Let’s look at these two use cases in a bit more depth.

Interactive Shell

You can launch an interactive shell into your application’s container just by running the bx dev shellcommand.

Interactive Shell

Optionally, you can specify the shell command to target your application’s run container or tools container, or even specify an arbitrary container name using the --container-name argument. If you don’t identify a specific target, the shell command will target the default container specified in the cli-config.yml file’s container-shell-target value, and if that value doesn’t exist, the shell command will target the tools container by default.

For example:

  • bx dev shell – opens an interactive shell targeting the default container

  • bx dev shell run – opens an interactive shell targeting the application’s run container

  • bx dev shell tools – opens an interactive shell targeting the application’s tools container

  • bx dev shell --container-name foo – opens an interactive shell targeting an already running container named “foo”

If the target container is already running, the shell command will open an interactive shell to the already running container. If the target container is not already running, then it will open an interactive shell to the container, but will override the default command/entrypoint in the container’s Dockerfile to target the OS shell. This is exactly what allows you to open up an interactive shell to the container, but not start the server process.

Why would you want to start a shell without starting the server process?

Developers often run into cases where they want to change environment variables before starting a server. Lets think of it from the perspective of a developer… imagine that you’re working on a Node.js project, and you’ve got your Dockerfile configured so that it starts the server by default. You want to develop your project using nodemon for so it automatically reloads changes to the app source code, but you don’t want to change anything in your Dockerfile.

Interactive Shell

You can open up a shell into the application’s container, install nodemon (if not already installed), and then run nodemon. These steps will launch nodemon inside of the local container, but nothing in your Dockerfile changes.

Inline Command Execution

In addition to opening up an interactive shell, you can also specify arguments to the shell command, and these arguments will be passed through and executed inside of the Docker container.

Inline Command Execution

This is much like how you would use docker exec to execute a command, but you don’t necessarily need to know the name of your container.

  • bx dev shell nodemon – will use the inline command to launch “nodemon” using in the application’s default container

  • bx dev shell "ps -e" – will use the inline command to launch “ps -e” using in the application’s default container (note the additional pass-through argument should be within quotes.

  • bx dev shell tools nodemon – will use the inline command to launch “nodemon” using in the application’s tools container

  • bx dev shell run nodemon – will use the inline command to launch “nodemon” using in the application’s run container

What Next?

If you don’t already have the latest version of the IBM Cloud Developer Tools CLI, then go get it! Create a new project or enable an existing one and enjoy a streamlined developer experience to get your applications on the IBM Cloud.

Be the first to hear about news, product updates, and innovation from IBM Cloud