Cara menggunakan php exec grep

Are you sure that it can find the txt file (did you try a full path for that too) ? PHP's current path can be different than the one on your console.

All 6 Replies

Cara menggunakan php exec grep

rch1231 169 Posting Shark

11 Years Ago

2 things:
You may have to give the full path for grep which you can find by running
which grep
from a shell prompt and second what happens when you run the command from the command line?

0 0

Share

Cara menggunakan php exec grep

jrhitokiri 0 Newbie Poster

11 Years Ago

2 things:
You may have to give the full path for grep which you can find by running
which grep
from a shell prompt and second what happens when you run the command from the command line?

full path didn't help

pasting it into command line, it functions normally and outputs the 2 lines (as mentioned in my orig post)

Any other suggestions?

0 0

Share

Cara menggunakan php exec grep

pritaeas 2,130 ¯\_(ツ)_/¯ Moderator Featured Poster

11 Years Ago

Are you sure that it can find the txt file (did you try a full path for that too) ? PHP's current path can be different than the one on your console.

0 0

Share

Cara menggunakan php exec grep

jrhitokiri 0 Newbie Poster

11 Years Ago

Are you sure that it can find the txt file (did you try a full path for that too) ? PHP's current path can be different than the one on your console.

Yep, fill path for both the grep command and the text file.

0 0

Share

Cara menggunakan php exec grep

smantscheff 265 Veteran Poster

11 Years Ago

Also have a look at the preg functions of PHP which might be much easier to handle than the external grep.

0 0

Share

Cara menggunakan php exec grep

jrhitokiri 0 Newbie Poster

11 Years Ago

Also have a look at the preg functions of PHP which might be much easier to handle than the external grep.

The thing is, I'm searching a 1GB file and I fear that the preg is significantly slower than the grep external function. :<

You probably heard from the new kid around the block called "Docker"? You are a PHP developer and would like to get into that, but you didn't have the time to look into it, yet? Then this tutorial is for you! By the end of it, you should know: - how to set up Docker "natively" on a Windows 10 machine - how to build and run containers from the command line - how to log into containers and explore them for information - what a Dockerfile is and how to use it - how containers can talk to each other - how

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
20 can be used to fit everything nicely together

Note: I will not only walk on the happy path during this tutorial. That means I'll deep-dive into some things that are not completely related to docker (e.g. how to find out where the configuration files for php-fpm are located), but that are imho important to understand, because they enable you to solve problems later on your own.

But if you are short on time, you might also jump directly to .

All code samples are publicly available in my Docker PHP Tutorial repository on Github. The branch for this tutorial is part_1_setting-up-php-php-fpm-and-nginx-for-local-development-on-docker.

All published parts of the Docker PHP Tutorial are collected under a dedicated page at Docker PHP Tutorial. The following part is Setting up PhpStorm with Xdebug for local development on Docker .

If you want to follow along, please subscribe to the RSS feed or to get automatic notifications when the next part comes out :)

Table of contents

  • Introduction

Introduction

Preconditions

I'm assuming that you have installed Git bash for Windows. If not, please do that before, see .

Why use Docker?

I won't go into too much detail what Docker is and why you should use it, because others have already talked about this extensively.

As for me, my main reasons were - Symlinks in vagrant didn't work the way they should - VMs become bloated and hard to manage over time - Setup in the team involved a lot of work - I wanted to learn Docker for quite some time because you hear a lot about it

In general, Docker is kind of like a virtual machine, so it allows us to develop in an OS of our choice (e.g. Windows) but run the code in the same environment as it will in production (e.g. on a linux server). Thanks to its core principles, it makes the separation of services really easy (e.g. having a dedicated server for your database) which - again - is something that should happen on production anyway.

Transition from Vagrant

On Windows, you can either use the Docker Toolbox (which is essentially a VM with Docker setup on it) or the Hyper-V based Docker for Windows. This tutorial will only look at the latter.

