I am actually blocked because when I add this command in my Dockerfile
"RUN ln -s /etc/nginx/sites-available/local.magento /etc/nginx/sites-enabled" to activate my virtual host, I have always the same error
"nginx: [emerg] open() "/var/www/html/magento2/site/nginx.conf.sample" failed (13: Permission denied) in /etc/nginx/sites-enabled/local.magento:10"
Although I have made add this command "mkdir -p /var/www/html;
chmod 777 -R /var/www/html;
chown rootless:rootless /var/www/html; ", I have read that we cannot make a chown for an other user on a folder already binded by a volume. I have read every posts on that and I tried every methods without succeed. So I guess I must not use a correct method elsewhere.
Could you please tell me what's wrong with my dockerfile?
RUN apt-get -y update
##creation du user qui sera owner des dossiers
#
RUN groupadd -g 1000 rootless \
&& useradd -g 1000 -u 1000 -d /var/www -s /bin/bash rootless
#
#
#
#installation de nginx
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
default-mysql-client \
nginx
#
#
#
# installation de php
RUN apt-get clean && apt-get update
RUN apt-get install -y ca-certificates apt-transport-https software-properties-common wget curl lsb-release
RUN curl -sSL https://packages.sury.org/php/README.txt | bash -x
RUN apt-get update
RUN wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
RUN echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/php.list
RUN apt-get install -y php8.1-fpm php8.1-cli \
php8.1-curl \
php8.1-bcmath \
php8.1-intl \
php8.1-mbstring \
php8.1-xmlrpc \
php8.1-mcrypt \
php8.1-mysql \
php8.1-gd \
php8.1-xml \
php8.1-cli \
php8.1-ctype \
php8.1-dom \
php8.1-fileinfo \
php8.1-iconv \
php8.1-simplexml \
php8.1-soap \
php8.1-sockets \
php8.1-tokenizer \
php8.1-xmlwriter \
php8.1-xsl \
php8.1-zip
#
RUN mkdir -p /var/www/html/magento
COPY ./conf/nginx/local.magento /etc/nginx/sites-available/local.magento
#RUN ln -s /etc/nginx/sites-available/local.magento /etc/nginx/sites-enabled
RUN service php8.1-fpm stop;
RUN service php8.1-fpm start;
RUN service nginx restart;
#installation de composer
RUN curl -O https://getcomposer.org/download/2.2.17/composer.phar | php
RUN mv composer.phar /usr/local/bin/composer && chmod +x /usr/local/bin/composer
#
#
#
##MAJ des droits pour rootless
RUN set -eux; \
mkdir -p /etc/php; \
chmod 777 -R /etc/php; \
chown rootless:rootless /etc/php; \
mkdir -p /etc/nginx; \
chmod 777 -R /etc/nginx; \
chown rootless:rootless /etc/nginx; \
mkdir -p /var/pid; \
chmod 777 -R /var/pid/; \
chown rootless:rootless /var/pid; \
mkdir -p /var/run; \
chmod 777 -R /var/run/; \
chown rootless:rootless /var/run; \
mkdir -p /var/lock; \
mkdir -p /var/lock/opcache; \
chmod 777 -R /var/lock/; \
chown rootless:rootless /var/lock; \
mkdir -p /var/log; \
chmod 777 -R -R /var/log; \
chown rootless:rootless /var/log; \
mkdir -p /var/cache; \
mkdir -p /var/cache/composer; \
mkdir -p /var/cache/opcache; \
chmod 777 -R /var/cache; \
chown rootless:rootless /var/cache; \
mkdir -p /var/lib; \
mkdir -p /var/lib/mysql; \
mkdir -p /var/lib/nginx; \
mkdir -p /var/lib/nginx/body; \
chmod 777 -R /var/lib; \
chown rootless:rootless /var/lib; \
mkdir -p /var/www/html; \
chmod 777 -R /var/www/html; \
chown rootless:rootless /var/www/html; \
mkdir -p /bin; \
chmod 777 -R /bin; \
chown rootless:rootless /bin; \
touch /dev/stdout; \
chmod 777 -R /dev/stdout; \
chown rootless:rootless /dev/stdout
#
#
WORKDIR /var/www/html/magento2
VOLUME /var/myvolume
#
#
#STOPSIGNAL SIGQUIT
#
EXPOSE 80
#
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
And here my docker-compose
version: "1.0"
services:
web:
user: "1000"
build: .
ports:
- "8080:80"
volumes:
- ".:/var/www/html/magento2:rw"
depends_on:
- mysql
mysql:
image: mariadb:10.6
container_name: mysql
restart: always
ports:
- "3306:3306"
environment:
- MYSQL_USER=root
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=magento
phpmyadmin:
container_name: phpmyadmin
restart: always
image: phpmyadmin/phpmyadmin:latest
environment:
- MYSQL_ROOT_PASSWORD=root
- PMA_USER=root
- PMA_PASSWORD=root
ports:
- "8081:80"
links:
- mysql:db
elasticsearch:
container_name: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch:7.9.0
environment:
- ELASTICSEARCH_URL=http://elasticsearch:9200
- node.name=elasticsearch
- cluster.name=es-cluster
- discovery.type=single-node
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- ./magento-es-data:/usr/share/elasticsearch/data
ports:
- 9200:9200
I have tried many things regarding the existing topics on that but nothing to do...
Don't judge the details on my dockerfile like my password or the comments, it's a test file nothing else:)
Thanks in advance for your help
Related
I am trying to use Xdebug on Docker, I'm using Debian, PHP 8. Cannot make a connection with Xdebug. Won't give any errors.
Since I don't get any errors or logs can't figure out what is wrong with my configurations.
Everything builds normally with the configurations below. Could someone help me activate Xdebug?
Dockerfile:
FROM debian:stretch-slim as builder
RUN mkdir -p /usr/share/man/man1
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends wget gnupg2 unzip curl cron make \
locales ca-certificates apt-transport-https default-mysql-client-core sendmail \
default-jre-headless ca-certificates-java
RUN sed -i 's/mozilla\/DST_Root_CA_X3.crt/!mozilla\/DST_Root_CA_X3.crt/g' /etc/ca-certificates.conf
RUN update-ca-certificates
RUN wget -q https://packages.sury.org/php/apt.gpg -O- | apt-key add - && \
echo "deb https://packages.sury.org/php/ stretch main" | tee /etc/apt/sources.list.d/php.list && \
cd /etc/apt/sources.list.d && \
rm -rf ondrej-ubuntu*
RUN apt-get update && apt-get install -y \
git \
zip \
unzip \
sudo \
libzip-dev \
libicu-dev \
libbz2-dev \
libpng-dev \
libjpeg-dev \
libmcrypt-dev \
libreadline-dev \
libfreetype6-dev \
g++ \
nano \
vim \
php-xdebug
RUN curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
RUN apt-get update && apt-get install -y nodejs
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install -y --no-install-recommends \
php8.0 \
php8.0-mysql \
php8.0-gd \
php8.0-xml \
php8.0-mbstring \
php8.0-curl \
libapache2-mod-php8.0 \
php8.0-intl \
php8.0-soap \
php8.0-zip \
php8.0-ldap \
php8.0-dev \
php-pear \
mysql-client \
apache2 \
php-xdebug && \
update-alternatives --set php /usr/bin/php8.0 && \
a2enmod php8.0 && \
a2enmod rewrite && \
a2enmod ssl
RUN echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen && \
echo "tr_TR.UTF-8 UTF-8" >> /etc/locale.gen && \
locale-gen
RUN apt-get -y autoremove wget unzip && apt-get -y clean && \
rm -rf "/tmp/"* "/var/cache/apt" "/usr/share/man" "/usr/share/doc" "/usr/share/doc-base" "/usr/share/info/*" >> /dev/null
RUN chown -R www-data. /var/www/html
RUN apt-get -y autoremove && apt-get -y clean && \
rm -rf "/tmp/"* "/var/cache/apt" "/usr/share/man" "/usr/share/doc" "/usr/share/doc-base" "/usr/share/info/*" >> /dev/null
RUN curl -Ss https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /var/www/html
RUN a2enmod rewrite
CMD service apache2 restart
ENTRYPOINT ["/usr/sbin/apache2ctl"]
CMD ["-D", "FOREGROUND"]
EXPOSE 80
docker-compose.yml
version: "3.3"
networks:
my_network:
volumes:
my_volume:
services:
mysqllatest:
image: mysql:latest
container_name: mysqllatest
volumes:
- my_volume:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
tty: true
environment:
MYSQL_DATABASE: local_db
MYSQL_USERNAME: root
MYSQL_PASSWORD: toor
MYSQL_ROOT_PASSWORD: toor
MYSQL_SERVICE_NAME: mysql
ports:
- "3306:3306"
networks:
- my_network
php8debian:
container_name: php8debian
build: ./base/
volumes:
- ../:/var/www/html/
- ./server/php/vhost/000-default.conf:/etc/apache2/sites-enabled/000-default.conf
- ./server/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
ports:
- 80:80
extra_hosts:
- "mysite.test:127.0.0.1"
stdin_open: true
tty: true
networks:
- my_network
xdebug.ini
zend_extension=xdebug.so
[XDebug]
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_port = 9003
xdebug.client_host = 'host.docker.internal'
xdebug.idekey = VSCODE
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"hostname": "0.0.0.0",
"pathMappings": {
"/var/www/html": "${workspaceRoot}/my_site"
},
"log": true
}
]
}
I have this docker-compose config.
The "app" is a PHP application. As you can see, 3 env vars are passed to the container.
However, after docker-compose up, PHP doesn't see these. They are not returned by getenv() and they cannot be found $_ENV either.
What's wrong here?
version: '3.4'
services:
db:
image: postgres:11.0
restart: always
environment:
POSTGRES_PASSWORD: testuser
POSTGRES_USER: test
POSTGRES_DB: db
volumes:
- /data/db
redis:
image: redis:latest
restart: always
volumes:
- /data/redis
app:
build:
context: .
dockerfile: Dockerfile
environment:
- DATABASE_URL=postgresql://db:5432/db
- REDIS_URL=tcp://redis:6379?database=1
- NODE_ENV=development
ports:
- '80:80'
volumes:
- '${BASEDIR}:/var/www/some'
Here is my Dockerfile:
FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
software-properties-common \
apt-utils \
tzdata \
locales
RUN add-apt-repository ppa:ondrej/php
RUN apt-get update
RUN locale-gen en_US.UTF-8
RUN echo "Europe/Budapest" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata
RUN apt-get -y update && apt-get -y install \
libglu1-mesa \
less \
vim \
nginx \
php7.4 \
php7.4-fpm \
php7.4-cli \
php7.4-common \
php7.4-curl \
php-deepcopy \
php7.4-gd \
php7.4-mbstring \
php7.4-pgsql \
php7.4-soap \
php7.4-xdebug \
php7.4-zip \
php7.4-xml \
phpunit \
npm && npm i -g npm
ENV NGINX_RUN_USER www-data
ENV NGINX_RUN_GROUP www-data
ENV NGINX_LOG_DIR /var/log/nginx
ENV NGINX_LOCK_DIR /var/lock/nginx
ENV NGINX_PID_FILE /run/nginx.pid
RUN mkdir www
RUN mkdir -p /var/www/dbv.local/html
RUN chmod -R 755 /var/www/dbv.local
COPY ./php.ini /etc/php/7.4/fpm/php.ini
COPY ./dbv.local /etc/nginx/sites-available/dbv.local
COPY ./lib/aspose_php.so /usr/lib/php/20190902
COPY ./lib/libaspose_cpp_clang3_libstdcpp.so /usr/lib/libaspose_cpp_clang3_libstdcpp.so
COPY ./lib/libAspose.Slides_clang3_libstdcpp.so /usr/lib/libAspose.Slides_clang3_libstdcpp.so
COPY ./lib/libphpcpp.so.2.2 /usr/lib/libphpcpp.so.2.2
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
RUN ln -s /etc/nginx/sites-available/dbv.local /etc/nginx/sites-enabled/
ADD ./xdebug.ini /etc/php/7.4/mods-available/xdebug.ini
ADD ./aspose_php.ini /etc/php/7.4/mods-available/aspose_php.ini
ADD ./start.sh /root/start.sh
RUN ln -s /etc/php/7.4/mods-available/xdebug.ini /etc/php/7.4/mods-available/20-xdebug.ini
RUN ln -s /etc/php/7.4/mods-available/aspose_php.ini /etc/php/7.4/fpm/conf.d/aspose_php.ini
RUN ln -s /etc/php/7.4/mods-available/aspose_php.ini /etc/php/7.4/cli/conf.d/aspose_php.ini
RUN rm -rf /var/lib/apt/lists/*
RUN apt-get clean
CMD ["/root/start.sh"]
EXPOSE 80 9000 5432
Edit:
start.sh is just a one liner
service php7.4-fpm start && nginx
Ubuntu 18 is necessary. I could use an official Nginx image though. Maybe that's the issue?
I have developed a simple rails app that uses Redis, sidekiq, and mysql2. I'm trying to run the app using docker-compose. I wrote a docker-compose.yml which is working fine. I've made a few changes to the docker file which isn't working and when I see the logs the webapp container is exiting with exit code 1.
These are my files
Dockerfile
FROM ubuntu:16.04
ENV RUBY_MAJOR="2.6" \
RUBY_VERSION="2.6.3" \
RUBYGEMS_VERSION="3.0.8" \
BUNDLER_VERSION="1.17.3" \
RAILS_VERSION="5.2.1" \
RAILS_ENV="production" \
GEM_HOME="/usr/local/bundle"
ENV BUNDLE_PATH="$GEM_HOME" \
BUNDLE_BIN="$GEM_HOME/bin" \
BUNDLE_SILENCE_ROOT_WARNING=1 \
BUNDLE_APP_CONFIG="$GEM_HOME"
ENV PATH="$BUNDLE_BIN:$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH"
USER root
RUN apt-get update && \
apt-get -y install sudo
RUN echo "%sudo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers && \
addgroup --gid 1024 stars && \
useradd -G stars,sudo -d /home/user --shell /bin/bash -m user
RUN mkdir -p /usr/local/etc \
&& echo 'install: --no-document' >> /usr/local/etc/gemrc \
&& echo 'update: --no-document' >> /usr/local/etc/gemrc
USER user
RUN sudo apt-get -y install --no-install-recommends vim make gcc zlib1g-dev autoconf build-essential libssl-dev libsqlite3-dev \
curl htop unzip mc openssh-server openssl bison libgdbm-dev ruby git libmysqlclient-dev tzdata mysql-client
RUN sudo rm -rf /var/lib/apt/lists/* \
&& sudo curl -fSL -o ruby.tar.gz "http://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR/ruby-$RUBY_VERSION.tar.gz" \
&& sudo mkdir -p /usr/src/ruby \
&& sudo tar -xzf ruby.tar.gz -C /usr/src/ruby --strip-components=1 \
&& sudo rm ruby.tar.gz
USER root
RUN cd /usr/src/ruby \
&& { sudo echo '#define ENABLE_PATH_CHECK 0'; echo; cat file.c; } > file.c.new && mv file.c.new file.c \
&& autoconf \
&& ./configure --disable-install-doc
USER user
RUN cd /usr/src/ruby \
&& sudo make -j"$(nproc)" \
&& sudo make install \
&& sudo gem update --system $RUBYGEMS_VERSION \
&& sudo rm -r /usr/src/ruby
RUN sudo gem install bundler --version "$BUNDLER_VERSION"
RUN sudo mkdir -p "$GEM_HOME" "$BUNDLE_BIN" \
&& sudo chmod 777 "$GEM_HOME" "$BUNDLE_BIN" \
&& sudo gem install rails --version "$RAILS_VERSION"
RUN mkdir -p ~/.ssh && \
chmod 0700 ~/.ssh && \
ssh-keyscan github.com > ~/.ssh/known_hosts
ARG ssh_pub_key
ARG ssh_prv_key
RUN echo "$ssh_pub_key" > ~/.ssh/id_rsa.pub && \
echo "$ssh_prv_key" > ~/.ssh/id_rsa && \
chmod 600 ~/.ssh/id_rsa.pub && \
chmod 600 ~/.ssh/id_rsa
USER root
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get install -y nodejs
USER user
WORKDIR /data
RUN sudo mkdir /data/checklist
WORKDIR /data/checklist
ADD Gemfile Gemfile.lock ./
RUN sudo chown -R user /data/checklist
RUN bundle install
ADD . .
RUN sudo chown -R user /data/checklist
EXPOSE 3001
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
RUN chmod +x ./config/docker/compile.sh && ./config/docker/compile.sh
CMD ["bundle", "exec", "rails", "s", "-p", "3001"]
compile.sh
bundle exec rake assets:precompile
bundle exec rake db:migrate 2>/dev/null || bundle exec rake db:create db:migrate
echo "Assets Pre-compiled!"
This is my docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: "list"
MYSQL_ROOT_PASSWORD: "Mission2019"
MYSQL_USERNAME: "root"
webapp:
build: .
ports:
- '3001:3001'
volumes:
- '.:/data/checklist'
depends_on:
- db
- redis
command: rails db:migrate
environment:
DB_USERNAME: "root"
DB_PASSWORD: "Mission2019"
DB_DATABASE: "list"
DB_PORT: 3306
DB_HOST: db
RAILS_ENV: production
RAILS_MAX_THREADS: 5
redis:
image: redis:4.0-alpine
command: redis-server
ports:
- '6379:6379'
sidekiq:
build: .
command: bundle exec sidekiq -C config/sidekiq.yml
depends_on:
- "db"
- "redis"
Working Dockerfile
FROM ubuntu:16.04
ENV RUBY_MAJOR="2.6" \
RUBY_VERSION="2.6.3" \
RUBYGEMS_VERSION="3.0.8" \
BUNDLER_VERSION="1.17.3" \
RAILS_VERSION="5.2.1" \
RAILS_ENV="production" \
GEM_HOME="/usr/local/bundle"
ENV BUNDLE_PATH="$GEM_HOME" \
BUNDLE_BIN="$GEM_HOME/bin" \
BUNDLE_SILENCE_ROOT_WARNING=1 \
BUNDLE_APP_CONFIG="$GEM_HOME"
ENV PATH="$BUNDLE_BIN:$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH"
USER root
RUN apt-get update && \
apt-get -y install sudo
RUN echo "%sudo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers && \
addgroup --gid 1024 stars && \
useradd -G stars,sudo -d /home/user --shell /bin/bash -m user
RUN mkdir -p /usr/local/etc \
&& echo 'install: --no-document' >> /usr/local/etc/gemrc \
&& echo 'update: --no-document' >> /usr/local/etc/gemrc
USER user
RUN sudo apt-get -y install --no-install-recommends vim make gcc zlib1g-dev autoconf build-essential libssl-dev libsqlite3-dev \
curl htop unzip mc openssh-server openssl bison libgdbm-dev ruby git libmysqlclient-dev tzdata mysql-client
RUN sudo rm -rf /var/lib/apt/lists/* \
&& sudo curl -fSL -o ruby.tar.gz "http://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR/ruby-$RUBY_VERSION.tar.gz" \
&& sudo mkdir -p /usr/src/ruby \
&& sudo tar -xzf ruby.tar.gz -C /usr/src/ruby --strip-components=1 \
&& sudo rm ruby.tar.gz
USER root
RUN cd /usr/src/ruby \
&& { sudo echo '#define ENABLE_PATH_CHECK 0'; echo; cat file.c; } > file.c.new && mv file.c.new file.c \
&& autoconf \
&& ./configure --disable-install-doc
USER user
RUN cd /usr/src/ruby \
&& sudo make -j"$(nproc)" \
&& sudo make install \
&& sudo gem update --system $RUBYGEMS_VERSION \
&& sudo rm -r /usr/src/ruby
RUN sudo gem install bundler --version "$BUNDLER_VERSION"
RUN sudo mkdir -p "$GEM_HOME" "$BUNDLE_BIN" \
&& sudo chmod 777 "$GEM_HOME" "$BUNDLE_BIN" \
&& sudo gem install rails --version "$RAILS_VERSION"
RUN mkdir -p ~/.ssh && \
chmod 0700 ~/.ssh && \
ssh-keyscan github.com > ~/.ssh/known_hosts
ARG ssh_pub_key
ARG ssh_prv_key
RUN echo "$ssh_pub_key" > ~/.ssh/id_rsa.pub && \
echo "$ssh_prv_key" > ~/.ssh/id_rsa && \
chmod 600 ~/.ssh/id_rsa.pub && \
chmod 600 ~/.ssh/id_rsa
USER root
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get install -y nodejs
USER user
WORKDIR /data
RUN sudo mkdir /data/checklist
WORKDIR /data/checklist
ADD Gemfile Gemfile.lock ./
RUN sudo chown -R user /data/checklist
RUN bundle install
ADD . .
RUN sudo chown -R user /data/checklist
EXPOSE 3001
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
ENTRYPOINT ["sh", "./config/docker/startup.sh"]
startup.sh for the working Dockerfile
kill -9 `cat /data/checklist/tmp/pids/server.pid`
bundle exec rake assets:precompile
bundle exec rake db:migrate 2>/dev/null || bundle exec rake db:create db:migrate
rails s -p 3001 -b 0.0.0.0 -e PRODUCTION
echo "Assets Pre-compiled!"
The reason for changing the Dockerfile from the one which works is, since I'm using sidekiq and I want sidekiq to run in another separate container all together. If I give command option in the docker-compose.yml it's not picking up and bundle exec sidekiq -C config/sidekiq.yml is not running in the sidekiq container.
I've kubernetes yaml files for the same app. Facing same problem there as well. I'm not able to overwrite the entrypoint instruction from the YAML for sidekiq pod.
Please let me know if any other info is needed.
You should use CMD in the second Dockerfile too, and then it can be overridden by the Docker Compose command:.
# CMD, not ENTRYPOINT
CMD ["sh", "./config/docker/startup.sh"]
One common use for ENTRYPOINT is to be a wrapper program that does some environment or other first-time setup, then executes the CMD that's passed in as the remainder of the command-line arguments. Then you can separately replace the command part, while keeping the setup part. In a Ruby environment bundle exec ... has the right semantics, so you can also consider:
# Note: MUST be JSON-array syntax
ENTRYPOINT ["bundle", "exec"]
# Can be either string or JSON-array form
CMD ["./config/docker/startup.sh"]
version: '3.8'
services:
webapp:
build: .
# Use default CMD/ENTRYPOINT from image
sidekiq:
build: .
# Overrides CMD, leaves ENTRYPOINT in place
command: sidekiq -C config/sidekiq.yml
There is a separate Compose entrypoint: override, but you should rarely need it.
(Your Dockerfile can be much much simpler. In general you do not need to configure sudo or user passwords, and Dockerfiles run as root by default unless you explicitly use USER to switch the current user IDs. Adding ssh credentials into a Dockerfile where they can be trivially docker cp'd out is also not a best practice. Also consider using the Docker Hub ruby image over building your own from source.)
I have a basic docker container which I build using docker-compose (version 3) to bring up a basic LAMP stack.
The issue I am having is that files created inside the docker container are always owned by root, so I am unable to edit them locally.
I have tried setting the container www-data user to have the same uid as my local user, which works, but new files are still created by root.
How do I create file in the container that I can edit locally?
My compose file;
version: "3"
services:
webserver:
build:
context: ./docker/containers/webserver
container_name: 'apache7.1-webserver'
restart: 'always'
ports:
- "80:80"
- "443:443"
links:
- mysql
volumes:
- ${DOCUMENT_ROOT}:/var/www/html
- ${PHP_INI}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR}:/etc/apache2/sites-enabled
- ${APACHE_LOG_DIR}:/var/log/apache2
mysql:
build: ./docker/containers/mysql
container_name: 'apache7.1-mysql'
restart: 'always'
ports:
- "3306:3306"
volumes:
- ${MYSQL_DATA_DIR}:/var/lib/mysql
- ${MYSQL_LOG_DIR}:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
redis:
container_name: 'apache7.1-redis'
image: redis:latest
ports:
- "6379:6379"
My webserver Dockerfile;
FROM php:7.1-apache
# Get any build argument overrides
ARG APP_UID
ARG APP_GID
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apt-get clean -y \
&& apt-get update -y \
&& apt-get install -y \
g++ \
locales \
libxml2-dev \
php-soap \
zlib1g-dev \
libicu-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng12-dev \
libmcrypt-dev \
libpng12-dev \
libcurl4-openssl-dev \
libxml2-dev \
nano \
&& apt-get clean -y
RUN docker-php-ext-install mysqli mbstring zip intl mcrypt curl json
RUN docker-php-ext-install iconv xml xmlrpc
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
# Add any required locales here and restart php-fpm, note that some locales do not include currencies such as EURO, if
# this is the case then they will need to be generated in addition to main locale
RUN sed -i -e 's/# en_GB.UTF-8 UTF-8/en_GB.UTF-8 UTF-8/' /etc/locale.gen \
&& sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \
&& sed -i -e 's/# pt_BR.UTF-8 UTF-8/pt_BR.UTF-8 UTF-8/' /etc/locale.gen \
&& sed -i -e 's/# de_AT.UTF-8 UTF-8/de_AT.UTF-8 UTF-8/' /etc/locale.gen \
&& sed -i -e 's/# de_AT#euro ISO-8859-15/de_AT#euro ISO-8859-15/' /etc/locale.gen \
&& sed -i -e 's/# de_DE.UTF-8 UTF-8/de_DE.UTF-8 UTF-8/' /etc/locale.gen \
&& sed -i -e 's/# fr_FR.UTF-8 UTF-8/fr_FR.UTF-8 UTF-8/' /etc/locale.gen \
&& dpkg-reconfigure --frontend=noninteractive locales \
&& kill -USR2 1
RUN pecl install redis-3.1.2 \
&& pecl install xdebug-2.5.0 \
&& docker-php-ext-enable redis xdebug
# Enable apache modules
RUN a2enmod rewrite headers
# Change www-data user to match the host system UID and GID and chown www directory
RUN usermod --non-unique --uid 1000 www-data \
&& groupmod --non-unique --gid 1000 www-data \
&& chown -R www-data:www-data /var/www
You can set the user with the USER directive https://docs.docker.com/engine/reference/builder/#user.
So you would need to for example add USER 1000 or USER www-data in the Dockerfile.
With the combination of USER instruction and chown command inside the Dockerfile, you could change the PHP user and the owner of the root directory to www-data:
FROM php:8.1.1-fpm
.
.
.
RUN chown -R www-data:www-data /var/www
USER www-data
Not that the USER instruction is at the end of the Dockerfile (or before CMD instruction, if it exists), otherwise you might get permission denied error for the next instructions.
I have the following in my docker-compose.yml:
php:
build:
args:
http_proxy:
https_proxy:
no_proxy:
context: ./vm-images/php
environment:
http_proxy:
https_proxy:
no_proxy:
CURRENT_ENVIRONMENT: DOCKER
container_name: php
ports:
- "9000:9000"
depends_on:
- mysql
links:
- mysql:mysql
logging:
driver: "json-file"
volumes:
- /var/www/:/var/www/
My DOCKERFILE has the following:
FROM ubuntu:latest
RUN locale-gen en_US.UTF-8 && \
export LANG=en_US.UTF-8 && \
export LC_ALL=en_US.UTF-8
RUN apt-get update && \
apt-get install -y --reinstall ca-certificates
RUN apt-get update \
&& apt-get install -y --allow-unauthenticated --no-install-recommends \
software-properties-common python-software-properties
RUN add-apt-repository ppa:ondrej/php
RUN apt-get update \
&& apt-get install -y --allow-unauthenticated --no-install-recommends \
git-core \
libjpeg-progs \
mysql-client \
optipng \
php5.6 \
php5.6-curl \
php5.6-intl \
php5.6-fpm \
php5.6-gd \
php5.6-mcrypt \
php5.6-mysqli \
php5.6-pdo \
php5.6-xdebug \
php5.6-xml \
php5.6-zip
RUN rm -r /var/lib/apt/lists/*
RUN rm -r /var/cache/apt/*
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \
php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \
php -r "unlink('composer-setup.php');"
RUN mkdir -p /run/php && chown www-data:www-data /run/php
COPY files/etc/php/fpm/php-fpm.conf /etc/php/5.6/fpm/php-fpm.conf
COPY files/etc/php/fpm/pool.d/www.conf /etc/php/5.6/fpm/pool.d/www.conf
COPY files/etc/php/fpm/php.ini /etc/php/5.6/fpm/php.ini
COPY files/etc/php/cli/php.ini /etc/php/5.6/cli/php.ini
COPY files/etc/php/mods-available/xdebug.ini /etc/php/5.6/mods-available/xdebug.ini
RUN phpenmod curl intl gd mcrypt mysqli pdo xdebug xml zip
CMD ["php-fpm5.6", "--nodaemonize", "--fpm-config", "/etc/php/5.6/fpm/php-fpm.conf"]
When I run docker-compose --verbose up -d I notice that there are no env vars passed in for my php container. I've verified that those env vars are set on my host, and they are passed in for all of my other containers without fail. Anyone have any ideas?
EDIT: Strangely enough, I've also noticed that /proc/$PID/environ (where $PID is the PID of the php container [obtained by running docker inspect --format "{{.State.Pid}}" php] on the host machine is empty.