2

My goal is that running a PHP script with Docker compose.

I somehow found how to execute a PHP example script using Dockerfile like the following.

$ ls
Dockerfile  test.php

$ cat Dockerfile 
FROM php:7.0-cli
COPY ./test.php /tmp
WORKDIR /tmp
CMD [ "php", "./test.php" ]

$ cat test.php 
<?php
phpinfo();

$ docker build -t my-php-app .

$ docker run -it --rm --name my-running-app my-php-app

https://docs.docker.com/samples/library/php/

I'm looking into how to connect into MySQL from the PHP script.

Any help will be helpful for me.


Update 1

I had little progress. I was able to connect to the container of mysql from PHP container.

$ cat index.php 
<?php 
$mysqli = new mysqli("database", "admin", "12dlql*41");
echo $mysqli->server_info;

$ docker run -d --name database -e MYSQL_USER=admin -e MYSQL_PASSWORD=12dlql*41 -e MYSQL_RANDOM_ROOT_PASSWORD=true mysql:latest

$ docker run --rm -v $(pwd):/app -w /app --link database tommylau/php php index.php
5.7.21

https://www.shiphp.com/blog/2017/php-mysql-docker


Update 2

Thanks to everyone I was able to find the way.

Dockerfile

FROM php:7.1.9-fpm

RUN apt-get update \
  && docker-php-ext-install pdo_mysql mysqli

RUN apt-get update \
  && apt-get install -y libmemcached-dev zlib1g-dev \
  && pecl install memcached-3.0.3 \
  && docker-php-ext-enable memcached opcache

docker-compose.yml

version: '3.4'

services:
  myapp_memcached:
    image: memcached:latest
    container_name: memcached
  myapp_mysql:
    image: mysql:latest
    container_name: database
    volumes:
      - ./docker/:/etc/mysql/conf.d
      - ./docker/:/docker-entrypoint-initdb.d
    environment:
      - MYSQL_RANDOM_ROOT_PASSWORD=true
      - MYSQL_DATABASE=counterparty
      - MYSQL_USER=admin
      - MYSQL_PASSWORD=12dlql*41
  myapp_php:
    build: .
    container_name: myapp
    working_dir: /app
    volumes:
      - ./:/app
    external_links:
      - database
      - memcached
7
  • 1
    This could maybe help you although it's postgres: lvthillo.com/… Commented Jan 19, 2018 at 13:39
  • @lvthillo Thank you. I'll try it. It's must be useful to know docker. Commented Jan 19, 2018 at 13:43
  • Where is your docker-comppose.yml? Commented Jan 19, 2018 at 16:17
  • 1
    Try understanding docker first and then move forward with docker-compose. Bear in mind that docker-compose is built upon docker. If you don't understand docker, containers and linking of multiple containers, then docker-compose won't make much sense. Your question hence seems a bit too broad in that expect. Try asking questions that focus either on your specific docker problem. Right now, too much is muddled together here. Commented Jan 19, 2018 at 16:25
  • 1
    docker docs: docs.docker.com/engine/docker-overview docker-compose docs: docs.docker.com/compose/overview Commented Jan 19, 2018 at 16:27

1 Answer 1

1

You should set up a docker-compose.yml file, which would interconnect your containers into one network.

For example:

version: '3.4'

myapp_php:
  build: ./Dockerfile
  container_name: myapp_php
myapp_mysql:
  image: mysql:latest
  container_name: myapp_mysql
  environment:
    - MYSQL_ROOT_PASSWORD=somerandompassword
    - MYSQL_DATABASE=database
    - MYSQL_USER=admin
    - MYSQL_PASSWORD=12dlql*41

Now when you run docker-compose up -d, the stack should be up with mysql having pre-set a database with your credentials.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.