A word of caution: Unfortunately, we cannot have other Gods besides Docker (on Windows). The native Docker client requires Hyper-V to be activated which in turn will cause Virtualbox to not work any longer. Thus, we will not be able to use Vagrant and Docker alongside each other. This was actually the main reason it took me so long to start working with Docker.

Setup Docker

First, download Docker for Windows (requires Microsoft Windows 10 Professional or Enterprise 64-bit). The version I am using in this tutorial is

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
21. During the installation, leave the option "Use Windows Containers instead of Linux containers" unticked as we intend to develop on linux containers (you can change it later anyway).

Cara menggunakan php exec grep

After the installation finishes, we need to log out of Windows and in again. Docker should start automatically. If not, there should be a "Docker for Windows" icon placed on your desktop. If Hyper-V is not activated yet, Docker will automatically urge you to do so now.

If you agree, Hyper-V and container features are activated and a reboot is initiated. See Install Hyper-V on Windows 10 to deactivate it again.

Caution: VirtualBox will stop working afterwards! Starting one of my previous machines from the VirtualBox interface or via

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
22 fails with the error message

VT-x is not available (VERR_VMX_NO_VMX)

After rebooting, Docker will start automatically and a welcome screen appears.

We can ignore that (close the window). In addition, a new icon is added to your system tray. A right-click reveals the context menu.

Open the tab "Shared Devices" and tick the hard drives on your host machine that you want to share with Docker containers.

Note: We will still need to define explicit path mappings for the actual containers later on, but the hard drive that the path belongs to must be made available here. After clicking "Apply", you will be prompted for your credentials

Next, open tab "Advanced". You don't actually have to change any of the settings but if you (like me) don't have

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
23 set up as you biggest partition, you might want to change the "Disk image location". I'm putting mine at
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
24. It might take some minutes for Docker to process the changes.

Docker "physically" stores the container images in that location.

Congratulations, Docker is now set up on your machine 😊

Setting up the PHP cli container

Now that we have the general stuff out of the way, let's set up our first container. I've created the directory

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
25 and will run the remaining examples in there.

Firstly, lets create a directory for our sourcecode:

mkdir -p "C:/codebase/docker-php/app"

For the sake of simplicity, we will stick to the official PHP image and run:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli

Which means:

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container

The result looks something like this:

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f

Since we don't have the image on our machine (see

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
26), Docker attempts to pull it from the official registry at https://hub.docker.com/. We've specifically chosen the "7.0-cli" version of the PHP image (which means: PHP 7.0 CLI only). See https://hub.docker.com/_/php/ for a list of all available tags/images.

Now let's see if the container is actually running via

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
27

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Weird. For some reason, we don't see our newly created container there. Let's check with the

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
28 flag to list all containers, even the ones that are not running.

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                      PORTS               NAMES
56af890e1a61        php:7.0-cli         "docker-php-entrypoi…"   27 seconds ago     Exited (0) 25 seconds ago                       docker-php

Aha. So the container was created, but immediately stopped (see

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
29). That's because a container only lives as long as it's main process is running. According to the docs,

A container's main running process is the ENTRYPOINT and/or CMD at the end of the Dockerfile."

This answer explains the difference between CMD and ENTRYPOINT quite well. Since we don't have a Dockerfile defined, we would need to look at the Dockerfile of the base image we're using, but I actually don't wanna go down this rabbit hole for now. Basically, the "problem" is, that the container doesn't have a long running process / service defined, (as the php-fpm or the nginx containers do later on). To keep the container alive, we need to add the

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
30 flag to the
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
31 command:

docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli

But then this happens:

[email protected] MINGW64 /
$ docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/docker-php" is already in use by container "56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.

Apparently, we cannot use the same name (

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
32) again. Bummer. So, let's remove the previous container first via

docker rm docker-php

and try again afterwards:

[email protected] MINGW64 /
$ docker rm docker-php
docker-php

