1

I'm trying to build an Apache-PHP + MySQL + PHPMyAdmin entities environment without success.

I reduce my project as simple as possible with thoses files :

test-docker $ ls -la
total 24
drwxrwxr-x  2 david david 4096 janv. 18 11:01 .
drwxrwxr-x 20 david david 4096 janv. 18 10:59 ..
-rw-rw-r--  1 david david  457 janv. 18 11:01 docker-compose.yml
-rw-rw-r--  1 david david   96 janv. 18 11:01 Dockerfile
-rw-rw-r--  1 david david  214 janv. 18 10:59 .env
-rw-rw-r--  1 david david   12 janv. 18 11:00 index.php

The content of each files :

test-docker $ cat Dockerfile
FROM php:7.0-apache

COPY . /var/www/html
WORKDIR /var/www/html

RUN apt-get update \
    && env

test-docker $ cat docker-compose.yml 
version: '2'

services:
  web:
    build: .
    depends_on:
      - db
    restart: always
    environment:
      MYSQL_ADDON_HOST: db

  db:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: shyrka
      MYSQL_USER: shyrka
      MYSQL_PASSWORD: shyrka

  dbadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - db
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
    restart: always

test-docker $ cat .env 
MYSQL_ADDON_DB=shyrka
MYSQL_ADDON_USER=shyrka
MYSQL_ADDON_PASSWORD=shyrka
MYSQL_ADDON_PORT=3306
MAILER_HOST=smtp.sparkpostmail.com
MAILER_USER=SMTP_Injection
MAILER_PASSWORD=d6ed7a00b2032116ef3aa14789759b959aade6e5

And here the result :

test-docker $ docker-compose up -d
Creating network "testdocker_default" with the default driver
Building web
Step 1 : FROM php:7.0-apache
 ---> 17c7673c5783
Step 2 : COPY . /var/www/html
 ---> a7987297ae7b
Removing intermediate container a4daf7dbc92a
Step 3 : WORKDIR /var/www/html
 ---> Running in d9b6a5426d83
 ---> ef3d9bb0f0b7
Removing intermediate container d9b6a5426d83
Step 4 : RUN apt-get update     && env
 ---> Running in 6aada591c2a2
Get:1 http://security.debian.org jessie/updates InRelease [63.1 kB]
[...]
Fetched 9856 kB in 2s (4408 kB/s)
Reading package lists...
PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2
APACHE_CONFDIR=/etc/apache2
HOSTNAME=45f28166fed1
PHP_INI_DIR=/usr/local/etc/php
PHP_EXTRA_BUILD_DEPS=apache2-dev
HOME=/root
PHP_LDFLAGS=-Wl,-O1 -Wl,--hash-style=both -pie
PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2
PHP_MD5=a51f1d4f03f4e4c745856e9f76fca476
PHP_VERSION=7.0.14
GPG_KEYS=1A4E8B7277C42E53DBA9C7B9BCAA30EA9C0D5763
PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2
PHP_ASC_URL=https://secure.php.net/get/php-7.0.14.tar.xz.asc/from/this/mirror
PHP_URL=https://secure.php.net/get/php-7.0.14.tar.xz/from/this/mirror
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PHPIZE_DEPS=autoconf        file        g++         gcc         libc-dev        make        pkg-config      re2c
PWD=/var/www/html
PHP_SHA256=0f1dff6392a1cc2ed126b9695f580a2ed77eb09d2c23b41cabfb41e6f27a8c89
APACHE_ENVVARS=/etc/apache2/envvars
 ---> 8c9978981124
Removing intermediate container 6aada591c2a2
Successfully built 8c9978981124
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating testdocker_db_1
Creating testdocker_web_1
Creating testdocker_dbadmin_1

As you can see neither environment variables specified in docker-composer.yml or in the .env file are set. And I do not know how to send "db" entity IP Adress to the "web" entity in enviro

I've followed the Docker documentation. I've certainly miss the point. I'm asking the community for help.

Thank you. David.

David
  • 113
  • 3

2 Answers2

2

It's because you're setting the environment variables at the docker-compose.yml but trying to reference them in build time (aka RUN env). Those variables will only be available after the image is built, in the ENTRYPOINT/CMD command.

You either need to set variables in build time using the ENV command in the Dockerfile or use the variables after the container has started (either way is fine in your case).

As to referencing db in the web service, you need to create a custom network (https://docs.docker.com/compose/compose-file/#/network-configuration-reference) and set the internal domain alias or reference the first container to the second using service links (https://docs.docker.com/compose/compose-file/#/links). The second seems more appropriate in your case, since the db service will not need to reference the web service.

0

as far as I know you need to set

env_file: .env

to have this running with .env files.

But what you are really missing is, that ENV files of docker compose are not available within your DOCKERFILE. Your Dockerfile creates an image, running this image in an container is with your .env files or those things.

  1. Step - Create Docker Image:

Just build your image with:

docker build -t NAMESPACE/NAME .

Replace Namespace and Name as you wish.

  1. Step - Run the Image as Container:

Just start docker-compose. You will now be able to see and use your envfiles within the given ENTRYPOINT / CMD.

You are not able to modify the build of an image with env variables, that is not the way docker works.

Greetings from our Codesprint in Basel,

Bastian