When building PHP web applications you usually have to take care about defining and providing a consistent development and production environment for your team. This includes your application packages, PHP-extensions, database servers and often further components such as a mail server or workers.
This is where Docker comes into play...
Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments. As a result, IT can ship faster and run the same app, unchanged, on laptops, data center VMs, and any cloud.
With Docker you can create an isolated environment for your application. If you've worked with virtual-machines already, this may be familiar to you - with an important difference - resources needed to spin up new Docker containers are just a fraction of virtual-machines.
You can easily run dozens of containers on a standard notebook, Docker can create databases, virtual-hosts and virtually any other type of service you need for your app ... sounds interesting? Let's get started!
If you haven't Docker running already, head over to the installation instructions.
Getting the container image ¶
We've created an image on Docker Hub for this example to make things easier.
docker pull schmunk42/yii2-app-basic
The download may take a few minutes, depending on your network connection, but once it's downloaded you can get updates as slim AUFS layers, which are usually only a few megabytes in size.
"Docker is like git for virtual-machines."
The image is build from this
Dockerfile, the comments should give you an idea what is happening there.
Run the container ¶
For a first quick test-drive just start the container with
docker run -p 8888:80 schmunk42/yii2-app-basic
Open http://127.0.0.1:8888 (Linnx) or http://192.168.59.103:8888 (OS X, Windows) in your browser.
Ctrl+c to stop the process, you can also start the docker process in the background, by adding the
--detach option to the
Since the application runs directly in your container, you can't really modify it ... yet.
To access the application source-code for development, just copy the application template from the image to a mounted volume your host.
The following command will create a
myapp app folder in your working directory, which contains the application.
docker run \ -v `pwd`/myapp:/app-install \ schmunk42/yii2-app-basic \ cp -a /app/. /app-install
Now as you have the source-code on your host system, go to the
myapp folder and mount the application as volume and re-run the container
cd myapp docker run -d -p 8888:80 \ -v `pwd`:/app \ --name myapp \ schmunk42/yii2-app-basic
Access the application under the URLs mentioned above. You can directly edit the files of your application or run commands like
docker exec myapp ./yii
directly off the running container.
Amazed as we were? If you say "Oh yes!" and you would like to use a Yii 2.0 Framework application with a more Docker-optimized setup based on environment variables and more cool features, you should have a look at Phundament 4.
Yet another dockerized yii2 application template ...
schmunk was a little faster, but I've also been working on a dockerized yii2 template. You can check it out here: https://github.com/codemix/yii2-dockerized.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.