[email protected] MINGW64 /c/codebase/docker-php
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7b3024a542a2d25fd36cef96f4ea689ec7ebb758818758300097a7be3ad2c2f6

[email protected] MINGW64 /c/codebase/docker-php
$ docker ps
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS               NAMES
7b3024a542a2        php:7.0-cli         "docker-php-entrypoi…"   5 seconds ago      Up 4 seconds                            docker-php

Sweet, so now that the container is up and running, let's "log in" via

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
0

You might get the following error message

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
1

If so, prefixing the command with

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
33 should help:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
2
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
3

A quick

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
34 within the container verifies, that we can actually run php scripts in there:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
4

Remember the path mapping, that we specified? Let's create a simple "hello world" script on the windows 10 host machine at

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
35 to make sure it works:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
5

Should look like this on the host machine:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
6

And like this from within the container:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7

Let's run the script in the container via

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
8
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
9

Purrfect. We created the file on our host system and it's automatically available in the container.

Installing Xdebug in the PHP container

Since we intend to use Docker for our local development setup, the ability to debug is mandatory. So let's extend our image with the xdebug extension. The readme of the official Docker PHP repository does a good job at explaining . For xdebug, we'll use PECL. To install the extension, make sure to be logged into the container and run

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
0

You should see an output like this:

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
1

The xdebug extension has been build and saved in

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
36. To actually activate it, run

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
2

That helper command will place the file

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
37 in the directory for additional php ini files with the content

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
3

which enables the extension. Btw. you can locate the additional php ini files folder by running

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
4

Result:

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
5

When we check the contents of that folder, we will indeed find the

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
38 file with the before mentioned content and
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
39 reveals, that xdebug is actually active.

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
6

Now we'll log out of the container (type "exit" or hit

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
40 +
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
41) and stop the container via

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
7
docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
8

Now we start the container again via

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
9

log back in and check if xdebug is still there:

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f
0

And... it is! So the changes we made "survived" a restart of the container. But: They won't survive a "rebuild" of the container. First we stop and remove the container via

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f
1

The

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
42 flag forces the container to stop. Otherwise we would need an additional
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
43 before.

Then we rebuild it, log in

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f
2

and check for xdebug:

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f
3

... which won't be there anymore.

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f
4

Note the new container ID (before:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
44; now:
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
45) and that
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
46 doesn't yield anything.

Persisting image changes with a Dockerfile

Simply put, a Dockerfile describes the changes we make to a base image, so we (and everybody else) can easily recreate the same environment. In our case, we need to define the PHP base image that we used as well as instructions for installing and enabling xdebug. To clearly separate infrastructure from code, we'll create a new directory at

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
47. Create a file named
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
48 in this directory

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f
5

and give it the following content:

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f
6

Change to the

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
47 directory and build the image based on that Dockerfile

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f
7

The

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
50 is actually optional as this is the default anyway. "docker-php-image" is the name of our new image.

If you encounter the following error

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f
8

you probably missed the trailing

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
51 at the end of
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
52 ;)

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f
9

Note, that the building takes longer than before, because Docker now needs to do the extra work of installing xdebug. Instead of using the base

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
53 image, we'll now use our new, shiny
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
54 image to start the container and check for xdebug.

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
1

Aaaand we get an error, because we tried to use the same name ("docker-php"), that we used for the previous, still running container. Sigh.. fortunately we already know how to solve that via

$ docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
Unable to find image 'php:7.0-cli' locally
7.0-cli: Pulling from library/php
f2aa67a397c4: Pulling fs layer
c533bdb78a46: Pulling fs layer
65a7293804ac: Pulling fs layer
35a9c1f94aea: Pulling fs layer
54cffc62e1c2: Pulling fs layer
153ff2f4c2af: Pulling fs layer
96d392f71f56: Pulling fs layer
e8c43e665458: Pulling fs layer
35a9c1f94aea: Waiting
54cffc62e1c2: Waiting
153ff2f4c2af: Waiting
96d392f71f56: Waiting
e8c43e665458: Waiting
c533bdb78a46: Verifying Checksum
c533bdb78a46: Download complete
35a9c1f94aea: Verifying Checksum
35a9c1f94aea: Download complete
f2aa67a397c4: Verifying Checksum
f2aa67a397c4: Download complete
153ff2f4c2af: Verifying Checksum
153ff2f4c2af: Download complete
54cffc62e1c2: Verifying Checksum
54cffc62e1c2: Download complete
e8c43e665458: Verifying Checksum
e8c43e665458: Download complete
96d392f71f56: Verifying Checksum
96d392f71f56: Download complete
f2aa67a397c4: Pull complete
65a7293804ac: Verifying Checksum
65a7293804ac: Download complete
c533bdb78a46: Pull complete
65a7293804ac: Pull complete
35a9c1f94aea: Pull complete
54cffc62e1c2: Pull complete
153ff2f4c2af: Pull complete
96d392f71f56: Pull complete
e8c43e665458: Pull complete
Digest: sha256:ff6c5e695a931f18a5b59c82b1045edea42203a299e89a554ebcd723df8b9014
Status: Downloaded newer image for php:7.0-cli
56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f
1

Retry

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4

Yep, all good. Btw. since we "only" want to check if xdebug was installed, we could also simply pass

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
55 to the
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
31 command:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5

Be aware that this will create a new container every time it's run (, note the first entry with the wonderful name "distracted_mclean"):

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6

Before we move on, let's []stop and remove all containers via](https://coderwall.com/p/ewk0mq/stop-remove-all-docker-containers).

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7

The

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
57 part returns only the numeric ids of all containers and passes them to the
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
58 command.

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
8

Setting up a web stack with php-fpm and nginx

Since most people are probably not only working on CLI scripts but rather on web pages, the next step in this tutorial is about setting up an nginx web server and connect it to php-fpm.

Setting up nginx

We're gonna use the official nginx image and since we don't know anything about that image yet, let's run and explore it a bit:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9

yields

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                      PORTS               NAMES
56af890e1a61        php:7.0-cli         "docker-php-entrypoi…"   27 seconds ago     Exited (0) 25 seconds ago                       docker-php
0

Note that we only used the minimum number of arguments here. Since we did not specify a name, we will simply use the ID instead to log in (so be sure to use the one that your shell returned - don't just copy the line below :P)

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                      PORTS               NAMES
56af890e1a61        php:7.0-cli         "docker-php-entrypoi…"   27 seconds ago     Exited (0) 25 seconds ago                       docker-php
1

We would expect that there is an nginx process running, but upon checking with

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
59 we get

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                      PORTS               NAMES
56af890e1a61        php:7.0-cli         "docker-php-entrypoi…"   27 seconds ago     Exited (0) 25 seconds ago                       docker-php
2

This is common when using docker images, because they are usually kept as minimal as possible. Although this is a good practice in production, it is kind of cumbersome in development. So, let's install

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
60 via

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                      PORTS               NAMES
56af890e1a61        php:7.0-cli         "docker-php-entrypoi…"   27 seconds ago     Exited (0) 25 seconds ago                       docker-php
3

and try again:

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                      PORTS               NAMES
56af890e1a61        php:7.0-cli         "docker-php-entrypoi…"   27 seconds ago     Exited (0) 25 seconds ago                       docker-php
4

Ah. Much better. Lets dig a little deeper and see how the process is configured via

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
61

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                      PORTS               NAMES
56af890e1a61        php:7.0-cli         "docker-php-entrypoi…"   27 seconds ago     Exited (0) 25 seconds ago                       docker-php
5

Sweet, so the configuration file is placed in the default location at

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
62 (see
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
63). Checking that file will show us, where we need to place additional config files (e.g. for the configuration of our web site). Run

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                      PORTS               NAMES
56af890e1a61        php:7.0-cli         "docker-php-entrypoi…"   27 seconds ago     Exited (0) 25 seconds ago                       docker-php
6

... and see

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                      PORTS               NAMES
56af890e1a61        php:7.0-cli         "docker-php-entrypoi…"   27 seconds ago     Exited (0) 25 seconds ago                       docker-php
7

Note the line

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
64 at the end of the file. In this directory, we'll find the default nginx config:

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                      PORTS               NAMES
56af890e1a61        php:7.0-cli         "docker-php-entrypoi…"   27 seconds ago     Exited (0) 25 seconds ago                       docker-php
8
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS                      PORTS               NAMES
56af890e1a61        php:7.0-cli         "docker-php-entrypoi…"   27 seconds ago     Exited (0) 25 seconds ago                       docker-php
9

So the server is listening on port 80. Unfortunately, we cannot reach the web server from our windows host machine, as there is currently (2018-05-31) an open bug for accessing container IPs from a windows host (don't worry, we'll fix that with port mappings in a second)). So, in order to verify that the server is actually working, we'll install

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
65 inside the nginx container and fetch
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
66:

docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
0

Looks like this:

docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
1

Looks good! Now let's customize some stuff: - point the root to

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
67 - place a "Hello world" index file in
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
68

docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
2

To make the changes become effective, we need to via

docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
3
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
4

Check with curl, et voilá:

docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
5

With all that new information we can set up our nginx image with the following folder structure on the host machine:

docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
6

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
69

docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
70

docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
8

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
71

docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
9

Clean up the "exploration" nginx container,

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
72 into
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
73 and build the new image:

[email protected] MINGW64 /
$ docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/docker-php" is already in use by container "56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
0
[email protected] MINGW64 /
$ docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/docker-php" is already in use by container "56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
1

And then run the "new" container via

[email protected] MINGW64 /
$ docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/docker-php" is already in use by container "56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
2

where

[email protected] MINGW64 /
$ docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/docker-php" is already in use by container "56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
3

Thanks to the port mapping we can now simply open http://127.0.0.1:8080/ in a browser on the host machine and see the content of our

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
71 file.

If you want some more information about running nginx on Docker, check out this tutorial.

Before we move on, let's clean up

[email protected] MINGW64 /
$ docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/docker-php" is already in use by container "56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
4

Setting up php-fpm

We are already familiar with the official docker PHP image but have only used the cli-only version so far. FPM ones can be pulled in by using the

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
75 tags (e.g. like
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
76). As with nginx, let's explore the php-fpm image first:

[email protected] MINGW64 /
$ docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/docker-php" is already in use by container "56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
5

The first thing to note is, that the image automatically exposes port 9000 as a

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
27 reveals:

[email protected] MINGW64 /
$ docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/docker-php" is already in use by container "56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
6

When we examine the Dockerfile that was used to build the image (click here and search for the "7.0-fpm" tag that currently (2018-05-31) links here), we can see that it contains an

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
78 at the bottom.

What else we can we figure out...

[email protected] MINGW64 /
$ docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/docker-php" is already in use by container "56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
7

First, will check where the configuration files are located via

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
79:

[email protected] MINGW64 /
$ docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/docker-php" is already in use by container "56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
8

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
80 is our suspect. So it is very likely, that we will find the at
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
81 (unfortunately, we cannot resolve the location directly).
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
82'ing this file for
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
83 reveals the location for the :

[email protected] MINGW64 /
$ docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Conflict. The container name "/docker-php" is already in use by container "56af890e1a61f8ffa5528b040756dc62a94c0b929c29df82b9bf5dec6255321f". You have to remove (or rename) that container to be able to reuse that name.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
9
docker rm docker-php
0

Hm - a relative path. That looks kinda odd? Let's get a little more context with the

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
84 option for
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
82:

docker rm docker-php
1
docker rm docker-php
2

