Docker download code from git and run php internal server - php

I want to download a public github project and run a project file through internal php server, through docker.
This is my file so far:
FROM php:7.2-cli
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y git
RUN git clone https://github.com/mygit src
WORKDIR src
CMD ["php", "-S", "0.0.0.0:80", "-t", "/src/src/examples/image.php"]
The process does not show up in docker ps when I run:
docker build -t myimage .
docker run -d -p 8080:8080 myimage

Try with this Dockerfile:
FROM php:7.2-cli
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y git
RUN git clone https://github.com/douma/langtons-ant src
WORKDIR src
ENTRYPOINT ["php"]
CMD ["-S", "0.0.0.0:8080","/src/src/examples/image.php"]
Note I don't know php but checking your github project's readme I think you do not need the -t argument.
I also added ENTRYPOINT command to make your Dockerfile clearer. For differences check this.
The build and run commands should be the same as these you posted.
docker build -t myimage .
docker run -d -p 8080:8080 myimage

Related

dockerfile composer install does not work

I couldn't find a solution to this problem on the internet. I wrote a dockerfile file below.
FROM php:7.3.6-fpm
RUN docker-php-ext-install pdo_mysql
RUN apt-get update \
&& apt-get install -y sudo \
&& apt-get install -y \
curl \
sed \
zlib1g-dev \
git \
zip \
unzip \
nano
RUN cd ~
RUN sudo curl -sS https://getcomposer.org/installer | php -- --
install-dir=/usr/local/bin --filename=composer
RUN echo 'alias api="php api"' >> ~/.bashrc
RUN echo 'cd /var/www/html/app' >> ~/.bashrc
WORKDIR /var/www/html/app
COPY composer.json composer.json
COPY composer.lock composer.lock
RUN composer install
# Copy codebase
COPY . ./
running this code, installing dependencies with composer install doesn't seem to be a problem at all.
docker-compose up -d
and then
docker exec -it php /bin/bash
and then
cd app
vendor does not appear when I enter the directory with this command.I don't understand what the reason is. Can you help me how to solve the problem?

custom make install in Docker file

Not able to run make install from docker file, not sure where I am going wrong.
Getting below error message when docker-compose build is run in ubuntu
Step 6/10 : RUN make install
---> Running in c8f67e8de3b5
make: *** No rule to make target 'install'. Stop.
ERROR: Service 'web' failed to build:
The command '/bin/sh -c make install' returned a non-zero code: 2
I have the below structure where server is laravel project and in root there is a docker-compose.yml file.
Below is the structure of server folder
docker-compose.yml file
version: '3'
services:
web:
image: api_server
container_name: api_server
build:
context: ./server/release
ports:
- 9000:80
volumes:
- ./server:/var/www/app
Dockerfile
FROM php:7.3.1-apache-stretch
RUN apt-get update -yqq && \
apt-get install -y apt-utils zip unzip && \
apt-get install -y nano && \
apt-get install -y libzip-dev libpq-dev libpng-dev&& \
a2enmod rewrite && \
docker-php-ext-install gd mbstring && \
docker-php-ext-install pdo_pgsql && \
docker-php-ext-install pgsql && \
docker-php-ext-configure zip --with-libzip && \
docker-php-ext-install zip && \
rm -rf /var/lib/apt/lists/*
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
COPY default.conf /etc/apache2/sites-enabled/000-default.conf
WORKDIR /var/www/app
RUN make install
RUN chown -R www-data:www-data /var/www/app/
RUN chmod -R 777 /var/www/app/storage
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
EXPOSE 80
server/Makefile
install:
# some other commands
composer install
refresh:
# For testing
php artisan migrate:refresh
php artisan db:seed
make run:
php artisan migrate:status
php artisan serve
Basically if I comment RUN make install and RUN chmod -R 777 /var/www/app/storage commands from Dockerfile it works. To test i'll get into the container and run the above 2 commands manually then it works by showing the default laravel page.
So I feel trying to run the above 2 commands from Dockerfile i guess source code isn't available at that point.

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"]

Permission Issue in Docker container for Symfony2

I'm tring to create an Docker image to bootstrap Symfony project.
Here is my Dockerfile:
FROM php:7-apache
LABEL Description = "This image is used to start Symfony3 project"
ENV DIRPATH /var/www/html
# apt-get command
RUN apt-get update && apt-get install -y \
vim \
git
RUN apt-get install -y zlib1g-dev && docker-php-ext-install zip
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
# Install the Symfony Installer
RUN curl -LsS http://symfony.com/installer -o /usr/local/bin/symfony
RUN chmod a+x /usr/local/bin/symfony
# Create the php.ini file
RUN cp /usr/src/php/php.ini-development /usr/local/etc/php/php.ini
The build and the container creation works well but I have a permission issue in my container.
When I'm going to my app_dev.php, I have this message:
You are not allowed to access this file. Check app_dev.php for more information.
Apparently, I can access this file only with localhost.
Also, PHP can't delete or create anything in my container.
For exemple I have the following error when I'm running:
$php app/console cache:clear
Failed to remove directory "/var/www/html/app/cache/dev_old/doctrine
How can I solved that in my Dockerfile?
Finally found it after weeks:
Add that in you Dockerfile. It solved the permission issue.
# Workaround for write permission on write to MacOS X volumes
# See https://github.com/boot2docker/boot2docker/pull/534
RUN usermod -u 1000 www-data

How can I run my docker container with installed Nginx?

I have docker image with Dockerfile, that successfully build with docker build . command. The Dockerfile content is:
FROM ubuntu
RUN apt-get update && apt-get install -y nginx php5 php5-fpm
ADD . /code
How can I run my docker container to see that Nginx is work?
UPDATE: When I try to use next Dockerfile:
FROM ubuntu
RUN apt-get update && apt-get install -y nginx php5 php5-fpm
RUN sudo echo "daemon off;" >> /etc/nginx/nginx.conf
CMD service php5-fpm start && nginx
It build successfully with docker build -t my/nginx ., but when I enter docker run --rm -ti my/nginx command, my terminal not response:
When you build the image you probably want to specify the image name with -t option.
docker build -t my/nginx .
To run a container use the run command
docker run --rm -ti my/nginx
You probably should add the following command to your Dockerfile
CMD ["nginx"]
Or with php5-fpm
CMD service php5-fpm start && nginx
UPDATE.
You should run nginx as daemon off. Add the following to your Dockerfile after installing nginx.
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
Update2.
-ti option in run allows you to check the log messages if any.
Usually you should run a container in background using -d instead of -ti.
You can attach to a running container using the attach command.
You may also check docker reference to see how to stop and remove a container and other commands.

Categories