What is the difference between commands and container_commands configuration keys in Beanstalk?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Elastic Beanstalk .ebextensions, commands and container_commands run at different points in the deployment lifecycle. The short answer is that commands are for early instance-level setup, while container_commands are for application-level work after your source bundle has been unpacked into the staging area.
Using the wrong section causes predictable problems: package installs run too late, application build steps run before source files exist, or database migrations run more times than expected. Understanding the deployment phase is the real key here.
What commands Are For
commands run early during instance provisioning, before the application is deployed. They run as root, and they are best suited for machine setup tasks.
Typical examples include:
- installing OS packages
- writing system configuration files
- creating directories or system users
- changing permissions for instance-level resources
Example:
These commands are about preparing the environment the app will run in, not manipulating the staged application source code.
What container_commands Are For
container_commands run later, after Elastic Beanstalk has extracted your application into a staging directory but before it is deployed to the final live location. This is the right place for tasks that need the application files.
Typical examples include:
- building frontend assets
- collecting static files
- compiling templates
- running application-specific setup against staged code
- running one-time deployment tasks such as migrations
Example:
Because these run against the staged application bundle, they can safely reference files in your project.
The Most Important Practical Difference
If a command needs your application source tree, it almost certainly belongs in container_commands, not commands.
For example, this is a mistake:
At the commands phase, the deployment may not yet have the staged app code in the location you expect. That is why application build steps tend to fail there.
The corrected version is:
leader_only Matters for Shared Deployments
One especially important container_commands feature is leader_only. On environments with multiple instances, this lets one instance perform a task that should not run everywhere.
Database migrations are the classic example:
If you put a migration in plain container_commands without leader_only, every instance may attempt to run it during deployment.
Think in Terms of Machine Setup Versus App Setup
A useful mental model is:
- '
commandsprepare the server' - '
container_commandsprepare the app release'
That phrasing is often easier to remember than the full Beanstalk lifecycle details. It also helps during code review because you can ask whether a script is operating on the machine or on the staged application.
Common Pitfalls
- Putting application build steps in
commandsbefore the source bundle is staged. - Installing system packages in
container_commandsinstead of doing that machine setup earlier. - Forgetting
leader_onlyfor migrations or other one-time deployment actions. - Assuming both sections run from the same directory and with the same purpose.
- Writing non-idempotent commands that break on redeploys or instance replacement.
Summary
- Use
commandsfor early instance-level setup such as OS packages and system configuration. - Use
container_commandsfor tasks that need the staged application code. - If a script references project files like
manage.pyorpackage.json, it usually belongs incontainer_commands. - Use
leader_onlyfor one-time tasks in multi-instance environments. - The simplest rule is machine setup first with
commands, app setup later withcontainer_commands.