Ah - that makes more sense. So we need to resolve

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
86 relative to
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
87. Resulting in
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
88 (usually you'll at least find a
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
89 file in there). The pool config determines amongst other things how php-fpm listens for connections (e.g. via Unix socket or via TCP IP:port).

docker rm docker-php
3
docker rm docker-php
4

php-fpm ist listening on port 9000 on 127.0.0.1 (localhost). So it makes total sense to expose port 9000.

Installing xdebug

Since we probably also want to debug php-fpm, xdebug needs to be setup as well. The process is pretty much the same as for the cli image:

docker rm docker-php
5

Of course we'll also put that in its own Dockerfile:

docker rm docker-php
6

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
90

docker rm docker-php
7

Clean up the test container and build the new image

docker rm docker-php
8

Connecting nginx and php-fpm

Now that we have containers for nginx and php-fpm, we need to connect them. To do so, we have to make sure that both containers are in the same network and can talk to each other (which is a common problem). Docker provides so called allowing automatic service discovery. That basically means, that our nginx container can use the name of the php-fpm container to connect to it. Otherwise we would have to figure out the containers IP address in the default network every time we start the containers.

docker rm docker-php
9

reveals a list of the current networks

[email protected] MINGW64 /
$ docker rm docker-php
docker-php

[email protected] MINGW64 /c/codebase/docker-php
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7b3024a542a2d25fd36cef96f4ea689ec7ebb758818758300097a7be3ad2c2f6

[email protected] MINGW64 /c/codebase/docker-php
$ docker ps
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS               NAMES
7b3024a542a2        php:7.0-cli         "docker-php-entrypoi…"   5 seconds ago      Up 4 seconds                            docker-php
0

Now let's add a new one called

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
91 for our web stack via

[email protected] MINGW64 /
$ docker rm docker-php
docker-php

[email protected] MINGW64 /c/codebase/docker-php
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7b3024a542a2d25fd36cef96f4ea689ec7ebb758818758300097a7be3ad2c2f6

[email protected] MINGW64 /c/codebase/docker-php
$ docker ps
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS               NAMES
7b3024a542a2        php:7.0-cli         "docker-php-entrypoi…"   5 seconds ago      Up 4 seconds                            docker-php
1
[email protected] MINGW64 /
$ docker rm docker-php
docker-php

[email protected] MINGW64 /c/codebase/docker-php
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7b3024a542a2d25fd36cef96f4ea689ec7ebb758818758300097a7be3ad2c2f6

[email protected] MINGW64 /c/codebase/docker-php
$ docker ps
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS               NAMES
7b3024a542a2        php:7.0-cli         "docker-php-entrypoi…"   5 seconds ago      Up 4 seconds                            docker-php
2

Start the nginx container and connect it to the new network via

[email protected] MINGW64 /
$ docker rm docker-php
docker-php

[email protected] MINGW64 /c/codebase/docker-php
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7b3024a542a2d25fd36cef96f4ea689ec7ebb758818758300097a7be3ad2c2f6

[email protected] MINGW64 /c/codebase/docker-php
$ docker ps
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS               NAMES
7b3024a542a2        php:7.0-cli         "docker-php-entrypoi…"   5 seconds ago      Up 4 seconds                            docker-php
3

Finally, we need to mount the local code folder

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
92 we mounted to the nginx container at
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
67 also in the php-fpm container in the same location:

[email protected] MINGW64 /
$ docker rm docker-php
docker-php

[email protected] MINGW64 /c/codebase/docker-php
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7b3024a542a2d25fd36cef96f4ea689ec7ebb758818758300097a7be3ad2c2f6

[email protected] MINGW64 /c/codebase/docker-php
$ docker ps
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS               NAMES
7b3024a542a2        php:7.0-cli         "docker-php-entrypoi…"   5 seconds ago      Up 4 seconds                            docker-php
4

Note that we specified the network in the run command via the

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
94 option. We can verify that both containers are connected to the
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
91 by running

