What is the difference between commands and container_commands configuration keys in Beanstalk?

Solution 1:

The major difference between these two pieces is when in the Elastic Beanstalk deployment process they are run.

Commands

These commands are run early in the deployment process, before the web server has been set up, and before your application code has been unpacked:

The commands are processed in alphabetical order by name, and they run before the application and web server are set up and the application version file is extracted.1

By default, the commands run in the root user's home folder. This and various other pieces of EB's behavior can be changed via options (working directory, whether to continue on error, environment variables to pass to commands, etc.) that can be passed along with the command.

Container Commands

These commands are run later in the deployment process, after the web server has been set up, and after your application code has been unpacked to the staging folder, but before your application has been "deployed" (by moving the staging folder to its final location):

Container commands run after the application and web server have been set up and the application version archive has been extracted, but before the application version is deployed. Non-container commands and other customization operations are performed prior to the application source code being extracted.2

By default, these commands run in the staging folder, so that any changes you make to the current folder will persist once your application is deployed (the path will change though, so be careful about relative links!).

Container commands support all the same options as (non-container) commands, but they also support a "leader_only" option:

You can use leader_only to only run the command on a single instance, or configure a test to only run the command when a test command evaluates to true. Leader-only container commands are only executed during environment creation and deployments, while other commands and server customization operations are performed every time an instance is provisioned or updated.2

Solution 2:

As an addition to @Tiro and @t10508hn answer I just want to clarify that both commands and container_commands are executed on the host machine.

To execute commands within the docker container use the Dockerfile.

This was a point of confusion when I was had to execute a couple of commands with leader_only and assumed that container_commands were actually executed within the container.