I'm trying to setup my project with Docker but I'm facing an issue...
This is my docker-compose:
version: '3.8'
services:
back:
container_name: ${BACK_CONTAINER_NAME}
build:
context: ./back
target: ${TARGET}
args:
USER_ID: ${USER_ID:-0}
GROUP_ID: ${GROUP_ID:-0}
user: ${USER_ID:-0}:${GROUP_ID:-0}
environment:
- SOME_ENVS
volumes:
- ./back:/srv/app
- ./back/opcache.ini:/usr/local/etc/php/conf.d/opcache.ini
networks:
- lan
networks:
lan:
name: ${NETWORK_NAME}
volumes:
db_data_prod:
Dockerfile:
FROM php:8.1.11-fpm as base
ARG USER_ID
ARG GROUP_ID
RUN addgroup --gid $GROUP_ID user
RUN adduser --disabled-password --gecos '' --uid $USER_ID --gid $GROUP_ID user
RUN apt update --fix-missing \
&& apt install -y zlib1g-dev libonig-dev iputils-ping libzip-dev libicu-dev zip libpq-dev
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint
ENTRYPOINT ["docker-entrypoint"]
CMD ["php-fpm"]
WORKDIR /srv/app
RUN docker-php-ext-install pdo pdo_pgsql mbstring zip
COPY . .
# Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
VOLUME /srv/app/var/
RUN PATH=$PATH:/srv/app/vendor/bin:bin
FROM base as dev
RUN composer install --prefer-dist --no-autoloader --no-scripts --no-progress
RUN pecl install xdebug
RUN docker-php-ext-enable mbstring xdebug
USER user
FROM base as prod
RUN docker-php-ext-install opcache
RUN composer install --prefer-dist --no-dev --no-progress --no-scripts --no-interaction
USER user
And the entrypoint:
#!/bin/sh
set -e
# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
set -- php-fpm "$#"
fi
if [ "$1" = 'php-fpm' ] || [ "$1" = 'php' ] || [ "$1" = 'bin/console' ]; then
if [ "$APP_ENV" != 'prod' ]; then
composer install --prefer-dist --no-progress --no-interaction
fi
if grep -q DATABASE_URL= .env; then
echo "Waiting for database to be ready..."
ATTEMPTS_LEFT_TO_REACH_DATABASE=60
until [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ] || DATABASE_ERROR=$(php bin/console dbal:run-sql -q "SELECT 1" 2>&1); do
if [ $? -eq 255 ]; then
# If the Doctrine command exits with 255, an unrecoverable error occurred
ATTEMPTS_LEFT_TO_REACH_DATABASE=0
break
fi
sleep 1
ATTEMPTS_LEFT_TO_REACH_DATABASE=$((ATTEMPTS_LEFT_TO_REACH_DATABASE - 1))
echo "Still waiting for database to be ready... Or maybe the database is not reachable. $ATTEMPTS_LEFT_TO_REACH_DATABASE attempts left."
done
if [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ]; then
echo "The database is not up or not reachable:"
echo "$DATABASE_ERROR"
exit 1
else
echo "The database is now ready and reachable"
fi
if [ "$( find ./migrations -iname '*.php' -print -quit )" ]; then
php bin/console doctrine:migrations:migrate --no-interaction
fi
fi
fi
exec docker-php-entrypoint "$#"
So, when I'm running the container, all the folders are set with the right user (user..) but only the /srv/app/var folder still root... and the app crashs cause of this.
Anyone have an idea?
Thanks
Related
I am new to Docker and trying to adapt a setup from Symfony Docker to another app that uses a different Dockerfile.
I use a docker-entrypoint to run database migrations which, of course, require the database to be up.
My Dockerfile calls docker-entrypoint but somehow in enters into a loop, running it - the entrypoint code - repeatedly.
This is the Dockerfile at /docker/php:
FROM php:8.1-apache AS symfony_php
RUN a2enmod rewrite
RUN apt-get update \
&& apt-get install -y libpq-dev libzip-dev git libxml2-dev nano wget --no-install-recommends \
&& docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
&& docker-php-ext-install pdo pdo_pgsql pgsql zip \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN wget https://getcomposer.org/download/2.3.5/composer.phar \
&& mv composer.phar /usr/bin/composer && chmod +x /usr/bin/composer
COPY docker/php/apache.conf /etc/apache2/sites-enabled/000-default.conf
COPY docker/php/php.ini /usr/local/etc/php/conf.d/app.ini
COPY docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint
ENV SYMFONY_PHPUNIT_VERSION=9
COPY . /var/www
WORKDIR /var/www
RUN mkdir -p var/cache var/log
RUN chmod -R 777 ./var/cache && chmod -R 777 ./var/log
RUN composer install --prefer-dist --no-progress --no-interaction
ENTRYPOINT ["docker-entrypoint"]
The entrypoint also at /docker/php
set -e
if grep -q DATABASE_URL= .env; then
echo "Waiting for database to be ready..."
ATTEMPTS_LEFT_TO_REACH_DATABASE=60
until [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ] || DATABASE_ERROR=$(php bin/console dbal:run-sql -q "SELECT 1" 2>&1); do
if [ $? -eq 255 ]; then
# If the Doctrine command exits with 255, an unrecoverable error occurred
ATTEMPTS_LEFT_TO_REACH_DATABASE=0
break
fi
sleep 1
ATTEMPTS_LEFT_TO_REACH_DATABASE=$((ATTEMPTS_LEFT_TO_REACH_DATABASE - 1))
echo "Still waiting for database to be ready... Or maybe the database is not reachable. $ATTEMPTS_LEFT_TO_REACH_DATABASE attempts left."
done
if [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ]; then
echo "The database is not up or not reachable:"
echo "$DATABASE_ERROR"
exit 1
else
echo "The database is now ready and reachable"
fi
if ls -A migrations/*.php >/dev/null 2>&1; then
php bin/console doctrine:migrations:migrate --no-interaction
fi
fi
exec docker-php-entrypoint "$#"
The resulting loop:
Adding this to my entrypoint solved the issue:
/usr/sbin/apache2ctl -D FOREGROUND
It seems that the entrypoint interrupts the server process and placing the above snippet forces the server back up.
EDIT:
Even better:
Change Dockerfile as below:
ENTRYPOINT ["docker-entrypoint"]
CMD ["apachectl", "-D", "FOREGROUND"]
And on entrypoint.sh keep as per the original post:
exec docker-php-entrypoint "$#"
I have a need to install awscli and jq library in Hasura Docker Image. I tried to use yum, apt-get or apk commands to install the dependencies, but none of them worked.
Docker Image: https://hub.docker.com/r/hasura/graphql-engine/
how to install these dependencies in Hasura Docker Image? Any help is appreciated.
Dockerfile:
FROM hasura/graphql-engine:latest
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["./entrypoint.sh"]
entrypoint.sh:
#!/bin/sh
set -o errexit -o nounset -o pipefail
DB_HOST=${DB_HOST:-postgres}
DB_PORT=${DB_PORT:-5432}
if [ -z "${DB_NAME}" ]; then
echo "Must provide DB_NAME environment variable. Exiting...."
exit 1
fi
if [ -z "${DB_USER}" ]; then
echo "Must provide DB_USER environment variable. Exiting...."
exit 1
fi
if [ -z "${DB_PASSWORD}" ]; then
echo "Must provide DB_PASSWORD environment variable. Exiting...."
exit 1
fi
export HASURA_GRAPHQL_DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}#${DB_HOST}:${DB_PORT}/${DB_NAME}
/bin/graphql-engine serve
DB_PASSWORD is encrypted with KMS, so i want to use aws cli to decrypt the password in entrypoint.sh file before setting the Environment Variable: HASURA_GRAPHQL_DATABASE_URL
I was able to customize Hasura Docker Image with the help of Hasura Team support.
Here is the link to github issue: https://github.com/hasura/graphql-engine/issues/2729
Dockerfile:
FROM hasura/graphql-engine:v1.0.0-beta.4 as base
FROM python:3.7-slim-stretch
RUN apt-get -y update \
&& apt-get install -y --no-install-recommends libpq-dev jq \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /usr/share/doc/ \
&& rm -rf /usr/share/man/ \
&& rm -rf /usr/share/locale/ \
&& pip install awscli
# copy hausra binary from base container
COPY --from=base /bin/graphql-engine /bin/graphql-engine
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]
entrypoint.sh:
#!/bin/bash
set -e
DB_HOST=${DB_HOST:-postgres}
DB_PORT=${DB_PORT:-5432}
AWS_REGION=${AWS_REGION:-us-east-1}
DB_PASSWORD_ENCYPTED=${DB_PASSWORD_ENCYPTED:-false}
if [ -z "${DB_NAME}" ]; then
echo "Must provide DB_NAME environment variable. Exiting...."
exit 1
fi
if [ -z "${DB_USER}" ]; then
echo "Must provide DB_USER environment variable. Exiting...."
exit 1
fi
if [ -z "${DB_PASSWORD}" ]; then
echo "Must provide DB_PASSWORD environment variable. Exiting...."
exit 1
fi
if [ ${DB_PASSWORD_ENCYPTED} == "true" ]
then
echo "loading KMS credentials"
decrypted_value_base64=$( \
aws --region ${AWS_REGION} kms decrypt \
--ciphertext-blob fileb://<(echo "${DB_PASSWORD}" | base64 -d) \
--query Plaintext \
--output text
)
decrypted_value=$(echo $decrypted_value_base64 | base64 -d)
export HASURA_GRAPHQL_DATABASE_URL=postgres://${DB_USER}:${decrypted_value}#${DB_HOST}:${DB_PORT}/${DB_NAME}
else
export HASURA_GRAPHQL_DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}#${DB_HOST}:${DB_PORT}/${DB_NAME}
fi
/bin/graphql-engine serve
My docker-compose is the same as the default one, exepting that it has this additional code:
hyperledger:
build:
context: .
ports:
- 8081:8081
My Dockerfile:
FROM hyperledger/composer-cli:0.19.0
WORKDIR /app
COPY . /app
USER root
RUN chown -R composer:composer .
USER composer
RUN ls -la
# RUN composer archive create -t dir -n .
RUN rm -rf .stage && mkdir ./.stage
RUN echo '{"name": "hyper-discover-network","x-type": "hlfv1","x-commitTimeout": 300,"version": "1.0.0","client": {"organization": "Org1","connection": {"timeout": {"peer": {"endorser": "300","eventHub": "300","eventReg": "300"},"orderer": "300"}}},"channels": {"composerchannel": {"orderers": ["orderer.example.com"],"peers": {"peer0.org1.example.com": {}}}},"organizations": {"Org1": {"mspid": "Org1MSP","peers": ["peer0.org1.example.com"],"certificateAuthorities": ["ca.org1.example.com"]}},"orderers": {"orderer.example.com": {"url": "grpc://orderer.example.com:7050"}},"peers": {"peer0.org1.example.com": {"url": "grpc://peer0.org1.example.com:7051","eventUrl": "grpc://peer0.org1.example.com:7053"}},"certificateAuthorities": {"ca.org1.example.com": {"url": "http://ca.org1.example.com:7054","caName": "ca.org1.example.com"}}}' > connection.json
EXPOSE 8081
USER root
RUN chmod 777 docker-entrypoint.sh
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat
ENTRYPOINT [ "sh", "/app/docker-entrypoint.sh" ]
CMD [ "composer" ]
And my docker-entrypoint.sh:
#!/bin/bash
# set -e
if [ "$1" = 'composer' ]; then
composer archive create -t dir -n .
echo '{...}' > connection.json
composer card create -p ./connection.json -u PeerAdmin -r PeerAdmin -r ChannelAdmin -f .stage/PeerAdmin.card \
-c crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem \
-k crypto-config/peerOrganizations/org1.example.com/ca/19ab65abbb04807dad12e4c0a9aaa6649e70868e3abd0217a322d89e47e1a6ae_sk
# composer-playground
composer card import -f .stage/PeerAdmin.card && composer card list && ls -la && pwd
echo 'som shit'
composer network install -a hyper-discover-network#0.0.1.bna -c PeerAdmin#hyper-discover-network
fi
The error appears at composer network install ... line.
I am building this environment by executing: ./startFabric.sh && docker-compose up hyperledger
I built a simple dockerfile using php5.6-fpm image. This file should run my site php code and install all required dependencies. The problem is that php is working, but dependencies like
composer install --working-dir=/var/www/html $COMPOSER_ARGS
and
cp /var/www/html/config.inc.php.dist /var/www/html/config.inc.php
are not installed.
Dockerfile:
FROM php:5.6-fpm
LABEL maintainer "karolis#pretendentas.lt"
WORKDIR /var/www/html
VOLUME /var/www/html
EXPOSE 9000
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN set -ex \
&& apt-get update && apt-get install -y \
zip \
git \
libxml2-dev \
libjpeg-dev \
libpng12-dev \
&& docker-php-ext-configure gd --with-png-dir=/usr/ --with-jpeg-dir=/usr/ \
&& docker-php-ext-install -j$(nproc) bcmath gd mysqli opcache soap
RUN mkdir -p /var/lib/php/session \
&& mkdir -p /var/lib/php/wsdlcache \
&& chown -R www-data:www-data /var/lib/php/session \
&& chown -R www-data:www-data /var/lib/php/wsdlcache \
&& chmod +x /docker-entrypoint.sh
ENV GITHUB_TOKEN ********************
RUN set -ex \
&& curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/bin \
--filename=composer \
&& composer config -g github-oauth.github.com $GITHUB_TOKEN
ENTRYPOINT ["/docker-entrypoint.sh"]
docker-entrypoint.sh:
#!/bin/bash
set -e
if [ -f var/www/html/docker/init.sh ]; then
sleep 5 && echo "[info] Running /var/www/html/init.sh script" && sh /var/www/html/init.sh &
fi
docker-php-entrypoint php-fpm
init.sh:
#!/bin/bash
if [ -f /var/www/html/composer.json ] && [ ! -d /var/www/html/vendor ]; then
echo "[info] Composer install"
composer install --working-dir=/var/www/html $COMPOSER_ARGS
fi
if [ ! -f /var/www/html/config.inc.php ]; then
echo "[info] Copy default config"
cp /var/www/html/config.inc.php.dist /var/www/html/config.inc.php
fi
this is php image snippet from docker-compose file:
php:
image: pretendentas/php5.6-test
ports:
- "9000:9000"
volumes:
- .:/var/www/html
- ./docker/php/php.ini:/usr/local/etc/php/php.ini:ro
working_dir: /var/www/html
restart: always
depends_on:
- db
I think you ask into wrong path to execute the file.
dockerfile
COPY init.sh /var/www/html/init.sh
entrypoint.sh
if [ -f var/wwww/html/docker/init.sh ];
Fix the typos here:
if [ -f var/wwww/html/docker/init.sh ];
To:
if [ -f /var/www/html/docker/init.sh ];
However, take into account that the COPY of init.sh is being overrided by the volume. So in the entrypoint.sh refer to the correct path of init.sh. I assume that init.sh is in the project root dir, so it is located at the root of the volume: /var/www/html
if [ -f /var/www/html/init.sh ]; then
sleep 5 && echo "[info] Running /var/www/html/init.sh script " && sh /var/www/html/init.sh &
fi
I have this docker file:
FROM node:6-slim
RUN apt-get update \
&& apt-get install -y python2.7 make g++ git bzip2 libfreetype6 libfontconfig1 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& ln -s /usr/bin/python2.7 /usr/bin/python
RUN cd $(npm root -g)/npm \
&& npm install serverless -g\
&& npm install fs-extra \
&& sed -i -e s/graceful-fs/fs-extra/ -e s/fs\.move/fs.rename/ ./lib/utils/rename.js
ADD package.json /tmp/package.json
CMD [REV=$(git rev-list -n 1 HEAD 'package.json');]
CMD [STAMP=$(git show --pretty=format:%ai --abbrev-commit "$REV" | head -n 1);]
CMD [touch -d "$STAMP" package.json;]
RUN cd /tmp && npm install --quiet --production \
&& npm rebuild bcrypt \
&& rm -Rf node_modules/ffprobe-static/bin/darwin \
&& rm -Rf node_modules/ffprobe-static/bin/win32
RUN mkdir -p /usr/src/app && cp -a /tmp/node_modules /usr/src/app
WORKDIR /usr/src/app
ADD . /usr/src/app
RUN cd /usr/src/app \
&& git gc --aggressive --prune=all
ENV PORT 9999
EXPOSE 9999
CMD [ "npm", "run", "offline" ]
When I run it, all works without problems, but when I try to access the docker instance:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' serverless
172.17.0.3
When I go to 172.17.0.3:9999 I am not able to connect to the instance.
Whereas if I run my application from my local machine, localhost:9999 responds.
Here is a copy of my serverless.yml:
service: serverless-graphql
plugins:
- serverless-offline
provider:
name: aws
runtime: nodejs4.3
timeout: 30
stage: dev
# you can add statements to the Lambda function's IAM Role here
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:*
Resource: "*"
- Effect: Allow
Action:
- s3:*
Resource: "*"
# you can define service wide environment variables here
environment:
database: mongo
MONGO_URI: "mongodb://docker-mongo:27017/test-db"
functions:
graphql:
handler: lib/handler.graphql
timeout: 12
events:
- http:
path: graphql
method: post
cors: true
stripewebhook:
handler: lib/handler.stripewebhook
events:
- http:
path: stripewebhook
method: post
cors: true
Any advice is much appreciated.