Hello guys im trying to setup an Dockerfile with ubuntu php7 , and Apache with workdir /app/web. phpmyadmin and mysql is already running but i dont get it how to setup the Dockerfile for ubuntu and php7 any suggestions? yeah i know there are lots of finished solutions on docker hub and git but i prefere to create my i own to know how it does work.
version: '2'
services:
#######################################
# PHP application Docker container
#######################################
app:
build:
context: .
dockerfile: Dockerfile
links:
- mysql
ports:
- "8000:80"
volumes:
#- ./app/:/app/
- ./:/docker/
volumes_from:
- storage
networks:
- php-network
#######################################
# MySQL server
#######################################
mysql:
build:
context: docker/mysql/
dockerfile: MySQL-5.7.Dockerfile
restart: always
volumes_from:
- storage
env_file:
- etc/environment.yml
networks:
- php-network
#######################################
# PHP MY ADMIN
#######################################
myphpadmin:
build:
context: docker/myphpadmin
dockerfile: Dockerfile
restart: always
links:
- mysql
ports:
- 8080:80
environment:
- PMA_HOST=mysql
- VIRTUAL_PORT=80
networks:
- php-network
storage:
build:
context: docker/storage/
volumes:
- /storage
networks:
php-network:
driver: bridge
Dockerfile
FROM ubuntu:latest
# Install apache, PHP, and supplimentary programs. openssh-server, curl, and lynx-cur are for debugging the container.
RUN apt-get update && apt-get -y upgrade && DEBIAN_FRONTEND=noninteractive apt-get -y install \
apache2 php7.0 php7.0-mysql libapache2-mod-php7.0 curl lynx-cur
# Enable apache mods.
RUN a2enmod php7.0
RUN a2enmod rewrite
# Update the PHP.ini file, enable <? ?> tags and quieten logging.
RUN sed -i "s/short_open_tag = Off/short_open_tag = On/" /etc/php/7.0/apache2/php.ini
RUN sed -i "s/error_reporting = .*$/error_reporting = E_ERROR | E_WARNING | E_PARSE/" /etc/php/7.0/apache2/php.ini
# Manually set up the apache environment variables
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
# Expose apache.
EXPOSE 80
# Copy this repo into place.
ADD www /var/www/site
# Update the default apache site with the config we created.
ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf
# By default start up apache in the foreground, override with /bin/bash for interative.
CMD /usr/sbin/apache2ctl -D FOREGROUND
Found this solution Had to include apache-config.conf
Related
I followed step by step this article ( https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose-on-ubuntu-20-04 ).
I just changed php version (8.1) instead of (7.4) and mysql version (8.0) instead of (5.7.22)
When I run (docker ps) command, digitalocean.com/php and nginx:alpine are fine. But mysql:8.0 is always restarting status.
But I tested firstly it in local development.
When local development, everything is okay.
Buy I deployed it in the droplet, I faced the error that MySQL is always restarting status.
When I run (docker logs -f ) command, I faced the following error.
2022-08-13 06:35:20+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
2022-08-13 06:35:20+00:00 [Note] [Entrypoint]: Switching to dedicated user ‘mysql’
2022-08-13 06:35:20+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
‘/var/lib/mysql/mysql.sock’ -> ‘/var/run/mysqld/mysqld.sock’
When I run (docker run --restart=always mysql:8.0) command, I faced the following error.
2022-08-13 06:57:37+00:00 [Note] [Entrypoint]: Entrypoint script - for MySQL Server 8.0.30-1.el8 started.
2022-08-13 06:57:38+00:00 [Note] [Entrypoint]: Switching to dedicated user ‘mysql’ 2022-08-13 06:57:38+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.30-1.el8 started.
2022-08-13 06:57:38+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following:
MYSQL_ROOT_PASSWORD
MYSQL_ALLOW_EMPTY_PASSWORD
MYSQL_RANDOM_ROOT_PASSWORD
Dockerfile
FROM php:8.1-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
libzip-dev
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:8.0
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: 123123
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
I have a working laravel environment using docker. my projects has multiple services in different container such as redis, mongodb, mysqldb and nodejs. I want to use supervisor on my project to interact with redis for the queues and php to run the job. I have done some testing and research but I really can't make it work.
so here is my DockerFile:
FROM php:7.3-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
mariadb-client \
libpng-dev \
libzip-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
cron \
supervisor
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-configure bcmath --enable-bcmath
RUN docker-php-ext-install bcmath
# install mongodb ext
RUN pecl install mongodb \
&& docker-php-ext-enable mongodb
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy supervisor configs
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
CMD ["/usr/bin/supervisord"]
# Change current user to www
USER www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
and my docker-compose.yml file
version: '3'
services:
#PHP Service
php:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: php
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: php
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
- ./supervisord.conf:/etc/supervisor/conf.d/supervisord.conf
networks:
- app-network
#NODEJS Service
nodejs:
image: node:10
container_name: nodejs
restart: unless-stopped
working_dir: /var/www
volumes:
- ./:/var/www
tty: true
networks:
- app-network
#Nginx Service
nginx:
image: nginx:alpine
container_name: nginx
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
mysqldb:
image: mysql:5.7.22
container_name: mysqldb
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_USER: ${DB_USERNAME}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql
- ./mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
#MongoDB Service
mongodb:
image: mongo:3
container_name: mongodb
restart: unless-stopped
tty: true
ports:
- "27017:27017"
networks:
- app-network
#Redis Service
redis:
image: redis
container_name: redis
restart: unless-stopped
tty: true
ports:
- "${REDIS_PORT}:6379"
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
you might also want to see my supervisord.conf
[supervisord]
user=www
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
pidfile=/var/run/supervisord.pid
loglevel = INFO
[unix_http_server]
file=/var/run/supervisor.sock
chmod=0700
username=www
password=www
[supervisorctl]
serverurl=unix:///var/run/supervisord.sock
username=www
password=www
[rpcinterface:supervisor]
supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface
[program:php-fpm]
command = /usr/local/sbin/php-fpm
autostart=true
autorestart=true
priority=5
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:ohwo-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/artisan horizon
autostart=false
autorestart=true
user=www
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/laravel-worker.log
so from that setup. when the containers is UP it seems that supervisord is not working because if I run php artisan horizon manually on my php container the queuing works perfectly. btw horizon is the tool i use for queuing.
and then I also try to run supervisorctl on my php container and I got this error unix:///var/run/supervisord.sock no such file
so I'm just pretty new to docker just started few months ago. I do know how to configure supervisord on linux but i can't make it work on docker.
so please pardon my stupidity :)
The idea here is to eliminate the supervisor and instead run whatever the supervisor used to run in several different containers. You can easily orchestrate this with docker-compose, for example, all running the same container with different CMD overrides, or the same container with a different CMD layer at the end to split it out. The trouble here is the supervisor won't be able to communicate the status of the processes it manages to Docker. It will always be "alive" even if all of its processes are completely trashed. Exposing those directly means you get to see they crashed.
What's best is to break out each of these services into separate containers. Since there's official pre-built ones for MySQL and so on there's really no reason to build one yourself. What you want to do is translate that supervisord config to docker-compose format.
With separate containers you can do things like docker ps to see if your services are running correctly, they'll all be listed individually. If you need to upgrade one then you can do that easily, you just work with that one container, instead of having to pull down the whole thing.
The way you're attacking it here is treating Docker like a fancy VM, which it really isn't. What it is instead is a process manager, where these processes just so happen to have pre-built disk images and a security layer around them.
Compose your environment out of single-process containers and your life will be way easier both from a maintenance perspective, and a monitoring one.
If you can express this configuration as something docker-compose can deal with then you're one step closer to moving to a more sophisticated management layer like Kubernetes which might be the logical conclusion of this particular migration.
According to official documentation:
There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.
And your Dockerfile has two CMD commands so the command php-fpm will override
/usr/bin/supervisord
So that you can execute PHP commands but can't find supervisor's socket created in the container.
You can fix your issue by deleting the last CMD command related to PHP-FPM as you already configured supervisor to start it and your Dockerfile should have one CMD command:
CMD ["/usr/bin/supervisord"]
I recommend that you familiarize yourself with this simple project on github, there is a configuration for a docker with a working supervisor web interface.
https://github.com/Staniczek/supervisord-docker
I currently have a problem running my apache web-server successfully while using Docker...
Here is my docker file:
FROM fedora:27
# Container Owner
MAINTAINER nzhiti#gmail.com
# Update & install Apache & clean dnf
RUN dnf upgrade -y
RUN dnf install -y httpd
RUN dnf clean packages
RUN dnf install -y mod_ssl
# Configuring hosts
ADD ./hosts/hosts /etc/hosts
# Port
EXPOSE 443
# Starting httpd
ENTRYPOINT ["/usr/sbin/httpd"] & CMD ["-D", "FOREGROUND"]
No errors during building. But when I try to compose it, it never works, and the only message outputted is apache exiting with code 0
version: '3'
services:
php-apache:
image : httpd_fedora
ports:
- 443:443
volumes:
- ./Apache/www/:/var/www/html
- ./Apache/vhosts/:/etc/httpd/conf.d/
- ./Apache/SSLcert/:/etc/httpd/ssl/
- ./Apache/errorlogs/error.log:/var/log/httpd/error.log
tty: true
I'm out of ideas...
Thanks,
DRK
Try indenting tty: true so it's aligned with php_apache properties. Also copy the Dockerfile to the same directory of docker-compose.yml, and change image to build: ..
version: '3'
services:
php-apache:
build: .
ports:
- 443:443
volumes:
- ./Apache/www/:/var/www/html
- ./Apache/vhosts/:/etc/httpd/conf.d/
- ./Apache/SSLcert/:/etc/httpd/ssl/
- ./Apache/errorlogs/error.log:/var/log/httpd/error.log
tty: true
i'm new to docker and slimframework, i'm trying to make a docker file or docker-compose file that when i send to someone else they can run and have my sql db running with the tables already created i dont know if this is right but i get an error.
dockerfile
FROM php:7.1-apache
COPY apache2.conf /etc/apache2
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
this is my docker-compose
version: '2'
volumes:
logs:
driver: local
services:
db-mysql:
image: mysql:5
ports:
- "3307:3306"
environment:
MYSQL_DATABASE: path
MYSQL_USER: root
MYSQL_PASSWORD: root
slim:
image: php:7-alpine
working_dir: /var/www
command: php -S 0.0.0.0:8080 -t public
environment:
docker: "true"
depends_on:
- db-mysql
ports:
- 80:8080
volumes:
- .:/var/www
- logs:/var/www/logs
I've created a PHP/Apache/MySQL development environment with Docker and would like to set variable that I can use with $_SERVER in PHP.
Usually I will configure something like that in my virtual host
SetEnv ENV "developement"
Is there a way to do it with my docker_compose.yml file ?
I'll try by using environment: - ENV=developement in my docker-compose file but it doesn't work.
Here is my Dockerfile
FROM php:5.6-apache
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
and my docker-compose.yml
version: '2'
services:
webserver:
build: ./docker/webserver
image: dev_web
ports:
- "80:80"
- "443:443"
volumes:
- /pathtodev/www:/var/www/html
links:
- db
environment:
- ENV=developement
db:
image: mysql:5.7
ports:
- "3306:3306"
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=******
- MYSQL_DATABASE=db_dev
Consider the below Dockerfile
FROM php:5.6-apache
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
RUN echo 'PassEnv FIRST_NAME' > /etc/apache2/conf-enabled/expose-env.conf
RUN echo '<?php echo $_SERVER["FIRST_NAME"];' > /var/www/html/first.php && echo '<?php echo $_SERVER["LAST_NAME"];' > /var/www/html/last.php
Now run the container using
docker run -it -e FIRST_NAME=TARUN -e LAST_NAME=LALWANI -p 80:80 4ba2aa50347b
Testing
$ curl localhost/first.php
TARUN
$ curl localhost/last.php
$
As you can see the only FIRST_NAME can be accessed, because we exposed the same using PassEnv directive in our apache config insider the container
I know this is a old thread, however, another solution could also be that inside your docker compose, you need to remove the "-" in front of ENV and then instead of "=" it needs to be ":".
It will end up looking like this then:
version: '2'
services:
webserver:
build: ./docker/webserver
image: dev_web
ports:
- "80:80"
- "443:443"
volumes:
- /pathtodev/www:/var/www/html
links:
- db
environment:
ENV: developement
db:
image: mysql:5.7
ports:
- "3306:3306"
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=******
- MYSQL_DATABASE=db_dev
At least it's working like that in version 3.