I have a simple Dockerfile for php:7.2-fpm that is being referenced by my docker-compose.yml configuration. I want to get rid of the Dockerfile which is located at php/Dockerfile and integrate the commands to my docker-compose-yml file.
Here is my php/Dockerfile:
FROM php:7.2-fpm
RUN docker-php-ext-install mysqli
And here is my docker-compose.yml which brings up php-fpm, nginx, letencrypt certbot, mysql, and phpmyadmin:
version: '3'
services:
php:
container_name: dev_php
build:
context: ./php
ports:
- 9000:9000
volumes:
- ./website:/website
- ./php/www.conf:/usr/local/etc/php-fpm.d/www.conf
environment:
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
nginx:
container_name: dev_nginx
image: nginx:1.13.8
ports:
- 80:80
- 443:443
volumes:
- ./website:/website
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
depends_on:
- php
- mysql
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
certbot:
container_name: dev_certbot
image: certbot/certbot
volumes:
- ./certbot/conf:/etc/letsencrypt
- ./certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
mysql:
container_name: dev_mysql
image: mysql:5.7
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
volumes:
- ./mysql:/docker-entrypoint-initdb.d
phpmyadmin:
container_name: dev_pma
image: phpmyadmin/phpmyadmin
links:
- mysql
depends_on:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
PMA_ARBITRARY: 1
restart: always
ports:
- 8183:80
Since my php/Dockerfile is relatively simple, I thought I could do something like this:
php:
container_name: dev_php
image: php:7.2-fpm
ports:
- 9000:9000
volumes:
- ./website:/website
- ./php/www.conf:/usr/local/etc/php-fpm.d/www.conf
environment:
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
command: bash -c "docker-php-ext-install mysqli"
But this does not work. When I bring my project up using docker-compose up I can see dev_php (the name of the PHP container) perform a bunch of tasks that I do not ordinarily see, it then proceeds to install mysqli but then quits immediately after with exit code 0.
Thoughts?
Your original Dockerfile does much more than just installing the mysqli extension.
It has the entrypoint as shown in the source code here
It also has the command as shown here
These entrypoint and command are what will start the container and keep it running
Now back to your modification:
I can see dev_php (the name of the PHP container) perform a bunch of tasks that I do not ordinarily see
This is the output of docker-php-ext-install mysqli. You did not see it before is because it was done when building the docker image.
quits immediately after with exit code 0
This is expected because the command has finished processing and there is nothing else for it to do.
In order to keep the container running, you will have to manually add in the original entrypoint and command. Something like this:
php:
container_name: dev_php
image: php:7.2-fpm
ports:
- 9000:9000
volumes:
- ./website:/website
- ./php/www.conf:/usr/local/etc/php-fpm.d/www.conf
environment:
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
entrypoint:
- bash
- -c
command:
- |
docker-php-ext-install mysqli; \
docker-php-entrypoint php-fpm
I have not tested to confirm that the syntax is 100% correct but you should get the idea. The important bit is docker-php-entrypoint php-fpm
My recommendation is to just stick to using Dockerfile in this case.
A good side effect is that Docker will cache the build layers so you do not need to wait for the installing process of mysqli every time you want to start the containers.
You may consider enabling the extension too
Related
It seems that when i run Docker compose up, docker is not reading from docker-compose.yml.
It seems like it is loading images from cache or i don't think where is finding them.
Bellow is my docker-compose.yml
version: '3'
services:
httpd:
image: httpd:latest
user: root
ports:
- "80:80" # Default Apache port (Default on PHP 7.4)
- "8073:8073" # PHP 7.3 Apache port
- "8074:8074" # PHP 7.4 Apache port
- "8081:8081" # PHP 8.1 Apache port
volumes:
- ./:/var/www/html/myApp/:rw
- ./dev/Docker/httpd/httpd.conf:/usr/local/apache2/conf/httpd.conf
restart: on-failure
container_name: httpd
networks:
- mb-frontend
php8.1-fpm:
build: ./dev/Docker/php-fpm/8.1
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php8.1-fpm
networks:
- mb-frontend
- mb-backend
php7.4-fpm:
build: ./dev/Docker/php-fpm/7.4
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php7.4-fpm
networks:
- mb-frontend
- mb-backend
php7.3-fpm:
build: ./dev/Docker/php-fpm/7.3
user: root
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
volumes:
- ./:/var/www/html/myApp/:rw
restart: on-failure
container_name: php7.3-fpm
networks:
- mb-frontend
- mb-backend
db:
image: mariadb:10.3.5
environment:
MYSQL_ROOT_PASSWORD: myPassword
MYSQL_USER: dev
MYSQL_PASSWORD: myPassword
ports:
- "3306:3306"
volumes:
- /root/Bureau/mysql:/var/lib/mysql/:rw
- ./dev/Docker/mariadb/conf.d/:/etc/mysql/conf.d/:rw
- ./dev/Docker/mariadb/config/init.sql:/docker-entrypoint-initdb.d/init.sql
restart: on-failure
container_name: db
networks:
- mb-backend
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: db
volumes:
- /root/Bureau/phpmyadmin:/var/lib/mysql/
networks:
- mb-backend
depends_on:
- db
redis:
image: redis:6.2
container_name: redis
ports:
- "6379:6379"
networks:
- mb-backend
networks:
mb-frontend:
driver: bridge
mb-backend:
driver: bridge
I commented some images on docker-compose.yml but when i tape the command Docker compose up on terminal, all images even commented images are Up.
Can anyone help me how i force docker to read images from the edited docker-compose.yml
Good practice would be do use:
docker compose down
and then
docker compose up
UPDATE:
Next I would suggest to clean up your containers:
List all containers:
docker ps -a
Remove those you don't want because they might still be in the system
docker rm <CONTAINER ID/NAME>
How to Stop & Remove a running container by ID or Name?
The command is actually docker compose up.
The command docker-compose has been deprecated as of latest version. We can now use docker compose without the hyphen(-).
You can use docker-compose -f <path-to-compose-file> to pass in the compose file.
Example:
docker compose -f docker-compose.yml up
Reference documentation: https://docs.docker.com/compose/reference/
When loading docker-compose up, wordpress loads on the url but the mysqli_connect function is undefined because of the absence of the extension.
I have tried to add the following under the fpm image
command: "docker-php-ext-install mysqli"
I have tried to add a Dockerfile into the directory of the docker-compose.yml file containing
version: "3"
services:
#database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# webserver
nginx:
image: nginx:latest
ports:
- "8080:80"
links:
- fpm
volumes:
- /Users/connergesbocker/Github/cbgesbocker/dotfiles/root/etc/nginx/conf.d/village.conf:/etc/nginx/conf.d/village.conf
- /Users/connergesbocker/WordPress:/WordPress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- "8888:8888"
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: wordpress
networks:
- wpsite
fpm:
image: php:5.6.20-fpm
ports:
- "90:9000"
# command: "docker-php-ext-install mysqli"
links:
- db
volumes:
- /Users/connergesbocker/WordPress:/WordPress
working_dir: "/"
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:```
You could customize your dockerfile & add install in it:
Dockerfile:
FROM php:5.6.20-fpm
RUN docker-php-ext-install mysqli
Part of docker-compose.yaml:
fpm:
build: .
image: myphp:1
ports:
- "90:9000"
# command: "docker-php-ext-install mysqli"
links:
- db
volumes:
- /Users/connergesbocker/WordPress:/WordPress
working_dir: "/"
networks:
- wpsite
your override will install the extension but the container will stop after installation because php-fpm will not start by overriding CMD. Replace the command with below one.
command:
- /bin/sh
- -c
- |
docker-php-ext-install mysqli
echo "extension installed.....starting php-fpm........................................"
php-fpm
Error:
Fatal error: Uncaught Error: Class 'mysqli' not found in /var/www/html/index.php:16 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 16
I made the index.php file and PDO mysql connection example code. Everything working as expected, except MySQLi connection. I added docker-php-ext-install run command to docker file but that did not help.
Here is my PHP docker file:
FROM php:7.0-fpm
RUN docker-php-ext-install pdo pdo_mysql mysqli
RUN docker-php-ext-enable mysqli
My docker-compose.yml file:
nginx:
build: ./nginx/
container_name: nginx-container
ports:
- 8080:80
links:
- php
volumes_from:
- app-data
php:
build: ./php/
container_name: php-container
expose:
- 9000
links:
- mysql
volumes_from:
- app-data
app-data:
#image: php:7.0-fpm
build: ./php/
container_name: app-data-container
volumes:
- ./www/html/:/var/www/html/
- ./nginx/nginx:/etc/nginx/conf.d
command: “true”
mysql:
#image: mysql:5.7
build: ./mysql/
container_name: mysql-container
expose:
- 3306
ports:
- 3306:3306
volumes_from:
- mysql-data
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: zavrel_db
MYSQL_USER: user
MYSQL_PASSWORD: password
mysql-data:
image: mysql:latest
container_name: mysql-data-container
volumes:
- /var/lib/mysql
command: "true"
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin-container
ports:
- 8888:80
links:
- mysql
environment:
PMA_HOST: mysql
Thank you.
Try change name of packet
php-mysqli
I solved the problem by deleting all images and containers. After that, I started and everything is fine.
docker rmi $(docker images -a -q)
docker rm $(docker ps -a -q)
I found the problem, the package is RUN pdo_mysqliso
i've added this RUN docker-php-ext-install pdo_mysqli in my docker-compose
I have a Wordpress site on live server and I want to create a LAMP stack locally with Docker to test things.
I pull the images of php:7.0-apache and mysql:5.7, the same versions on live.
I create a MySQL container:
docker run -d --name achi-mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
I create a php & apache container and link it with MySQL:
docker run -d --name achi-php-apache --link achi-mysql:mysql -p 8080:80 -v /home/achi/workspace/web/wordpress-template/:/var/www/html php:7.0-apache
I get the following error on localhost:8080:
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /var/www/html/wp-includes/wp-db.php:1564 [...]
Do I link these two containers the wrong way?
Your problem is not the connection between your containers. The problem is your PHP / Apache container which doesn't support mysqli (or PDO MySQL). WordPress can't find another function to connect with a MySQL database or your MySQL container. Instead WordPress is using a deprecated and removed (since PHP 7.0) mysql_ function per default. You need to install at least mysqli on your PHP container (explained below).
I also recommend to use a docker-compose file to install and run all containers with one command.
To create the containers you want, you can use the following docker-compose.yml file:
version: "3"
services:
achi-php-apache:
build:
context: ./
container_name: achi-php-apache
ports:
- "8080:80"
volumes:
- /home/achi/workspace/web/wordpress-template:/var/www/html:rw
depends_on:
- achi-mysql
networks:
- wp-net
achi-mysql:
image: mysql:5.7
container_name: achi-mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wp-dbname
volumes:
- wp-mysql-data:/var/lib/mysql
networks:
- wp-net
networks:
wp-net:
driver: bridge
volumes:
wp-mysql-data:
You need the following Dockerfile on the same directory as the docker-compose.yml file:
FROM php:7.0-apache
RUN docker-php-ext-install -j$(nproc) mysqli
This Dockerfile installs the missing mysqli extension so WordPress can use it.
You can also use PDO MySQL instead of mysqli. In this case you can use the following Dockerfile:
FROM php:7.0-apache
RUN docker-php-ext-install -j$(nproc) pdo
RUN docker-php-ext-install -j$(nproc) pdo_mysql
Now you can execute the command docker-compose up inside the folder where the docker-compose.yml file is located. After creating the container and running you should be able to access the WordPress site (<ip-or-hostname>:8080).
On the wp-config.php file you need to use the following constants:
define('DB_NAME', 'wp-dbname');
define('DB_USER', 'root');
define('DB_PASSWORD', '123456');
define('DB_HOST', 'achi-mysql');
You can also use the official WordPress image to install WordPress. In this case you can use the following docker-compose.yml file:
version: "3"
services:
achi-php-apache:
image: wordpress:4.9.4-php7.0-apache
container_name: achi-php-apache
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: achi-mysql
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: 123456
WORDPRESS_DB_NAME: wp-dbname
volumes:
- /home/achi/workspace/web/wordpress-template:/var/www/html:rw
depends_on:
- achi-mysql
networks:
- wp-net
achi-mysql:
image: mysql:5.7
container_name: achi-mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wp-dbname
volumes:
- wp-mysql-data:/var/lib/mysql
networks:
- wp-net
networks:
wp-net:
driver: bridge
volumes:
wp-mysql-data:
The simplest way is to use docker-compose to link all your docker instances together rather than linking through the docker command. Here is a sample docker-compose.yml file that should do what you want:
version: '2'
services:
achi-php-apache:
image: php:7.0-apache
ports:
- "8080:80"
volumes:
- /home/achi/workspace/web/wordpress-template/:/var/www/html
links:
- achi-mysql
achi-mysql:
image: mysql:5.7
volumes:
- /var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: someuser
MYSQL_PASSWORD: somepassword
MYSQL_DATABASE: somedefaultdatabase
I have this docker stack running nginx, php, and mariadb.
Now I want to add NodeJS in order to migrate my platform to services, one at the time.
I'm trying to add node in the same way I added php,but when I run "docker-compose up", nodejs appears "Exited with code 254".
It appears to be a ENOET error related with my package.json file, but I'm already coping my file into the container, also if I run everything less nodejs, when I access my nginx container the package.json appears in the route it means to be, so I don't know why I'm seeing this error.
Here is my docker-compose.yml
nginx:
image: tutum/nginx
ports:
- "80:80"
- "8080:8080"
# - "8081:8081"
# - "8082:8082"
# - "8083:8083"
# - "8090:8090"
links:
- nodejs
- phpfpm
- mariadb
volumes:
- ./public/leal-api:/var/www
- ./nginx/default:/etc/nginx/sites-available/default
- ./nginx/default:/etc/nginx/sites-enabled/default
- ./public:/usr/share/nginx/html
nodejs:
command: npm start
build: ./node
ports:
- "3000:3000"
links:
- mariadb
volumes:
- ./public/leal-api:/var/www
phpfpm:
build: ./php
ports:
- "9000:9000"
links:
- mariadb
volumes:
- ./public:/usr/share/nginx/html
mariadb:
image: mariadb
environment:
MYSQL_DATABASE: leal
MYSQL_USER: lealadm
MYSQL_PASSWORD: leal2015*
MYSQL_ROOT_PASSWORD: LealColombia2017!
ports:
- "3306:3306"
And here is the Dockerfile of the nodejs
FROM node:7.10
VOLUME ["/var/www"]
# WORKDIR /src
WORKDIR /var/www
# COPY . /src
# RUN npm install
# RUN npm install -g nodemon #hmm idk
EXPOSE 3000
CMD ["npm", "start"]
Thanks in advance for any help that you can provide.