Part 1 was all about Running a single docker container explained step by step.

In Part 2 we were Running multiple Docker containers side by side and observed the Data separation.

In Part 3 we were Understanding Host Directory Volume Mounting in Docker for Beginners

Photo by Fernando Hernandez / Unsplash

Now in this part, we are spinning up a PHP container with host directory volume mounting in docker step by step. It doesn’t really matter that it’s PHP. Believe me! It can be anything really, Python, Go, Ruby, JavaScript, you name it.

This way you can edit your source-code on your host-machine, while serving it through a web-server in a container. Pretty cool!

Prerequisites

Again, make sure you installed “Docker Desktop” and you have the Docker Daemon running. Then open a PowerShell on Windows, a Terminal on Mac/Linux and follow along! This is fun.

Docker Volume Mounting Video Walkthrough

You can also follow along in this video, if you prefer. You can also get this as a full Docker Video QuickStart Course.

Practical Example of Host Directory Volume Mounting in Docker Step by Step for Beginners

Let’s run this container:

docker run -it --rm --name my-running-script php:7.2-cli /bin/bash

This downloads the official php 7.2 cli image and runs a shell inside. Download may take a bit and you’ll see this:

Downloading layers for the image php:7.2-cli
php -m

Get the installed modules. This will print a list of all modules available in php.

A list of all modules available once you are in logged into the container.
php -i

Get the config of the PHP cli. The output is similar to above, just with the configuration of PHP instead of the modules only.

Let’s try what happens if we are outputting “test text” inside a PHP file. Create a new PHP File in your home directory with a simple echo:

echo '<? echo "test text\n";' > ~/index.php

This will write <? echo “text text\n”; into the index.php file in the home directory of root

Let’s execute this

php ~/index.php

This sould print now “test text”. It should looks like this:

Adding an index.php file and executing it with PHP inside the docker container.

Now, what will happen if we exit the container?

exit

Will end the container, but will also remove the container (including our index.php) because of the--rmflag. This means, our index.php is gone. End of story. Or?

This is where mounting an external host directory comes in handy. This way you can edit your files on the host, keep your files there, persistently, and just serve them through the container. First create a new directory somewhere and cd into it:

mkdir myDockerTestDir 
cd myDockerTestDir

Make sure it is empty:

ls
Created a new directory and it’s empty at the beginning.

And then run a new container with the current directory mounted to /myfiles inside the container:

docker run -it --rm -v ${PWD}:/myfiles --name my-running-script php:7.0-cli /bin/bash

Now we are running the php 7.0, because we can, not the php 7.2 cli. Just as a demonstration how easy it is to switch the PHP versions. But you could very well also run php 7.2.

And we are mounting the root directory into the /myfiles directory inside the container

echo '<? echo "test text\n";' > /myfiles/index.php

This creates the same index.php, but this time inside /myfiles.

Observe the host directory

php /myfiles/index.php

This should run index.php with the php 7.0 interpreter. It will output you again “test text”, and that’s exactly what we want.

Running the docker container with php 7.0 and a directory mounted into the container.

What happens if we exit the container?

exit

The container is gone. But the file stays. We can safely exit again, because our file is securely stored on the host. It will not be destroyed with the container upon exiting.

You can see the file now in your host:

ls

Will show you the file is there. You created it inside the container, but it was written outside of the container on the host. Pretty cool!

The created file is available on the docker host though volume mounting.

We can run php 7.2 now and execute the exact same file if we want to.

docker run -it --rm -v ${PWD}:/myfiles --name my-running-script php:7.2-cli /bin/bash

Let’s re-run the 7.2-cli container. This time with the same directory mounted in /myfiles

php /myfiles/index.php

This allows you to execute the same script with another PHP version

exit

Let’s exit the container.

This is how it looks like:

The file is still there, we are mounting the directory with the index.php into the container.

But can we make this a bit more efficient? Instead of opening a shell in a container, then executing the command, then closing it, why not directly execute PHP?

docker run -it --rm -v ${PWD}:/myfiles -w /myfiles --name my-running-script php:7.2-cli php index.php

Will directly run index.php and output the “test text” and exit again on the host. Pretty neat. No need to actually enter the container. We also used the -w flag, which sets the working directory to /myfiles, so we don’t need to “cd” into the directory or use an absolute path in the index.php.

Running a docker container with work-dir set and host-directory mounted volume outputting directly on the host.

That’s the end of the tutorial. If you like this, then also check out the video course Understanding Docker and Docker-Compose — 100% Hands-On. It’s a 2.5h Quickstart into Docker.

If you want more, then follow me on Medium, there will be a new tutorial frequently.

Also, checkout my blog where I have tons of Beginner Tutorials about about Blockchain, Solidity, Ethereum, Docker and other Technical Topics.

Thanks!