9

I use this to set up nginx for PHP:

nginx:
    image: nginx:latest
    ports:
        - 8080:80
    volumes:
        - ./code:/code
        - ./site.conf:/etc/nginx/conf.d/site.conf
    links:
        - php
php:
    image: php:7-fpm
    volumes:
        - ./code:/code

But how about Apache? How can I set up Apache + PHP in docker-compose.yml?

Following this guide:

version: '2'

services:
  php:
    build: php
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./php/www:/var/www/html

Error:

ERROR: In file './docker-compose.yml' service 'version' doesn't have any configuration options. All top level keys in your docker-compose.yml must map to a dictionary of configuration options.

Any ideas? I'm on Xubuntu 16.04.

EDIT:

After managing to upgrade docker-compose to 1.9, I try with this file below:

version: '2'
services:
    php:
        build: php
        expose:
            - 9000
        volumes:
            - ./php/www:/var/www/html

    apache2:
        image: webdevops/apache:latest
        args:
            - PHP_SOCKET=php:9000
        volumes:
            - ./php/www:/var/www/html
        ports:
            - 80:80
            - 443:443
        links:
            - php

Error:

$ sudo docker-compose up -d
Building php
ERROR: Cannot locate specified Dockerfile: Dockerfile

Docker is such as pain!

Any ideas how to fix this?

Run
  • 54,938
  • 169
  • 450
  • 748

7 Answers7

9

Since the example above does not work, here is a different approach: docker-compose.yml

version: '3.1'

services:
  php:
    image: php:apache
    ports:
      - 80:80
    volumes:
      - ./php/www:/var/www/html/

Launch the server with

docker-compose up
meles
  • 315
  • 1
  • 4
  • 13
7

I would choose webdevops dockerized apache, because it has simple configuration:

version: '2'
services:
    php:
        build: php
        expose:
            - 9000
        volumes:
            - ./php/www:/var/www/html

    apache2:
        image: webdevops/apache:latest
        args:
            - PHP_SOCKET=php:9000
        volumes:
            - ./php/www:/var/www/html
        ports:
            - 80:80
            - 443:443
        links:
            - php
rokas
  • 1,521
  • 9
  • 16
  • thanks. I have just tried that but I get the same error `ERROR: In file './docker-compose.yml' service 'version' doesn't have any configuration options. All top level keys in your docker-compose.yml must map to a dictionary of configuration options.` – Run Jan 02 '17 at 08:49
  • Looks like yours docker compose version is lower than 1.6. Docker compose version 2 is only available since 1.6 docker compose. Check version `docker-compose --version` – rokas Jan 02 '17 at 08:52
  • you are right - it is `docker-compose version 1.5.2, build unknown` so how can I upgrade it? – Run Jan 02 '17 at 08:53
  • 1
    Try this tut: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-14-04 – rokas Jan 02 '17 at 08:58
  • thanks but it doesn't mention about upgrading docker-compose to v2. – Run Jan 02 '17 at 09:00
  • 3
    For anyone stumbling on this later, the [Compose file version](https://docs.docker.com/compose/compose-file/compose-versioning/#versioning) does not match the `docker-compose` version, e.g. [`version: '2'`](https://docs.docker.com/compose/compose-file/compose-versioning/#version-2) corresponds to `docker-compose 1.6.0+` – CJ Harries Sep 26 '17 at 14:41
  • 3
    I get this error: "Unsupported config option for services.apache2: 'args'" – MikeiLL May 29 '18 at 19:42
  • Try this https://stackoverflow.com/a/45978518/6478501 – Andrii Sukhoi Mar 02 '23 at 13:48
1
  1. We need to create a new folders /php/www in current path

  2. Create a file under php folder save as "Dockerfile" which contains as below without quote

"FROM php:5.6-apache RUN docker-php-ext-install mysqli"

  1. Copy your docker-compose.yml file in your current folder where your "php" folder has.

  2. Create a sample file "index.php" under www folder (/php/www/index.php)

  3. Run in command prompt docker-compose up -d

  4. Open your browser type "localhost" you can see your sample file results.

Note: Above steps as per above mentioned docker-compose.yml file.

0

You can check this question. If you use build instead of image, then you need "Dockerfile". Dockerfile would be use as configuration file for building image.

You maybe miss part in guide, where you should create file with name "Dockerfile" inside directory "php". Directory "php" must be in the same directory, where your "docker-compose.yml". In "docker-compose.yml" you have this line.

build: php

The line mean, that configuration file (by default: "Dockerfile") is inside of directory "php". So you should create directory "php" and file "Dockerfile" inside of it.

This is "Dockerfile" from your guide.

FROM php:5.6-apache

RUN docker-php-ext-install mysqli

docker-compose.yml reference version 2

Dockerfile reference

0

I found an elegant way to dynamically configure the ports and other parameters: In apache2's configuration files you can reference environment variables.

#/etc/apache2/ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf
#APACHE_HTTP_PORT_NUMBER:80
#APACHE_HTTPS_PORT_NUMBER:443

Listen ${APACHE_HTTP_PORT_NUMBER}


<IfModule ssl_module>
    Listen ${APACHE_HTTPS_PORT_NUMBER}
</IfModule>

<IfModule mod_gnutls.c>
    Listen ${APACHE_HTTPS_PORT_NUMBER}
</IfModule>

you can set the variables in Dockerfile or docker-compose.yml

-1

You can set a directory with diferente Dockerfiles an declare in each service:

...
    image: php:custom
    build:
      context: .
      dockerfile: ./dockerfiles/Dockerfile-php
...
-1

I have created a working example of PHP, APACHE, MYSQL, and PHPMYADMIN for PHP developers. You may find it useful if you need the original old-school working style. Please note that I am using port 8080 for my website and port 8081 for PHPMyAdmin. You can change these as you like.

version: '3.8'


services:


 php-apache-environment:
container_name: php-apache
image: php:7.4-apache
volumes:
  - ./php/src:/var/www/html/
ports:
  - 8080:80
  db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
container_name: mysql
environment:
  MYSQL_ROOT_PASSWORD: admin
  MYSQL_DATABASE: ezapi
  MYSQL_USER: root
  MYSQL_PASSWORD: password
ports:
  - "6033:3306"
volumes:
  - dbdata:/var/lib/mysql


phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
links:
  - mysql
environment:
  PMA_HOST: mysql
  PMA_PORT: 3306
  PMA_ARBITRARY: 1
restart: always
ports:
  - 8081:80
volumes:


 dbdata:
jerryurenaa
  • 3,863
  • 1
  • 27
  • 17