Create an Apache PHP Container and Mount the Volume

by Andrew Nieuwsma – 2018-06-06 – Generated with ReText

Dependencies:

To Install Dependencies:

$ docker pull webdevops/php-apache

$ docker pull helper

$ docker pull ubuntu

Create a Volume to Mount in the Container

We will be creating a volume that we can put our source code into so that it can be mounted by the container. You will need some source code (I’d recommend: http://php.net/manual/en/tutorial.firstpage.php). You may perfer to copy source code into your volume from a symlink, but that is not required.

Create the volume

$ docker volume create my-code-volume

See https://docs.docker.com/storage/volumes/#share-data-among-machines for additional details

Optional – Inspect the volume

$ docker inspect my-code-volume

[

    {
        "CreatedAt": "2018-06-06T21:25:34-07:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/my-code-volume/_data",
        "Name": "my-code-volume",
        "Options": {},
        "Scope": "local"
    }
]

$ ln -s ~/path_to_src_dir/ ~/path_to_dest_dir

Create a busybox helper container to copy source code into the volume

$ docker run -v my-code-volume:/data --name=helper busybox true

Copy source code to volume

$ docker cp -L ~/path_to_code_dir/. helper:/data

Parameters:

  • -L – follow symlink
  • -a – preserve file permissions

Important ‘Gotcha’ – There is a difference between ~/path_to_code_dir/ & ~/path_to_code_dir/. The later will copy the dir contents without the dir itself.

Optional – Verify the contents of the volume

Method 1 – ls the physical file on the local system

$ sudo ls /var/lib/docker/volumes/my-code-volume/_data

Method 2 – Connect the volume to a temporary container

Spin up an ubuntu container to view the files

$ docker run -it --mount --name=ubuhelper source=my-code-volume,target=/data ubuntu bash

root@container #> ls /data

Remove the ubuntu containter

$ docker rm ubuhelper

Remove the busybox helper contianer

$ docker rm helper

Create an Apache PHP Container and Mount the Volume

There are two ways to link local storage to a container:https://docs.docker.com/storage/volumes/#choose-the–v-or—mount-flag

We will be using mount.

$ docker run -dP --name=apache-serv --mount source=my-code-volume,target=/app webdevops/php-apache

  • -d – Detached mode: run the container in the background and print the new container ID
  • -P – Publish all exposed ports to random ports on the host interfaces.
  • --mount source=[src],target=/app – The webroot is /app

Use the Container

To get the container’s IP address:

$ docker inspect apache-serv | grep -m 1 "IPAddress\":"

172.17.0.x

Optional – Clean Up

Stop the container

$ docker stop apache-serv

Optional – Remove the container

$ docker rm apache-serv

Optional – Remove the volume

$ docker volume rm my-code-volume

License

This work is licensed with Open Publication License http://opencontent.org/openpub/

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *