docker-compose volume mount is empty when using docker toolbox (windows) - php

I am trying to dockerise my laraver 5.5 application using docker-compose.
Here's my docker-compose.yml file definition:
version: '2.1'
services:
# The Database
database:
image: mysql:5.7
restart: always
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10
environment:
- "MYSQL_DATABASE=myapp"
- "MYSQL_USER=myapp"
- "MYSQL_PASSWORD=123456"
- "MYSQL_ROOT_PASSWORD=secret"
ports:
- "33061:3306"
# The Application
app:
depends_on:
database:
condition: service_healthy
build:
context: ./
dockerfile: ./docker-compose/app.dockerfile
volumes:
- ./:/var/www/html
environment:
- "DB_CONNECTION=mysql"
- "DB_HOST=database"
- "DB_PORT=3306"
- "DB_DATABASE=myapp"
- "DB_USERNAME=myapp"
- "DB_PASSWORD=123456"
ports:
- "8080:80"
and this is my ./docker-compose/app.dockerfile:
# Base image
FROM php:7.1-apache
# Configure system
RUN apt-get update && apt-get install -y \
libmcrypt-dev \
mysql-client \
zlib1g-dev \
--no-install-recommends \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-install mcrypt pdo_mysql
# Add php.ini and apache2.conf
COPY docker-compose/php.ini $PHP_INI_DIR/php.ini
COPY docker-compose/apache2.conf /etc/apache2/apache2.conf
# Configuring Apache
RUN rm -rf /etc/apache2/sites-available/* \
&& rm -rf /etc/apache2/sites-enabled/*
# Enable rewrite module
RUN a2enmod rewrite
# Download and install composer globally
RUN curl -s http://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/composer
# Add vendor binaries to PATH
ENV PATH=/var/www/html/vendor/bin:$PATH
I use the following command to start up my stack:
docker-compose -d --build via the Docker Quickstart Terminal on my Windows 10.
Everything builds fine and runs (I checked via docker-compose ps). When I visit the app url, I am getting forbidden error from apache, so I decided to login to the container using docker exec -it my_app_1 /bin/bash command and I went into /var/www/html directory and noticed that it's empty.
Doesn't volume mounting works in windows?

Related

Dockerizing an already existing Symfony project

I am trying to automate the development of a project I am currently working on by creating a container on Docker that is composed of mysql, php and nginx to run a Symfony project. Here is my docker-compose.yml
services:
database:
container_name: database
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: rapidhcm
MYSQL_USER: admin
MYSQL_PASSWORD: password
ports:
- '4306:3306'
volumes:
- ./docker/mysql:/var/lib/mysql
php:
container_name: php
build:
context: .
ports:
- '9000:9000'
volumes:
- ./rapid-backend:/var/www/public
depends_on:
- database
nginx:
container_name: nginx
image: nginx:stable-alpine
ports:
- '8080:80'
volumes:
- ./rapid-backend:/var/www/public
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- database
And here is my Dockerfile:
FROM php:8.0-fpm
RUN apt update \
&& apt install -y zlib1g-dev g++ git libicu-dev zip libzip-dev zip \
&& docker-php-ext-install intl opcache pdo pdo_mysql \
&& pecl install apcu \
&& docker-php-ext-enable apcu \
&& docker-php-ext-configure zip \
&& docker-php-ext-install zip
WORKDIR /var/www/html
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN curl -sS https://get.symfony.com/cli/installer | bash
RUN mv /root/.symfony5/bin/symfony /usr/local/bin/symfony
RUN git config --global user.email "email#email.com" \
&& git config --global user.name "Damian Kowalski"
The Dockerfile and docker-compose.yml files are in the root folder of the project, however the Symfony index.php file is situated in ./rapid-backend/public/index.php. How can I indicate that I want to serve that php file?
You would typically use only two containers: one for the database and one for the php-enabled web server.
You start from an php+apache image or an nginx image with php installed. Then you can bind-mount the web server's configuration files just as you did in your docker-compose.yaml for the nginx container.
You'd have something like this in your Dockerfile:
FROM php:8.2-apache AS production
ADD default.conf /etc/apache2/sites-available/default.conf
RUN # <...>
# Install tools and PHP dependencies
# apt-get update && \
# apt-get install -y <...> && \
# Configure docker php extensions, see image documenation on dockerhub
# docker-php-ext-configure <...> && \
# docker-php-ext-install <...> && \
# Maybe you want xdebug? In that case you'll also want to copy a .ini file
# pecl install xdebug && \
# Cleanup
# docker-php-source delete && \
# Install latest composer 2
# curl https://getcomposer.org/composer-2.phar > /usr/local/bin/composer && \
# chmod +x /usr/local/bin/composer && \
# Enable some apache mods if needed
# a2enmod rewrite headers expires xsend && \
# Some more cleanup if needed
# apt-get remove -y <...> && \
# apt-get clean
# If this image is to be used in a development environment, here you may want to create a user that you'll tell Apache to use with APACHE_RUN_USER, using the same UID as your own user on your host, so that you can bind-mount your sources in your docker-compose.yaml file without having permission issues.
# If however this is a deployment image, here you will probably want to "COPY" your source code in the image and to "RUN composer install".
with the appropriate apache configuration file default.conf that exposes your index.php (see the Symfony documentation at https://symfony.com/doc/current/setup/web_server_configuration.html for example configuration files).
Then your web server service in docker-compose.yaml could look like:
web-server:
build:
context: .
ports:
- '8080:80'
# If this is a dev environment, bind mount your source code, maybe also
# have a "user: ..." option here if you created one in the Dockerfile
volumes:
- ./rapid-backend:/var/www/public
It's a pretty open question but I hope this is a push in the right direction. Starting from here you can use docker-compose exec to run a bash shell inside your web server, and use composer to install Symfony and initialize your project.

Opening Docker container port on localhost in browser returns empty response

I'm trying to run a web project from a Docker container,
when I Dockerize the application on a macBook with an intel chip, everything runs fine and I can make a call to the Docker container. But when I run the same project, with the same setup on my M2 MacBook Air, the browser returns an empty response.
("this page isn't working" --> in Chrome)
This happens even though the containers appear to be running...
(Both containers are green lit up --> in Docker Desktop)
The container makes use of an Nginx service and a php service. The .yml file looks as below:
Docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: php:8.0.6-fpm
container_name: Asset-Service
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: Asset-Service
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:stable
container_name: Asset-Web-Server
restart: unless-stopped
tty: true
ports:
- "8087:80"
- "4487:443"
volumes:
- ./:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
My Dockerfile is the following, even though I don't think that this file causes the problem:
Dockerfile
FROM php:8.0.6-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Set working directory
WORKDIR /var/www
# Install dependencies
USER root
RUN apt-get update && apt-get install -y \
mariadb-client-10.3 \
libcurl4-openssl-dev \
pkg-config \
libssl-dev \
libpng-dev \
libzip-dev \
libonig-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
nano
RUN pecl uninstall mongodb
RUN pecl install mongodb
RUN echo "extension=mongodb.so" >> /usr/local/etc/php/conf.d/mongodb.ini
# 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-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
#RUN 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 -fg 1000 www
RUN id -u 1000 >/dev/null 2>&1 || 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"]
The container logs look normal and the ports in the inspect tab show 0.0.0.0:8087 and 0.0.0.0:4487.
Is this a recurring issue with the apple silicon version of Docker,
and is there anything I can do about it?
I have already tried to replicate the issue on an Intel macBook, but got the desired result instead of the empty response.
On my M2 I tried reinstalling Docker and rebuilding the containers but this didn't seem to fix anything...
Nevermind this issue.
This problem occurred when trying to build the containers from the Desktop folder.

Need help in executing powershell commands in dockerized laravel

I want to access vsphere config info from powercli script to laravel. But I do not know how to make them work together in docker. Whatever I do, the error is similar to this - The command "'pwsh' '-v'" failed. Exit Code: 127(Command not found) Working directory: /var/www/public Output: ================ Error Output: ================ sh: 1: exec: pwsh: not found
As a last resort, I am here.
docker-compose.yml:
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: vapp
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:5.7.22
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: vapp
MYSQL_ROOT_PASSWORD: vapp
SERVICE_TAGS: dev
SERVICE_NAME: mysql
TZ: Asia/Kolkata
volumes:
- dbdata:/var/lib/mysql/
- ./mysql/my.cnf:/etc/mysql/my.cnf
- ./mysql:/var/lib/mysql-files/
networks:
- app-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpMyAdmin
restart: always
ports:
- "8080:80"
environment:
MYSQL_ROOT_PASSWORD: vapp
PMA_HOST: db
external_links:
- mariadb:mariadb
volumes:
- "./phpmyadmin/sessions:/sessions"
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
Dockerfile
FROM mcr.microsoft.com/powershell:latest
WORKDIR ./
FROM php:7.4-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 \
libonig-dev \
locales \
libzip-dev \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
RUN snap install powershell --classic
# 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 mysqli
RUN docker-php-ext-configure gd --enable-gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-enable mysqli
# 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"]
Controller:
//$process = new Process(['ls', '-lsa']); #This works but next one do not
$process = new Process(['pwsh', '-v']);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
I know how the Process() method works. Above code fails.
I need help on making powershell and laravel work together in docker.
Is there anything wrong with docker configuration or the controller code in accessing powershell.
You are using multistage build in Dockerfile. It can copy artifacts, but you don't copy anything. So pwsh app doesn't copy to PHP image (to the second stage).
You could remove first stage (FROM mcr.microsoft.com/powershell:latest) and install properly Powershell inside PHP image.
For example:
RUN apt-get update && apt-get install -y \
wget
RUN wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb && \
dpkg -i packages-microsoft-prod.deb
RUN apt-get update && apt-get install -y \
powershell
PHP image use Debian 10, so here is the instruction: https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7.1#debian-10
Check pwsh inside container first:
docker exec -it app bash
pwsh
Thanks to #konstantin-bogomolov
The reason was the incorrect docker file. Powershell was not properly installed in container.
For those who stop by, Working dockerfile is below.
FROM php:7.4-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 \
libonig-dev \
locales \
libzip-dev \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl \
wget \
apt-utils
# Download the Microsoft repository GPG keys
RUN wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb
# Register the Microsoft repository GPG keys
RUN dpkg -i packages-microsoft-prod.deb
# Update the list of products
RUN apt-get update
# Install PowerShell
RUN apt-get install -y powershell
# Start PowerShell
RUN pwsh
# 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 mysqli
RUN docker-php-ext-configure gd --enable-gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/
RUN docker-php-ext-install gd
RUN docker-php-ext-enable mysqli
# 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"]

GitLab CI/CD - vendor directory on nginx container

I try write CI/CD for symfony, but i have problem with vendor directory. App is copied right, but i don't have vendor. I tried various ways but nothing works. Locally, as instead of variables with the image name, I will give the image name, it all works fine.
My .gitlab-ci.yml
image: tiangolo/docker-with-compose
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay2
IMAGE_TAG: $CI_REGISTRY_IMAGE/demo:$CI_COMMIT_REF_NAME
IMAGE_PHP_PROD: $CI_REGISTRY_IMAGE/demo:$CI_COMMIT_SHA
IMAGE_NGINX_PROD: $CI_REGISTRY_IMAGE/demo-nginx:latest
VERSION: $CI_COMMIT_SHA
stages:
- build
- deploy
before_script:
- apk add --no-cache python3 python3-dev py3-pip libffi-dev openssl-dev gcc libc-dev make
- docker login -u $CI_USER -p $CI_PASSWORD registry.gitlab.com
# build:
# stage: build
# script:
# - docker build --pull -t $IMAGE_TAG deploy
# - docker push $IMAGE_TAG
production images:
stage: build
script:
- docker build . -f deploy/php/Dockerfile -t $IMAGE_PHP_PROD
- docker push $IMAGE_PHP_PROD
- docker build . -f deploy/Dockerfile-nginx-prod -t $IMAGE_NGINX_PROD
- docker push $IMAGE_NGINX_PROD
pull:
stage: deploy
tags:
- prod
script:
- docker pull $IMAGE_PHP_PROD
- docker pull $IMAGE_NGINX_PROD
production:
stage: deploy
tags:
- deploy
before_script:
script:
- docker-compose -f deploy/docker-compose.yml up -d
docker-compose.yml
version: '3'
services:
nginx:
container_name: sf_nginx
image: $IMAGE_NGINX_PROD
restart: on-failure
ports:
- '81:80'
depends_on:
- php
php:
container_name: sf_php
image: $IMAGE_PHP_PROD
restart: on-failure
user: 1000:1000
Dockerfile for php
FROM php:7.4-fpm
RUN docker-php-ext-install pdo_mysql
RUN pecl install apcu
RUN apt-get update && \
apt-get install -y \
libzip-dev
RUN docker-php-ext-install zip
RUN docker-php-ext-enable apcu
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php composer-setup.php --filename=composer \
&& php -r "unlink('composer-setup.php');" \
&& mv composer /usr/local/bin/composer
WORKDIR /usr/src/app
COPY --chown=1000:1000 ./ /usr/src/app
RUN PATH=$PATH:/usr/src/apps/vendor/bin:bin
Dockerfile for nginx
FROM nginx
ADD ./ /usr/src/app
ADD ./deploy/nginx/default.conf /etc/nginx/conf.d/default.conf
In php container exist vendor directory. Docker-compose run, but my app not working:
Am I wrong or you are just installing composer without executing it?
After && mv composer /usr/local/bin/composer you should put && composer install --no-interaction!

Docker PHP image. lstat docker-php-source: no such file or directory

I am trying to setup PHP 5.6.30 fp, but i got error.
Error the same as here (Docker) Getting error: docker-php-source: no such file or directory when building docker file
But i can`t understand how to resolve this.
my folder looks like
app
--docker
--workspace
inside workspace Dockerfile i paste all from https://github.com/docker-library/php/blob/eadc27f12cfec58e270f8e37cd1b4ae9abcbb4eb/5.6/fpm/Dockerfile
Step 15/22 : COPY docker-php-source /usr/local/bin/
ERROR: Service 'workspace' failed to build: lstat docker-php-source: no such file or directory
So what i must to do?
i did this command docker pull php before and it is not helped.
So i have this .yml file
version: '2'
services:
app:
build: ./docker/app
volumes:
- .:/var/www/html
command: "true"
workspace:
build: ./docker/workspace
volumes_from:
- app
links:
- php
- nginx
tty: true
depends_on:
- app
nginx:
build: ./docker/nginx
ports:
- 8080:80
links:
- php
volumes_from:
- app
php:
build: ./docker/php
expose:
- "9000"
volumes_from:
- app
mysql:
image: mysql:5.7
volumes_from:
- app
expose:
- "3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app
MYSQL_USER: root
MYSQL_PASSWORD: root
memcached:
image: memcached
ports:
- "11211:11211"
inside workspace/Dockerfile i have all strings from github image 6.6.30fpm
Then inside this folder i do
docker-compose build workspace
Before this in workspace i had another settings
FROM ubuntu:14.04
ENV DEBIAN_FRONTEND noninteractive
RUN \
sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list && \
apt-get update && \
apt-get -y upgrade && \
apt-get install -y build-essential && \
apt-get install -y software-properties-common && \
apt-get install -y byobu curl git htop man unzip vim wget nano locate
RUN apt-get update --fix-missing --allow-unauthenticated
RUN apt-get install -y --allow-unauthenticated libmcrypt-dev libxml2-dev mysql-client
RUN apt-get update -y --allow-unauthenticated && apt-get upgrade -y --allow-unauthenticated && apt-get install -q -y php5 php5-dev php5-fpm php5-mysqlnd php5-mcrypt
RUN echo "extension=/usr/lib/php5/20121212/mcrypt.so" > /etc/php5/mods-available/mcrypt.ini \
&& ln -s /etc/php5/mods-available/mcrypt.ini /etc/php5/cli/conf.d/20-mcrypt.ini
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
WORKDIR /var/www/html
# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
and this works but i want to 5.6.30 instead of 5.5.9
Perhaps it is due to the fact that there is no permission to execute.
Try
chmod +x docker-php-*
UPD:
Just put all these files inside directory with your php 5.6 Dockerfile
If you want to use php 7.2 version, this link is for you
Anyway for other googlers. Just explore this repository and you will find that you want. :)

Categories