Cannot expose port from container to host, using PHP static webserver - php

I have this multi-stage build in a Dockerfile:
## Stage 1
FROM node:9 as builder
RUN apt-get update && apt-get -y install sudo
RUN echo "newuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN useradd -ms /bin/bash newuser
USER newuser
RUN mkdir -p /home/newuser/app
WORKDIR /home/newuser/app
RUN sudo chown -R $(whoami) $(npm config get prefix)/lib
RUN npm set progress=false
RUN npm config set depth 0
RUN npm cache clean --force
COPY dist .
## Stage 2
FROM php:5.6.30
RUN apt-get update && apt-get -y install sudo
RUN echo "newuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN useradd -ms /bin/bash newuser
USER newuser
RUN mkdir -p /home/newuser/app
WORKDIR /home/newuser/app
COPY --from=builder /home/newuser/app /home/newuser/app
RUN ls -a /home/newuser/app
CMD ["php", "-S", "localhost:3000"]
the image build successfully, using:
docker build -t x .
Then I run it with:
docker run -p 3000:3000 x
but when I go to localhost:3000 on the host machine, I don't get a response. The webpage is blank.
Does anyone know why that might happen?
I also tried:
CMD ["sudo", "php", "-S", "localhost:80"]
and
docker run -p 3000:80 x
and a few other variations, still nothing.

Not sure why the PHP static server wasn't working, but I got it working with Node.js static server package:
FROM node:9 as builder
RUN apt-get update && \
apt-get -y install sudo
RUN echo "newuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN useradd -ms /bin/bash newuser
USER newuser
RUN mkdir -p /home/newuser/app
WORKDIR /home/newuser/app
RUN sudo chown -R $(whoami) $(npm config get prefix)/lib
RUN sudo chown -R $(whoami) $(npm config get prefix)/lib/node_modules
RUN sudo chown -R $(whoami) $(npm config get prefix)/bin
RUN sudo chown -R $(whoami) $(npm config get prefix)/share
RUN sudo chown -R $(whoami) /usr/local/lib
RUN npm set progress=false
RUN npm config set depth 0
RUN npm cache clean --force
COPY dist .
RUN npm install -g http-server
ENTRYPOINT ["http-server", "."]
you build it like so:
docker build -t x .
and then run it like so:
docker run -p 3000:8080 x

Related

How to map composer vendor folder to docker volume ? (case of Symfony bundle linked to my projet)

