This container is built to act as a base container for projects built using Laravel4, Apache, and MySQL. It has two
ONBUILD hooks and several
ENV variables for configuration.
Create a new
Dockerfile on the first line add:
There are two build hooks that are run each time you build a container that inherits from
zanemiller/laravel-apache. The first hook will copy all data from the current folder (that is, where your
Dockerfile is located) to
/var/www/html. The second hook will simply
chown the copied files to be owned by the web-sever.
Note: While the web-app root is
/var/www/html the site is served from
/var/www/html/public. If you are not using Laravel, or are using an alternate folder structure make sure your bootstrapping file resides in the correct folder.
There are 3
ENV variables that can be used to configure MySQL. The only required variable is
ENV variable defines the new password for the
root mysql user.
ENV MYSQL_ROOT_PASSWORD new_password
If you do not provide this your container will not start!
This variable will define databases that should be automatically created on the first run of the container if they do not already exist. The variable is a space separated list of database names:
ENV MYSQL_DATABASES example_db foo bar
The command above will create 3 new databases when the container is first launched named
The third and final MySQL variable is
MYSQL_PERMISSIONS. This variable defines the users that should be created and what permissions they should be given on a database. Each permission argument is separated by a
space and arguments within permissions are separated by a colon
:. All arguments are required for each permission.
The argument list is as follows:
At this time it is only possible to
GRANT one permission at a time and all permission are granted on a per database level.
For example, to grant a user john_smith with
ALL privileges to our example_db on
localhost using a password of
hunter2 you would add the following line to your
ENV MYSQL_PERMISSIONS john_smith:localhost:hunter2:ALL:example_db
There is one final
ENV variable that can be set to instruct the startup script to run migrations and seed the database. If any value is set to the
LARAVEL_MIGRATE variable on container startup
artisan migrate --seed will be run from the root project directory.
WARNING: If set, migrations AND seeding will be run EVERY TIME you start the container. It is best to set this as a command line variable using
-e LARAVEL_MIGRATE=1, rather than in the
Dockerfile. If you have seeds that erase information from the DB they will erase this infomation EVERY start of the container.
Running the Container
Dockerfile should now look something like the following:
ENV MYSQL_ROOT_PASSWORD new_root_password
ENV MYSQL_DATABASES super_cool_webapp_db
ENV MYSQL_PERMISSIONS app_specific_user:localhost:secret_password:ALL:super_cool_webapp_db
You can now build your image by
cding to the folder that contains your
Dockerfile and running
docker build -t my_image_name . (note the
. at the end).
To instantiate a new, publicly accessible instance of your container you can now run
docker run -t -d -p 80:80 -v /var/lib/mysql:/var/lib/mysql -e LARAVEL_MIGRATE=1 my_image_name.
This will do 3 things for you, in addition to starting the container:
-p 80:80 will forward all request that come into the server on port 80 to port 80 on the container.
-v /var/lib/mysql:/var/lib/mysql will create a virtual link between the
/var/lib/mysql folders on the server and the container. This will allow data from your database to persist even when the container is stopped or removed.
-e LARAVEL_MIGRATE=1 will tell the startup script to run the migrations and seed the database. See the notes about this variable above for more information.