[email protected] MINGW64 /
$ docker rm docker-php
docker-php

[email protected] MINGW64 /c/codebase/docker-php
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7b3024a542a2d25fd36cef96f4ea689ec7ebb758818758300097a7be3ad2c2f6

[email protected] MINGW64 /c/codebase/docker-php
$ docker ps
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS               NAMES
7b3024a542a2        php:7.0-cli         "docker-php-entrypoi…"   5 seconds ago      Up 4 seconds                            docker-php
5
[email protected] MINGW64 /
$ docker rm docker-php
docker-php

[email protected] MINGW64 /c/codebase/docker-php
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7b3024a542a2d25fd36cef96f4ea689ec7ebb758818758300097a7be3ad2c2f6

[email protected] MINGW64 /c/codebase/docker-php
$ docker ps
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS               NAMES
7b3024a542a2        php:7.0-cli         "docker-php-entrypoi…"   5 seconds ago      Up 4 seconds                            docker-php
6

The "Containers" key reveals that the

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
96 container has the IP address 172.18.0.3 and the docker-nginx container is reachable via 172.18.0.2. But can we actually connect from nginx to php-fpm? Let's find out:

Log into the nginx container

[email protected] MINGW64 /
$ docker rm docker-php
docker-php

[email protected] MINGW64 /c/codebase/docker-php
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7b3024a542a2d25fd36cef96f4ea689ec7ebb758818758300097a7be3ad2c2f6

[email protected] MINGW64 /c/codebase/docker-php
$ docker ps
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS               NAMES
7b3024a542a2        php:7.0-cli         "docker-php-entrypoi…"   5 seconds ago      Up 4 seconds                            docker-php
7

and ping the IP

[email protected] MINGW64 /
$ docker rm docker-php
docker-php

[email protected] MINGW64 /c/codebase/docker-php
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7b3024a542a2d25fd36cef96f4ea689ec7ebb758818758300097a7be3ad2c2f6

[email protected] MINGW64 /c/codebase/docker-php
$ docker ps
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS               NAMES
7b3024a542a2        php:7.0-cli         "docker-php-entrypoi…"   5 seconds ago      Up 4 seconds                            docker-php
8
[email protected] MINGW64 /
$ docker rm docker-php
docker-php

[email protected] MINGW64 /c/codebase/docker-php
docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
7b3024a542a2d25fd36cef96f4ea689ec7ebb758818758300097a7be3ad2c2f6

[email protected] MINGW64 /c/codebase/docker-php
$ docker ps
CONTAINER ID        IMAGE               COMMAND                 CREATED             STATUS              PORTS               NAMES
7b3024a542a2        php:7.0-cli         "docker-php-entrypoi…"   5 seconds ago      Up 4 seconds                            docker-php
9

.. well, after we make the command available by installing

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
97:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
00
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
01

We can ping the container - that's good. But we were also promised we could reach the container by its name

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
96:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
02
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
03

And we can - awesome! Now we need to tell nginx to pass all PHP related requests to php-fpm by changing the

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
70 file on our windows host to

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
04

Note the

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
00 line that tells nginx how to reach our php-fpm service. Because we mounted the
docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
01 folder, we just need to reload nginx:

docker run -di --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
3

and open http://127.0.0.1:8080/hello-world.php on a browser on your host machine.

Btw. there's also a good tutorial on geekyplatypus.com on how to Dockerise your PHP application with Nginx and PHP7-FPM. But since it's using docker-compose you might want to read the next chapter first :)

Putting it all together: Meet docker-compose

Lets sum up what we have do now to get everything up and running: 1. start php-cli 2. start nginx 3. start php-fpm

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
06

Hm. That's alright I guess... but it also feels like "a lot". Wouldn't it be much better to have everything neatly defined in one place? I bet so! Let me introduce you to docker-compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.

Lets do this step by step, starting with the php-cli container. Create the file

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
02:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
07

Before we get started, we're gonna clean up the old containers:

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
7