I am stuck with that problem.
I have a docker compose file with volumes mapping that way :
volumes:
- ./:/var/www/html:rw
- ../epossobundle/:/var/www/epossobundle :rw;
In composer.json, there is a repo linked on a bundle which I am working on it like this :
"repositories": [
{
"type": "path",
"url": "../epossobundle"
},
But when I start my container I got the error
Warning: include(/var/www/html/vendor/composer/../epo/api-auth-sso-bundle/EpoApiAuthSsoBundle.php): failed to open stream: No such file or directory
How can I do ??
PS : It is not possible to install something in Docker image because it is intranet network
Thanks a lot
Serge
I assume that the vendor file is missing dependencies, specifically epo/api-auth-sso-bundle/EpoApiAuthSsoBundle.php so you'll need to install them.
Grab a root shell inside the container:
docker exec -it -u root <container_id_or_name> /bin/bash
Install composer (requires root):
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Then install your dependencies (if not in production, no need for --no-dev):
composer install --no-dev
Finally, change the permissions back (ls -rvla):
# Nginx
chown -R nginx:nginx .
# Apache2
chown -R www-data:www-data .
Note: This is a "hacky-fix", if you rebuild the container you will have to do this every time. You should look to do this inside your Dockerfile as a permanent solution.
Untested example of the Dockerfile:
FROM php:7.4-fpm
WORKDIR /var/www/html
# Nginx
RUN groupadd -g 101 nginx && \
useradd -u 101 -ms /bin/bash -g nginx nginx
# Apache2
RUN groupadd -g 101 www-data && \
useradd -u 101 -ms /bin/bash -g www-data www-data
# Download and install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Nginx
USER nginx
# Apache2
USER www-data
# Or add this as an entry-point (runs as user for permissions)
RUN composer install --no-dev

000-default.conf not found while building docker image of Laravel app on windows

I am trying to deploy my Laravel application on AWS using docker image by following given tutorial:
https://medium.com/#okekedesmond/deploying-containerized-laravel-application-using-aws-ec2-instances-with-docker-and-rds-883e8f6d6245
I am stuck at first point while building docker image. Here is my Dockerfile:
# Defining the base image for our project, if you understand how docker images and layers work, this should not be difficult to understand.
FROM php:7.3-cli
# We need to update the image and install some import packages
RUN apt-get update -y && apt-get install -y openssl zip unzip git curl libpng-dev libonig-dev libxml2-dev
# cleaning packages and install scripts
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Installing composer which is used to install Laravel
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin –filename=composer
#Creating a configuration file for apache and linking
ADD 000-default.conf /etc/apache2/sites-available/
RUN ln -sf /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-enabled/000-default.conf
#Restarting Apache
RUN a2enmod rewrite
RUN service apache2 restart
# Create a work directory and copy all project file into the
WORKDIR /var/www/app/
COPY . /var/www/app
#Granting permissions to files and folders
RUN chmod -R o+w /var/www/app/storage
RUN chown -R www-data:www-data ./storage
RUN chgrp -R www-data storage bootstrap/cache
RUN chmod -R ug+rwx storage bootstrap/cache
RUN chmod -R 755 /var/www/app/
RUN find /var/www/app/ -type d -exec chmod 775 {} \;
RUN chown -R www-data:www-data /var/www
# Installing dependencies from laravel package
RUN composer install --no-scripts --no-autoloader --no-ansi --no-interaction --working-dir=/var/www/app
#Running some packages
RUN docker-php-ext-install mbstring pdo pdo_mysql mbstring exif pcntl bcmath gd opcache
#Running Laravel on docker, because we are using the php-7.3-cli so we have to use a php server in our docker image
CMD php artisan serve --host=0.0.0.0 --port=80
EXPOSE 80`
It is giving error at step no. 5:
Step 5/21 : ADD 000-default.conf /etc/apache2/sites-available/
ADD failed: file not found in build context or excluded by .dockerignore: stat 000-default.conf: file does not exist
How do I solve it on windows? any help will be appreciated
I think the best solution is to manually make a new one, here is an example file:
https://gist.github.com/tjtoml/942d696c868b22a25259
And it should be located in:
/etc/apache2/sites-available/000-default.conf
It could be that you have to customize it for your needs

Nginx process and php-fpm share volumes

I have problems with a share volumes that both nginx apline and php-fpm can write and delete folder and file.
Nginx container will create static file on var/run.
FROM nginx:alpine
RUN set -x ; \
addgroup -g 82 -S www-data ; \
adduser -u 82 -D -S -G www-data www-data && exit 0 ; exit 1
RUN chmod 777 -R /var/run
RUN mkdir -p /var/run/static-cache/
I also create a same directory on php-fpm container so it can clear all the cache when I update a page.
FROM php:7.4-fpm-alpine
RUN apk add shadow && usermod -u 1000 www-data && groupmod -g 1000 www-data
RUN chmod 777 -R /var/run
RUN mkdir -p /var/run/static-cache.
WORKDIR /var/www/html/
A mount volume will link these two directories between 2 containers.
However, when I save a page, I'm not able to clear any folder on php-fpm container due to permision denied.
Is ther any solutions to solve arround this?

Add mysql DB on ubuntu container

I would like to add my mysql database to my Dockefile running ubuntu with installed mysql and php. The folder is structured in this way:
folder
|_ Dockerfile
|_ start.sh
|_ index.php
|_ init.sql
the problem is that start.sh don't exec all of my code, so the init.sql is not imported in mysql. I have looked some solution on StackOverflow but no one of them worked.
this is the Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get upgrade -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install apache2 php libapache2-mod-php php-mysql mysql-server -y
VOLUME ["/var/lib/mysql"]
ADD init.sql /tmp/init.sql
COPY index.php /var/www/html/
COPY start.sh /bin/
RUN chmod +x /bin/start.sh
EXPOSE 80 3306
CMD "/bin/start.sh"
This is the start.sh
#!/bin/bash
/usr/sbin/apache2ctl -D FOREGROUND
/usr/bin/mysqld_safe
mysql -u root -e "CREATE DATABASE mydb"
mysql -u root mydb < /tmp/init.sql
I'm not sure the problem is exactly start.sh, anyway every docker-compose solution will be useless for me since i need everything in a Dockerfile
first you must reorder sequence in you start.sh , because apache in foreground will prevent you to run mysql .
#!/bin/bash
chown -R mysql:mysql /var/lib/mysql /var/run/mysqld && /etc/init.d/mysql start
while [ ! -e /var/run/mysqld/mysqld.sock ]
do
sleep 2
done
mysqladmin ping
mysql -u root -e "CREATE DATABASE mydb"
mysql -u root mydb < /tmp/init.sql
/usr/sbin/apache2ctl -D FOREGROUND
and you must modify your Dockerfile to add :
RUN mkdir /var/run/mysqld ; chown mysql.mysql /var/run/mysqld

Docker stack and Doctrine migration

I have a simple docker stack with three containers : a mysql one, a php-fpm one and a nginx.
I just want to be able to execute my doctrine migrations and a symfony command every time my container is created.
Currently, i got an error like : "entrypoint.sh not found" when i type docker logs #myphpcontainername# after building and docker-compose up -d 'ing.
My php-fpm docker file :
FROM php:7.2-fpm
RUN apt-get update
RUN apt-get install -y zlib1g-dev libpq-dev git libicu-dev libxml2-dev \
......
RUN curl --insecure https://getcomposer.org/composer.phar -o /usr/bin/composer && chmod +x /usr/bin/composer
WORKDIR /var/www/symfony
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ['/entrypoint.sh']
And my entrypoint.sh, located at the same place than my Dockerfile :
#!/bin/bash
set -e
sleep 5
php /var/www/symfony/bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration
exec "$#"
I believe you will need to make that file executable
RUN chmod 0700 entrypoint.sh
or it should be
ENTRYPOINT /entrypoint.sh
Take a look at this post that has a good explanation
https://www.ctl.io/developers/blog/post/dockerfile-entrypoint-vs-cmd/
Try this
ADD . /var/www/symfony
WORKDIR /var/www/symfony
RUN chmod +x entrypoint.sh
...
CMD ["/var/www/symfony/entrypoint.sh"]

Categories