Video Lesson: Execute docker-magento helper scripts in Docker

Execute docker-magento helper scripts in Docker

Learn how to use helper scripts to simplify running commands and accessing shells inside Docker containers.


Lesson Content

To execute a command inside a Docker container, use Docker Compose. The command starts with docker-compose exec, followed by the service name and the command to run. In this project, the PHP service is named phpfpm, as defined in docker-compose.yml:

docker-compose exec phpfpm ls

This lists the directories inside the phpfpm container. The full command is useful to understand, but it can be difficult to remember. The project includes helper scripts in the bin directory to make common Docker commands easier to run.

The bin/cli script accepts a command and passes it to the phpfpm container. Run the same directory listing through the helper script:

bin/cli ls

The result is the same as running docker-compose exec phpfpm ls.

The first argument passed to a Bash script is available as $1. The bin/cli script uses that value to check whether you supplied a command. If you do not provide one, it displays a message asking you to specify a CLI command. The $@ variable contains all of the arguments passed to the script, so the helper can forward the command and any additional arguments to Docker Compose.

You can use the helper for other commands as well:

bin/cli pwd

To open a Bash prompt inside the PHP container, use the bin/bash helper:

bin/bash

This script is an alias for bin/cli bash. After it runs, your prompt changes to one with a random-looking alphanumeric hostname. That hostname is the container ID, which confirms that you are now inside the Docker container.

Run a command such as pwd to verify your current directory:

pwd

When you finish working inside the container, run exit to return to the prompt on your host machine:

exit

The project contains more helper scripts in the bin directory. To see the available scripts and their documentation, run make:

make

The output lists the available commands and explains what each one does. Some helpers are simple aliases, while others combine several Docker or Magento commands.

These helpers are just Bash scripts stored in the project’s bin directory. If you need functionality that the project does not provide, create another script in that directory and add the command you want to simplify.

Maggie

Hey, I'm Maggie! 👋

Ask me anything.

Want to chat with Maggie?

Log in for AI tutor access.

Comments

Join the discussion!

Notes

Keep track of what you learn!