Add Live containers to Kubernates Cluster - docker

I was working with docker containers to deploy my system and manage it, but now I decided to use Kubernetes in order to make autoscaling for my app, so I have four Containers working on the same network
1- Nginx
2-Mysql
3-Php-fpm
4-Redis
which are configured using this yaml file
version: "3.3" # optional since v1.27.0
networks:
test:
services:
nginx :
image: nginx:stable-alpine
container_name: Test_Qsinav
depends_on:
- php
- mysql
volumes:
- /var/www/html/Test/qSinav-starter:/var/www/html/Test/qSinav-starter/
- /var/www/docker_test/nginx/default.conf:/etc/nginx/conf.d/default.conf
- /var/www/cert:/var/www/cert
networks:
- test
mysql:
image: library/mysql:8.0.21
container_name: QsinavTestMysql
restart: unless-stopped
tty: true
volumes:
- /var/www/docker_test/mysql/my.cnf:/etc/mysql/my.cnf
environment:
MYSQL_DATABASE: qsinav_demo
MYSQL_USER: root
MYSQL_PASSWORD: QsinavPassw0rd!
MYSQL_ROOT_PASSWORD: QsinavPassw0rd!
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- test
php:
build:
context: .
dockerfile: Dockerfile
container_name: Test_php
working_dir: /var/www/html/Test/qSinav-starter
volumes:
- /var/www/html/Test/qSinav-starter:/var/www/html/Test/qSinav-starter
- /var/www/html/docker_laravel/php/www.conf:/usr/local/etc/php-fpm.d/www.conf
- ./php/docker-php-memlimit.ini:/usr/local/etc/php/conf.d/docker-php-memlimit.ini
ports:
- "10000:9000"
networks:
- test
links:
- redis
redis:
image: redis
container_name: TestRedis
networks:
- test
this is the Docker File
FROM php:7.4-fpm-alpine
RUN apk add pcre-dev $PHPIZE_DEPS \
&& pecl install redis \
&& docker-php-ext-enable redis.so
RUN apk add libzip-dev libpng-dev
RUN apk add --update libmemcached-libs zlib
RUN set -xe && \
cd /tmp/ && \
apk add --no-cache --update --virtual .phpize-deps $PHPIZE_DEPS && \
apk add --no-cache --update --virtual .memcached-deps zlib-dev libmemcached-dev cyrus-sasl-dev && \
# Install igbinary (memcached's deps)
pecl install igbinary && \
# Install memcached
( \
pecl install --nobuild memcached && \
cd "$(pecl config-get temp_dir)/memcached" && \
phpize && \
./configure --enable-memcached-igbinary && \
make -j$(nproc) && \
make install && \
cd /tmp/ \
) && \
# Enable PHP extensions
docker-php-ext-enable igbinary memcached && \
rm -rf /tmp/* && \
apk del .memcached-deps .phpize-deps
RUN docker-php-ext-install mysqli pdo pdo_mysql zip gd bcmath exif
RUN docker-php-ext-install bcmath
RUN curl --silent --show-error https://getcomposer.org/installer | php && \
mv composer.phar /usr/local/bin/composer
these containers are working without any problem and I defined volumes in order to change the files in a container from the host
so how can I do this structure with Kubernetes and define HPA
thanks

Related

Database is not created on docker compose up -d

I'm following the docs of mariadb. It says that the db should be created if it find a .sql in /docker-entrypoint-initdb.d.
I'm working on a Ubuntu Server in a Oracle Virtual BOX VM.
My docker-compose.yml looks like this:
version: "3.9"
services:
db:
image: mariadb:10
container_name: mariadb
ports:
- 3306:3306
environment:
- MYSQL_USER=user
- MYSQL_ROOT_PASSWORD=password
- MYSQL_PASSWORD=password
- MARIADB_DATABASE=database // tried with MYSQL_DATABASE and without this line
volumes:
- "db_data:/var/lib/mysql"
- ".database/initdb/dump.sql:/docker-entrypoint-initdb.d/initdb.sql"
# networks:
# - network
volumes:
db_data:
My initdb.sql looks like this (the one that should work in the end looks different but out of simplicity I reduced it to the max and could not even this simple one working):
CREATE DATABASE NEWDB;
I honestly don't know where to look or what to do now because everywhere I looked for a possible solution I found that this is the bare minimum example that should work.
I tried to restarted docker, deleted all containers, images and volumes, modified the initdb.sql into:
CREATE USER user WITH PASSWORD 'password';
CREATE DATABASE IF NOT EXISTS database;
GRANT ALL PRIVILEGES ON DATABASE database TO user;
but the database is not initialized when I docker compose up.
I looked up the container and the initdb.sql was there.
EDIT: It somehow worked, when I docker compose up with MARIADB_DATABASE=database but the script initdb.sql still doesn't work and it's the most important thing because it set's up the whole database.
(NOTE: On top of that I want to set up another PHP-container that runs a PHP-script in order to collect data that is being stored in the above MariaDB-container. The MariaDB is connected with a website that calls data from the container)
Well I'm using the following stack and it works fine for me.
php-apache:
This is an Apache server that runs all my php scripts. You can place your scripts in ./src directory and it will automatically be mounted to DocumentRoot directory of the Apache server.
db:
This the latest docker container of MariaDb
adminer:
This is the lite-weight database browser which I use for creating and altering my databases. You can just visit localhost:8081 and then enter the following credentials. It becomes simpler to manage the databases this way.
username: root
password: example
version: '3.8'
services:
php-apache:
container_name: php-apache
build:
context: .
dockerfile: Dockerfile
image: php:8.0-apache
volumes:
- ./src:/var/www/html/
ports:
- 8080:80
db:
image: mariadb
restart: always
environment:
MARIADB_ROOT_PASSWORD: example
adminer:
image: adminer
restart: always
ports:
- 8081:8080
Dockerfile:
This is a simple docker container which is extended from the base php:8.0-apache image, with mysql extensions installed in it for PDO support.
FROM php:8.0-apache
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN apt-get update && apt-get upgrade -y
P.S:
Here you'll have to create all your databases manually via GUI of Adminer. But if you prefer SQL queries via initdb.sql then be my guest. I've just provided this configuration as a suggestion.
I came up with a solution. I used a base image of laravel (installed the laravel project with <curl -s "https://laravel.build/project-name?with=mariadb" | sudo bash> and modified it a little bit. So here's the docker-compose.yml:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
- '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mariadb
mariadb:
image: 'mariadb:10'
container_name: 'mariadb-10'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sail-mariadb:/var/lib/mysql'
- './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sail-mariadb:
driver: local
Here you can see that the "10-create-testing-database.sh" is executed on startup. I tested this container and it created a database, so I just had to modify it a little bit and now the container creates a database and tables on container startup. Here's the "10-create-testing-database.sh":
#!/usr/bin/env bash
mysql --user=root --password="$MYSQL_ROOT_PASSWORD" <<-EOSQL
CREATE DATABASE IF NOT EXISTS database_name;
GRANT ALL PRIVILEGES ON \`testing%\`.* TO '$MYSQL_USER'#'%';
USE database_name;
CREATE TABLE IF NOT EXISTS table_name(
table_entries ...
);
EOSQL
I still don't know why my initial setup did not work. The only difference I see is that this working file is a .sh and the not working one is a .sql (this does not make sence to me but it what it is).
Dockerfile:
FROM ubuntu:22.04
LABEL maintainer="Taylor Otwell"
ARG WWWGROUP
ARG NODE_VERSION=16
ARG POSTGRES_VERSION=14
WORKDIR /var/www/html
ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update \
&& apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 \
&& mkdir -p ~/.gnupg \
&& chmod 600 ~/.gnupg \
&& echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf \
&& echo "keyserver hkp://keyserver.ubuntu.com:80" >> ~/.gnupg/dirmngr.conf \
&& gpg --recv-key 0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c \
&& gpg --export 0x14aa40ec0831756756d7f66c4f4ea0aae5267a6c > /usr/share/keyrings/ppa_ondrej_php.gpg \
&& echo "deb [signed-by=/usr/share/keyrings/ppa_ondrej_php.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
&& apt-get update \
&& apt-get install -y php8.1-cli php8.1-dev \
php8.1-pgsql php8.1-sqlite3 php8.1-gd \
php8.1-curl \
php8.1-imap php8.1-mysql php8.1-mbstring \
php8.1-xml php8.1-zip php8.1-bcmath php8.1-soap \
php8.1-intl php8.1-readline \
php8.1-ldap \
php8.1-msgpack php8.1-igbinary php8.1-redis php8.1-swoole \
php8.1-memcached php8.1-pcov php8.1-xdebug \
&& php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
&& curl -sLS https://deb.nodesource.com/setup_$NODE_VERSION.x | bash - \
&& apt-get install -y nodejs \
&& npm install -g npm \
&& curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | tee /usr/share/keyrings/yarn.gpg >/dev/null \
&& echo "deb [signed-by=/usr/share/keyrings/yarn.gpg] https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
&& curl -sS https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | tee /usr/share/keyrings/pgdg.gpg >/dev/null \
&& echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt jammy-pgdg main" > /etc/apt/sources.list.d/pgdg.list \
&& apt-get update \
&& apt-get install -y yarn \
&& apt-get install -y mysql-client \
&& apt-get install -y postgresql-client-$POSTGRES_VERSION \
&& apt-get -y autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.1
RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail
COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/8.1/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container
EXPOSE 8000
ENTRYPOINT ["start-container"]

running existing yii1 application in docker

I have a existing yii1 application. And I am using local xampp.
So I try to dockerise the existing yii1 application.
So This is my dockerfile in root folder:
FROM php:8.1-apache as dev
ENV DEBIAN_FRONTEND=noninteractive
ENV APP_ENV=development
WORKDIR /var/www/html
RUN apt-get update \
&& apt-get -y install --no-install-recommends apt-utils zip unzip nano ncdu 2>&1 \
&& apt-get -y install --no-install-recommends python graphviz 2>&1 \
&& apt-get -y install git iproute2 procps lsb-release \
&& apt-get install -y -qq software-properties-common \
&& apt-get install -y -qq wget git lynx ack-grep \
&& yes | pecl install xdebug \
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
&& apt-get -y install libicu-dev \
&& docker-php-ext-install intl pdo_mysql opcache \
&& pecl install apcu && docker-php-ext-enable apcu \
&& echo "apc.enable_cli=1" > /usr/local/etc/php/php.ini \
&& echo "apc.enable=1" > /usr/local/etc/php/php.ini \
&& echo "post_max_size = 100M" > /usr/local/etc/php/php.ini \
&& a2enmod rewrite \
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*
RUN apt-get update && apt-get install gnupg2 -y
RUN rm -rf /etc/apache2/sites-enabled \
&& ln -s /var/www/html/.devcontainer/sites-enabled /etc/apache2/sites-enabled
RUN echo 'alias ll="ls -la --color=auto"' >> ~/.bashrc && \
echo "alias ack='ack-grep'" >> ~/.bashrc
RUN chown www-data:www-data -R ./
ENV DEBIAN_FRONTEND=dialog
and this is my docker-compose.yml file also in root folder:
version: '3'
services:
web:
image: nguyenmanhluu/yii1:1.0
container_name: dockeryiidisc
ports:
- "9002:80"
build: .
volumes:
- ../:/var/www/html
command: /bin/sh -c "service apache2 start && while sleep 1000; do :; done"
db:
container_name: dockeryiimysql
image: mysql:latest
volumes:
- dockeryiimysql:/var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
MYSQL_DATABASE: sdi
phpmyadmin:
container_name: dockeryiipma
image: phpmyadmin:latest
environment:
UPLOAD_LIMIT: 300M
PMA_ARBITRARY: 1
APACHE_HTTP_PORT_NUMBER: 8080
ports:
- 8080:8080
command: /bin/bash -c "sed -i \"s/80/$$APACHE_HTTP_PORT_NUMBER/g\" /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf && /docker-entrypoint.sh apache2-foreground"
volumes:
dockeryiimysql: {}
So if I go to my root folder and do a docker-compose up. Then I see in docker desktop all three containers are running.
And if I go to: localhost:8080 I see the phpmyadmin database.
But if I go to localhost:9002 I see the startscreen of xampp. And I dont be redirected to the real application.
So what I have to change?
Thank you.
What I mean I will be redirected to: http://localhost:9002/dashboard/
I solved, like this:
version: '3'
services:
web:
image: nguyenmanhluu/yii1:1.0
container_name: dockeryiidisc
ports:
- "8082:80"
build:
context: ..
dockerfile: Dockerfile
target: dev
volumes:
- ./:/var/www/html
command: /bin/sh -c "service apache2 start && while sleep 1000; do :; done"
db:
container_name: dockeryiimysql
image: mysql:latest
volumes:
- dockeryiimysql:/var/lib/mysql
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
MYSQL_DATABASE: sdi
phpmyadmin:
container_name: dockeryiipma
image: phpmyadmin:latest
environment:
UPLOAD_LIMIT: 300M
PMA_ARBITRARY: 1
APACHE_HTTP_PORT_NUMBER: 8080
ports:
- 8080:8080
command: /bin/bash -c "sed -i \"s/80/$$APACHE_HTTP_PORT_NUMBER/g\" /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf && /docker-entrypoint.sh apache2-foreground"
volumes:
dockeryiimysql: {}

Docker-compose "no such service"

I have an error launching docker-compose up -d with this docker-compose.yml file: no such service: php-c
It used to work and I didn't change anything. I didn't have any docker update in the meantime either.
If I remove container_name key then I have the same error displayed with the name which is auto-generated.
I am using wsl2 and docker 20.10.8.
version: '3'
services:
db:
image: mysql:5.7
container_name: db-c
ports:
- 3307:3306
volumes:
- "./.data/db:/var/lib/mysql"
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
php:
build:
context: php7-fpm
args:
TIMEZONE: ${TIMEZONE}
username: ${username}
container_name: php-c
volumes:
- ${SYMFONY_APP_PATH}:/var/www/symfony
- ./logs/symfony/:/var/www/symfony/var/logs
environment:
- XDEBUG_REMOTE_HOST=host.docker.internal
- PHP_IDE_CONFIG=serverName=symfony.local
user: 1000:1000
nginx:
build: nginx
container_name: nginx-c
ports:
- 80:80
volumes_from:
- php
volumes:
- ./logs/nginx/:/var/log/nginx
Here is the Dockerfile.
# See https://github.com/docker-library/php/blob/master/7.1/fpm/Dockerfile
FROM php:7.3-fpm
ARG TIMEZONE
ARG username=bastien
MAINTAINER Maxence POUTORD <maxence.poutord#gmail.com>
RUN apt-get update && apt-get install -y \
openssl \
git \
unzip \
libicu-dev \
sudo \
lsof \
zlib1g-dev \
libzip-dev \
iputils-ping
# Install GD
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN composer --version
# Set timezone
RUN ln -snf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && echo ${TIMEZONE} > /etc/timezone
RUN printf '[PHP]\ndate.timezone = "%s"\n', ${TIMEZONE} > /usr/local/etc/php/conf.d/tzone.ini
RUN "date"
# Type docker-php-ext-install to see available extensions
RUN docker-php-ext-install pdo pdo_mysql zip
# install xdebug
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
RUN cd /usr/local/etc/php/conf.d/ && \
echo 'memory_limit = 1G' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini
COPY xdebug.ini /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
#RUN echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
# & echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
# & echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
# & echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
# & echo "xdebug.remote_connect_back=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
# & echo "xdebug.idekey=\"PHPSTORM\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
# & echo "xdebug.remote_port=9001" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
# CREATE USER FOR $username
RUN useradd --create-home --shell /bin/bash -u 1000 $username
USER $username
RUN mkdir /home/$username/.ssh \
&& chmod 700 /home/$username/.ssh
COPY .bashrc /home/$username/.bashrc
COPY config /home/$username/.ssh/config
WORKDIR /var/www/symfony
[EDIT]
When I use restart, this works... I really don't understand what's going on.
docker-compose restart
I had the same problem.
It seems that the volumes_from configuration is the source of the problem. (might be a docker-compose bug)
Replacing volumes_from with volumes did the trick for me.
Try to use the following for your nginx service configuration:
nginx:
build: nginx
container_name: nginx-c
ports:
- 80:80
volumes:
- ${SYMFONY_APP_PATH}:/var/www/symfony
- ./logs/symfony/:/var/www/symfony/var/logs
- ./logs/nginx/:/var/log/nginx
[EDIT] : better answer
Please : replace php7-form by ./php7-form
context: ./php7-fpm
it worked for me locally using docker-compose up --build with the following example:
version: '3'
services:
php:
build:
context: ./php7-form
environment:
- XDEBUG_REMOTE_HOST=host.docker.internal
- PHP_IDE_CONFIG=serverName=symfony.local
user: 1000:1000
[previous answer]
At the moment no image exists for your service.
You don't have an image in the "php" service.
you need to add an image in your docker-compose file bellow php: as you did for your db service :
php:
image: <your_image_name>
Btw you will have the same problem with the nginx service...

When I want to rebuild docker image it generates an error

I made docker images and built them using docker-compose.yml, everything works fine for the first time but when I add/update some configurations on the Dockerfiles and rebuild the images an error regarding the group or the user I am creating inside the image generates:
> [ 2/11] RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel:
#5 0.396 addgroup: group 'laravel' in use
Here I am adding laravel as user and group.
The only work around I make is to delete all the built images: docker system prune -a
Below are Docker files and docker-compose.yml file configuration:
.env file:
PROJECT_NAME=myproject
DEV_DOMAIN=docker.local
USERNAME=laravel
UID=1000
PHP_VERSION=7.4-fpm-alpine
MYSQL_VERSION=latest
MYSQL_DATABASE=local_db
MYSQL_USER=homestead
MYSQL_PASSWORD=secret
XDEBUG_REMOTE_HOST=172.31.33.213
XDEBUG_IDE_KEY=VSCODE
XDEBUG_PORT=7765
docker-compose.yml:
version: "3.5"
services:
nginx:
build:
args:
user: ${USERNAME}
uid: ${UID}
context: .
dockerfile: ./docker/nginx/Dockerfile
image: "nginx:stable-alpine"
container_name: ${PROJECT_NAME}-nginx
ports:
- 80:80
volumes:
- "./code:/var/www/html"
- "./docker/nginx/vhost:/etc/nginx/conf.d/default.conf"
- "./docker/nginx/nginx.conf:/etc/nginx/nginx.conf"
depends_on:
- php
- mysql
networks:
- devproject
php:
build:
args:
PHP_VERSION: ${PHP_VERSION}
user: ${USERNAME}
uid: ${UID}
XDEBUG_REMOTE_HOST: ${XDEBUG_REMOTE_HOST}
XDEBUG_IDE_KEY: ${XDEBUG_IDE_KEY}
XDEBUG_PORT: ${XDEBUG_PORT}
context: .
dockerfile: ./docker/php/Dockerfile
image: php:${PHP_VERSION}
container_name: ${PROJECT_NAME}-php
volumes:
- "./code:/var/www/html"
- "./docker/php/www.conf:/usr/local/etc/php-fpm.d/www.conf"
ports:
- 9000:9000
networks:
- devproject
mysql:
image: mysql:${MYSQL_VERSION}
container_name: ${PROJECT_NAME}-mysql
restart: unless-stopped
tty: true
ports:
- 3306:3306
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- devprojectmysql:/var/lib/mysql
networks:
- devproject
networks:
devproject:
driver: bridge
volumes:
devprojectmysql:
driver: local
docker/php/Dockerfile:
ARG PHP_VERSION
FROM php:$PHP_VERSION
ARG XDEBUG_REMOTE_HOST
ARG XDEBUG_IDE_KEY
ARG XDEBUG_PORT
ARG user
ARG uid
ENV XDEBUG_REMOTE_HOST $XDEBUG_REMOTE_HOST
ENV XDEBUG_IDE_KEY $XDEBUG_IDE_KEY
ENV XDEBUG_PORT $XDEBUG_PORT
RUN addgroup -g $uid $user && adduser -G $user -g $user -s /bin/sh -D $user
RUN mkdir -p /var/www/html
RUN chown $user:$user /var/www/html
WORKDIR /var/www/html
RUN apk add --no-cache --update icu-dev gettext gettext-dev git curl libpng-dev libzip-dev \
libmcrypt-dev libpng-dev libjpeg-turbo-dev libxml2-dev icu-dev postgresql-dev curl-dev \
libmemcached-dev build-base autoconf zip unzip oniguruma-dev &&\
apk add --update --virtual build-dependencies build-base gcc wget autoconf
RUN pecl install xdebug-3.0.4
RUN docker-php-ext-install gd ctype fileinfo mysqli pdo pdo_mysql mbstring exif pcntl bcmath xml curl json intl gettext tokenizer zip simplexml
RUN docker-php-ext-configure intl &&\
docker-php-ext-configure gettext &&\
docker-php-ext-enable xdebug
RUN apk del build-dependencies &&\
rm -rf /var/cache/apk/* &&\
rm -rf /var/lib/apt/lists/*
RUN echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_handler=dbgp" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_log=/tmp/xdebug_remote.log" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_port=$XDEBUG_PORT" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.idekey=$XDEBUG_IDE_KEY" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_host=$XDEBUG_REMOTE_HOST" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
docker/nginx/Dockerfile:
FROM nginx:stable-alpine
# Arguments defined in docker-compose.yml
ARG user
ARG uid
RUN mkdir -p /var/www/html
RUN addgroup -g $uid $user && adduser -G $user -g $user -s /bin/sh -D $user
RUN chown $user:$user /var/www/html
The image field in the compose file, e.g.
image: php:${PHP_VERSION}
Is the image you are creating. You have named your created images the same as your base images in the Dockerfile, overwriting the upstream names. This is a bug in your compose file, and confusing to others since running an image with that name does different things depending on whether you pull the upstream image or run your locally built image.
Instead your compose file should name images with something in your own registry namespace to avoid collisions. In Hub that involves prepending your Hub username:
image: <hubuser>/<appname>:<tag>

Docker container killed after Ctrl +C

I have a nginx and php-fpm containers.
When I'am in my php container in a projet and I exec any command (like vendor/bin/behat or composer update) who takes time and I click on CTRL+C. I'm ejected from the container. I don't know why.. When I click on CTRL+C without executing commands i don't have the problem.
Any idea ?
This is my docker-compose.yml file :
version: '3'
services:
nginx:
image: nginx:latest
restart: always
ports:
- "80:80"
volumes:
- ./nginx/conf:/etc/nginx/custom_conf
- ./nginx/hosts:/etc/nginx/conf.d/
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./logs/nginx:/var/log/nginx
- ..:/var/www
networks:
my_network:
ipv4_address: 10.5.0.31
web:
build: .
restart: always
ports:
- "9000:9000"
- "5001:5001"
volumes:
- ./php/php.ini:/usr/local/etc/php/conf.d/30-php.ini
- ./php/app2.conf:/usr/local/etc/php/conf.d/app2.conf
- ./keys/:/var/www/.ssh
- ./custom-hosts:/etc/custom-hosts
- ..:/var/www
- ./supervisor/supervisord.conf:/etc/supervisor/supervisord.conf
- ./supervisor/conf/:/etc/supervisor/conf.d/
networks:
my_network:
ipv4_address: 10.5.0.20
tty: true
db:
build: mysql
restart: always
ports:
- "3306:3306"
volumes:
- ./logs/mysql:/var/log/mysqld.log
- ./mysql/sql:/var/dumps
- data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=root
- MYSQL_PASSWORD=root
networks:
my_network:
ipv4_address: 10.5.0.23
volumes:
data:
driver: local
networks:
my_network:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
My php-fpm Dockerfile:
FROM php:7.1-fpm
WORKDIR /var/www
RUN apt-get update && apt-get install -y wget git vim sudo unzip apt-utils
RUN apt-get install -y gnupg
RUN apt-get update
### composer
RUN cd /usr/src
RUN curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
# xdebug
RUN pecl install xdebug-2.5.0 \
&& docker-php-ext-enable xdebug
### php extension
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
RUN apt-get clean && apt-get update && apt-get -y --fix-missing install libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
libicu-dev \
libxml2-dev \
g++ \
zlib1g-dev
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) gd
RUN docker-php-ext-install exif
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-configure intl
RUN docker-php-ext-install intl
RUN apt-get install -y libzip-dev
RUN docker-php-ext-install zip
### main
RUN usermod -u 1000 www-data
RUN chmod -R 777 /var/www/
RUN chown -R www-data:www-data /var/www
ADD bash_profile /var/www/.bash_profile
ADD script.sh /usr/bin/script.sh
RUN chmod 755 /usr/bin/script.sh
CMD ["bin/bash"]
ENTRYPOINT ["script.sh"]
EXPOSE 9000
And my script.sh :
#! /bin/bash
php-fpm &
echo "Serveur de développement Cartesia Education"
cat /etc/custom-hosts >> /etc/hosts
dpkg-reconfigure -f noninteractive tzdata
echo "LC_TIME=fr_FR.utf8" >> /etc/environment
service supervisor start
exec su -l www-data -s /bin/bash
Thank you for your help.
Have you tried running the container in detached mode (-d option)?
> docker run -d [CONTAINER-NAME]
This will cause the container to run in the background. You can still SSH into the running container by:
> docker exec -it [CONTAINER-NAME] bash
Exiting the container once in will not terminate it.

Resources