To test the docker-compose.yml we need to run

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
03 from
docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
04

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
09
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
10

Note that the image is build from scratch when we run

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
05 for the first time. A
docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
06 shows that the container is running fine, we can log in and execute source code from the host machine.

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
11

Logging in

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
12

and running

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
8

works as before

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
14

Now log out of the container and run

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
15

to shut the container down again:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
16

Add the remaining services to the

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
07 file:

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
17

And up again...

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
18
docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
19

Only nginx and php-fpm needed to be built because the php-cli one already existed. Lets check if we can still open http://127.0.0.1:8080/hello-world.php in a browser on the host machine:

Yes we can! So instead of needing to run 3 different command with a bunch of parameters we're now down to

docker run                               // run a container
-d                                       // in the background (detached)
--name docker-php                        // named docker-php
-v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                         // windows host with /var/www in the container
php:7.0-cli                              // use this image to build the container
03. Looks like an improvement to me ;)

The tl;dr

The whole article is a lot to take in and it is most likely not the most efficient approach when you "just want to get started". So in this section we'll boil it down to only the necessary steps without in depth explanations.

  • Download Docker for Windows
    • activate Hyper-V (Virtual Box will stop working)
    • enable Disk Sharing in the settings
  • Set up the following folder structure ```` C:\codebase\docker-php
    • nginx\
      • conf.d\
      • site.conf
      • Dockerfile
    • php-cli\
      • Dockerfile
    • php-fpm\
      • Dockerfile
    • app\
      • index.html
      • hello-world.html
    • docker-compose.yml ````
    • or simply
      docker run                               // run a container
      -d                                       // in the background (detached)
      --name docker-php                        // named docker-php
      -v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                               // windows host with /var/www in the container
      php:7.0-cli                              // use this image to build the container
      
      09
  • Open a shell at
    docker run                               // run a container
    -d                                       // in the background (detached)
    --name docker-php                        // named docker-php
    -v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                             // windows host with /var/www in the container
    php:7.0-cli                              // use this image to build the container
    
    04
  • run
    docker run                               // run a container
    -d                                       // in the background (detached)
    --name docker-php                        // named docker-php
    -v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                             // windows host with /var/www in the container
    php:7.0-cli                              // use this image to build the container
    
    03
  • check in browser via
    • 127.0.0.1:8080
    • 127.0.0.1:8080/hello-world.php
  • run
    docker run                               // run a container
    -d                                       // in the background (detached)
    --name docker-php                        // named docker-php
    -v "C:/codebase/docker-php/app":/var/www // sync the directory C:/codebase/docker-php/app on the 
                                             // windows host with /var/www in the container
    php:7.0-cli                              // use this image to build the container
    
    12

Your application code lives in the

docker run -d --name docker-php -v "C:/codebase/docker-php/app":/var/www php:7.0-cli
92 folder and changes are automatically available to the containers. This setup denotes the end of the first tutorial. In the next part we will learn how to set up Docker in PHPStorm, especially in combination with xdebug.

Wrapping up

Congratulations, you made it! If some things are not completely clear by now, don't hesitate to leave a comment. Apart from that, you should now have a first idea on what docker is and how you can use it.

If you want to go deeper, please check out the remaining articles of the Docker PHP Tutorial series.

Please subscribe to the RSS feed or to get automatic notifications when this next part comes out :)


Wanna stay in touch?

Since you ended up on this blog, chances are pretty high that you're into Software Development (probably PHP, Laravel, Docker or Google Big Query) and I'm a big fan of feedback and networking.

So - if you'd like to stay in touch, feel free to shoot me an email with a couple of words about yourself and/or connect with me on LinkedIn or Twitter or simply subscribe to my RSS feed or go the crazy route and subscribe via mail and don't forget to leave a comment :)

Subscribe to posts via mail

Email Address

First Name

We use Mailchimp as our newsletter provider. By clicking subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.