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 lsThis 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 lsThe 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 pwdTo open a Bash prompt inside the PHP container, use the bin/bash helper:
bin/bashThis 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:
pwdWhen you finish working inside the container, run exit to return to the prompt on your host machine:
exitThe project contains more helper scripts in the bin directory. To see the available scripts and their documentation, run make:
makeThe 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.
Join the discussion!
Comments
Want to comment on this lesson?
Log in to leave comments.