Php: [Problem] Enable mod_rewrite

Created on 20 Jan 2016  路  9Comments  路  Source: docker-library/php

Hi guys, I just downloaded the image PHP5.6-apache, but I need to activate the module "mod_rewirte" what I did to activate it, was going to "bash" and run "a2enmod rewrite" and it worked. I did a "commit" of this container but the problem is that now I run "my new image" and it seems that it is not running the "CMD [" apache2-foreground "]" command when I create a new container

Most helpful comment

Your best solution is going to create a new Dockerfile from this one, something like this:

FROM php:5.6-apache

RUN a2enmod rewrite

Combine that with automated builds (https://docs.docker.com/docker-hub/builds/) and repository links (https://docs.docker.com/docker-hub/builds/#repository-links) and it's reasonably easy to have an up-to-date image built FROM this one with the rewrite module enabled. :+1:

All 9 comments

Your best solution is going to create a new Dockerfile from this one, something like this:

FROM php:5.6-apache

RUN a2enmod rewrite

Combine that with automated builds (https://docs.docker.com/docker-hub/builds/) and repository links (https://docs.docker.com/docker-hub/builds/#repository-links) and it's reasonably easy to have an up-to-date image built FROM this one with the rewrite module enabled. :+1:

I had thought of that, but I wanted to know if there is a better way, or "best practices" to do this without creating a new "Dockerfile".
Thank you for guiding me.

  1. :x: Yo could do a bash oneliner with the docker run, the docker exec a2enmod, and commit (not best practice IMHO)
  2. :x: Yo could have those bash steps encapsulated in a bash function to reuse, with or without the commit (not best practice IMHO)
  3. :white_check_mark: Or you could write ONCE the Dockerfile, then commit with "-t myphp:5.6-apache-rewrite" and have a new image the Docker way, with it's beautiful cached layers and reach the Docker :angel:Heaven:angel: ! Then you can delete the Dockerfile and never again write one.

:whale:file is your friend, don't be mean to it, it's just innocent ASCII

Thanks @iamsortiz

You welcome @bufface.

I also rejected the idea at first, and sincerely laughed at it, but you'll end up adding some more apache mods or libraries (imagemagick, etc) and having that Dockerfile will be useful for upgrading your base apache container. I recommend you putting that base apache in a public repo, it could be useful for someone or just as "personal marketing" to show up you are using Docker and creating your own containers.

Bear in mind that this could be a first step into a "continuous integration" or "continuous delivery" workflow, where you have multiple containers you'd like to test and deploy together (just for fun, just for the coolness of that, or just for the "personal marketing").

In my workplace the codebase it's getting more complex everyday so any automation is welcome.

When you want to use multiple commands you use /bin/bash -c. Thus said, I did it with the following command. (I didn't want to create a docker file for just one command either)

docker run  -v myhtml:/var/www/html -p 8000:80 php:5.6-apache /bin/bash -c 'a2enmod rewrite; apache2-foreground'

Thanks @silgon

I would add:

  • $PWD in the volume to get the current folder as the one to be exposed via apache
  • a name for the container to ease log viewing, killing the container, etc... (e.g: docker logs name_here)
docker run -d -v $PWD:/var/www/html --name php_apache -p 8000:80 php:5.6-apache /bin/bash -c 'a2enmod rewrite; apache2-foreground'

If anyone is interested here you got a Gist with some convenience bash scripts around this idea:

Extract of the Gist:

PHP_VERSION='5.6'
CONTAINER_NAME='php_apache'

function php_apache-run() {
  docker run -d -v $PWD:/var/www/html --name $CONTAINER_NAME -p 8000:80 php:$PHP_VERSION-apache /bin/bash -c 'a2enmod rewrite; apache2-foreground'
}
alias php_apache-start="docker start $CONTAINER_NAME"
alias php_apache-stop="docker stop $CONTAINER_NAME"
alias php_apache-kill="docker rm -f $CONTAINER_NAME"
alias php_apache-logs="docker logs -t $CONTAINER_NAME"
alias php_apache-logs_live="docker logs -t -f $CONTAINER_NAME"

edited bug: alias php_apache-run="docker run -d -v $PWD:/var/www/html --name $CONTAINER_NAME -p 8000:80 php:$PHP_VERSION-apache /bin/bash -c 'a2enmod rewrite; apache2-foreground'"

  • Using aliases with _$PWD_ in _~.bashrc_ will result in the same value always, being used the one when the file was "sourced" (included)

In the Gist there are 2 more functions:

  1. A safest way to run the container (checking first if its already created)
  2. A "status" of the container (not created, started or stoped)

@bufface if your are interested in using the container with the volume of your actual folder, probably the script way is better, because in the end even with Dockerfile you'll have to write that long bash line to run the container with the port, volume, name, etc.

Even if u use docker-compose, where you can define the port and the volume, you'll have to hack a bit to get the right path used, because it uses absolute path's, relative to home folder, or relative to docker-compose.yml, so if you want your actual folder to be used you'll have to use and set environmental or bash variables, and again you'll end up writing messy bash or doing again some bash alias/function.

So I retract in my previous opinion about the Dockerfile, but I suggest using some form of bash alias/function to encapsulate it

Thanks @iamsortiz. Very useful your Gist.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sanjay-rakholiya picture sanjay-rakholiya  路  3Comments

PMExtra picture PMExtra  路  3Comments

dhoeric picture dhoeric  路  4Comments

mcnesium picture mcnesium  路  3Comments

2Kable picture 2Kable  路  3Comments