container not building with mentioned dockerfile - multiple containers - mysql

I am working with a case where there is a main kernel application that uses mysql and influxdb as the databases and grafana for showing the data pictorially. I am creating separate containers for each using docker-compose and individual Dockerfiles.
The folder structure is as such :
dockercompose
docker-compose.yml
kernel
Dockerfilekernel
app.war
sqldb
Dockerfilesqldb
dump.sql
startup.sh
grafana
Dockerfilegrafana
grafana.env
run.sh
tsdbdb
Dockerfiletsdb
influxdb.env
init-influxdb.sh
entrypoint.sh
The container for kernel builds but on checking the logs, it is displaying the logs of influxdb, the same that displays on checking the logs for tsdbdb container.
Code for docker-compose :
version: '3'
services:
mysqldb:
build:
context: ./sqldb
dockerfile: Dockerfilesqldb
container_name: sql
command: mysqld --user=root --verbose
volumes:
- ./sqldb:/app/sql
restart: always
expose:
- "3306"
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=secretpassword
- MYSQL_DATABASE=dbtest
- MYSQL_USER=user
- MYSQL_PASSWORD=password
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
tsdbdb:
build:
context: ./tsdbdb
dockerfile: Dockerfiletsdb
container_name: influx
restart: always
expose:
- "8083"
- "8086"
ports:
- "8083:8083"
- "8086:8086"
volumes:
- ./tsdb:/app/tsdb
env_file:
- './tsdbdb/influxdb.env'
grafana:
build:
context: ./grafana
dockerfile: Dockerfilegrafana
container_name: grafana
env_file:
- './grafana/grafana.env'
expose:
- "3000"
ports:
- "3003:3000"
links:
- tsdbdb
web:
build:
context: ./kernel
dockerfile: Dockerfilekernel
container_name: kernel
restart: always
expose:
- "8080"
ports:
- "8082:8080"
depends_on:
- mysqldb
links:
- tsdbdb
- mysqldb
volumes:
- ./kernel:/app/kernel
Dockerfile of kernel :
FROM java:openjdk-8-jre-alpine
ENV JHIPSTER_SLEEP 0
WORKDIR /app/kernel
# add directly the war
ADD *.war /app/kernel/app.war
RUN sh -c 'touch /app/kernel/app.war'
VOLUME /tmp
EXPOSE 8080
CMD echo "The application will start in ${JHIPSTER_SLEEP}s..." && \
sleep ${JHIPSTER_SLEEP} && \
java -Djava.security.egd=file:/dev/./urandom -jar /app/kernel/app.war
Dockerfile for influxdb, just in case :
FROM buildpack-deps:stretch-curl
WORKDIR /app/tsdb
RUN set -ex && \
for key in \
05CE15085FC09D18E99EFB22684A14CF2582E0C5 ; \
do \
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key" || \
gpg --keyserver pgp.mit.edu --recv-keys "$key" || \
gpg --keyserver keyserver.pgp.com --recv-keys "$key" ; \
done
ENV INFLUXDB_VERSION 1.2.4
ENV INFLUXDB_ADMIN_PASSWORD secretpassword
ENV INFLUXDB_DB master_db
RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" && \
case "${dpkgArch##*-}" in \
amd64) ARCH='amd64';; \
arm64) ARCH='arm64';; \
armhf) ARCH='armhf';; \
armel) ARCH='armel';; \
*) echo "Unsupported architecture: ${dpkgArch}"; exit 1;; \
esac && \
wget -q https://dl.influxdata.com/influxdb/releases/influxdb_${INFLUXDB_VERSION}_${ARCH}.deb.asc && \
wget -q https://dl.influxdata.com/influxdb/releases/influxdb_${INFLUXDB_VERSION}_${ARCH}.deb && \
gpg --batch --verify influxdb_${INFLUXDB_VERSION}_${ARCH}.deb.asc influxdb_${INFLUXDB_VERSION}_${ARCH}.deb && \
dpkg -i influxdb_${INFLUXDB_VERSION}_${ARCH}.deb && \
rm -f influxdb_${INFLUXDB_VERSION}_${ARCH}.deb*
COPY influxdb.conf /etc/influxdb/influxdb.conf
EXPOSE 8086
VOLUME /var/lib/influxdb
COPY entrypoint.sh /entrypoint.sh
COPY init-influxdb.sh /init-influxdb.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["influxd"]
It might be possible that this is happening due to a silly mistake but I am not able to find where exactly is the problem.

So I was able to solve the problem by doing the following -
Changed the service name from web to kernel and in docker-compose.yml I added the command line for kernel service:
version: '3'
services:
mysqldb:
build:
context: ./sqldb
dockerfile: Dockerfilesqldb
container_name: sql
command: mysqld --user=root --verbose
# volumes:
# - ./sqldb:/app/sql
restart: always
# expose:
# - "3306"
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=secretpassword
- MYSQL_DATABASE=dbtest
- MYSQL_USER=user
- MYSQL_PASSWORD=password
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
tsdbdb:
build:
context: ./tsdbdb
dockerfile: Dockerfiletsdb
container_name: influx
restart: always
# expose:
# - "8083"
# - "8086"
ports:
- "8083:8083"
- "8086:8086"
# volumes:
# - ./tsdb:/app/tsdb
env_file:
- './tsdbdb/influxdb.env'
grafana:
build:
context: ./grafana
dockerfile: Dockerfilegrafana
container_name: grafana
env_file:
- './grafana/grafana.env'
expose:
- "3000"
ports:
- "3003:3000"
links:
- tsdbdb
kernel:
build:
context: ./kernel
dockerfile: Dockerfilekernel
container_name: kernel
command: java -Djava.security.egd=file:/dev/./urandom -jar /app/kernel/app.war
restart: always
# expose:
# - "8080"
ports:
- "8082:8080"
links:
- tsdbdb
- mysqldb
# volumes:
# - ./kernel:/app/kernel
Although I am not sure why only adding command solved the problem.

Related

Docker cannot execute mysqld

I have a Laravel app that was running fine up until a day ago.
Now, anytime I run a "sail up" command, Mysql will start and then immediately shut down with the following error:
[Entrypoint] MySQL Docker Image 8.0.31-1.2.10-server
/entrypoint.sh: line 57: /usr/sbin/mysqld: cannot execute binary file: Exec format error
[Entrypoint] ERROR: Unable to start MySQL. Please check your configuration.
I'm not sure if this has anything to do with it or if it's just a coincidence but this issue seems to have started when I tried to run another "sail up" command on a different Laravel app.
I've tried the following
Tried to replicate in a fresh Laravel app. The same result occurs.
Tried to start the entrypoint with the -l and -c options. The same result occurs.
Tried to start the application with mysql server 5.7. The same result occurs.
Tried forwarding to another port 3307, instead of 3306. The same result occurs.
Both my docker-compose and Dockerfile are pretty standard...no changes from what Laravel provides:
Docker-composer
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./docker/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:
- mysql
- redis
- meilisearch
- mailhog
- selenium
mysql:
image: 'mysql/mysql-server:8.0'
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: 1
volumes:
- 'sail-mysql:/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
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
meilisearch:
image: 'getmeili/meilisearch:latest'
ports:
- '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
volumes:
- 'sail-meilisearch:/meili_data'
networks:
- sail
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--spider", "http://localhost:7700/health"]
retries: 3
timeout: 5s
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- '${FORWARD_MAILHOG_PORT:-1025}:1025'
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
networks:
- sail
selenium:
image: 'selenium/standalone-chrome'
volumes:
- '/dev/shm:/dev/shm'
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
sail-redis:
driver: local
sail-meilisearch:
driver: local
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"]
I'm not really sure what the issue is. Anyone have any suggestions?
Thanks.

Mysql connection refused in docker-compose

I have a problem connecting to MySQL from my golang app in docker-compose. I can connect to db from console: mysql -u user -D data -h 0.0.0.0 -P3306 -p
But, I can't connect when using docker-compose.
My docker-compose.yml
version: '3'
services:
app:
build: ./
volumes:
- ./internal/app:/app
- ./logs:/var/log/parser
links:
- db
db:
image: mysql:8
container_name: mysqldb
ports:
- "3306:3306"
volumes:
- ./docker/mysql/conf:/etc/mysql/conf/conf.d
- ./docker/mysql/logs/:/var/log/mysql
- ./docker/mysql/init:/docker-entrypoint-initdb.d
- ./docker/mysql/data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: data
MYSQL_USER: user
MYSQL_PASSWORD: pass
In my app main.go
dbConfig := mysql.NewConfig()
dbConfig.User = "user"
dbConfig.Passwd = "pass"
dbConfig.Addr = "mysqldb"
dbConfig.DBName = "data"
dbConfig.Net = "tcp"
db, err := sql.Open("mysql", dbConfig.FormatDSN())
if err != nil {
panic(err.Error())
}
defer db.Close()
But I get this error:
panic: dial tcp 172.20.0.2:3306: connect: connection refused
I think your golang service starts before the MySQL service. so you have to start MySQL service first then golang service so, use depends_on to achieve that.
new docker-compose.yml with depends_on
version: '3'
services:
app:
build: ./
volumes:
- ./internal/app:/app
- ./logs:/var/log/parser
depends_on:
- mysqldb
links:
- db
db:
image: mysql:8
container_name: mysqldb
ports:
- "3306:3306"
volumes:
- ./docker/mysql/conf:/etc/mysql/conf/conf.d
- ./docker/mysql/logs/:/var/log/mysql
- ./docker/mysql/init:/docker-entrypoint-initdb.d
- ./docker/mysql/data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: pass
MYSQL_DATABASE: data
MYSQL_USER: user
MYSQL_PASSWORD: pass

Access denied for user 'root'#'192.168.128.3' in Laravel project in Docker

I try to set up Laravel project in Docker.
My docker-compose.yml:
version: "2"
services:
web:
build:
context: .
dockerfile: Dockerfile
environment:
- MYSQL_DATABASE=identy
- MYSQL_USER=root
- MYSQL_PASSWORD=654321
- MYSQL_HOST=db
ports:
- "8080:80"
volumes:
- .:/var/www
depends_on:
- db
networks:
- identy-network
db:
image: mysql:8.0
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=654321
- MYSQL_USER=root
- MYSQL_PASSWORD=654321
- MYSQL_DATABASE=identy
volumes:
- "mysql_data:/var/lib/mysql"
- ./data/schema.sql:/docker-entrypoint-initdb.d/schema.sql
networks:
- identy-network
volumes:
mysql_data:
driver: local
networks:
identy-network:
My Dockerfile:
FROM php:7.4-apache
RUN apt-get update \
&& apt-get install -y vim git zlib1g-dev mariadb-client libzip-dev \
&& docker-php-ext-install zip mysqli pdo_mysql \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& echo 'xdebug.remote_enable=on' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_host=host.docker.internal' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_port=9000' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& a2enmod rewrite \
&& sed -i 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/000-default.conf \
&& mv /var/www/html /var/www/public \
&& curl -sS https://getcomposer.org/installer \
| php -- --install-dir=/usr/local/bin --filename=composer \
&& echo "AllowEncodedSlashes On" >> /etc/apache2/apache2.conf
WORKDIR /var/www
.env:
...
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=identy
DB_USERNAME=root
DB_PASSWORD=654321
...
When I run
docker exec identy-api_web_1 php artisan migrate
I get error:
** Illuminate\Database\QueryException
SQLSTATE[HY000] [1045] Access denied for user 'root'#'192.168.144.3' (using password: YES) (SQL: select * from information_schema.tables where table_schema = identy and table_name = migrations and table_type = 'BASE TABLE')
**
I have no idea why it doesn't work.
I think, You don't have permission to access the database from remote host on the mysql server. You need to allow access remotely to root from any host on the mysql server.
GRANT ALL PRIVILEGES ON *.* TO 'root'#'192.168.144.3' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
I think you should remove the volumes data and try again.
You don't need to define MYSQL_USER=root. Try this one (it creates identy db as well):
db:
image: mysql:8.0
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=654321
networks:
- identy-network
volumes:
- "mysql_data:/var/lib/mysql"
entrypoint: sh -c "
echo 'CREATE DATABASE IF NOT EXISTS identy;' > /docker-entrypoint-initdb.d/init.sql;
/usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci"

Windows Docker Can't connect to local MySQL server through socket

I have a project on windows Docker, when I run the docker-compose up, it gives me the bellow error
php_1 | Database tma not reachable at host: localhost for user: drupal
php_1 | ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)
Here is the docker-compose.yml
version: "3"
services:
mariadb:
image: wodby/mariadb:$MARIADB_TAG
container_name: "${PROJECT_NAME}_mariadb"
stop_grace_period: 30s
environment:
MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
MYSQL_DATABASE: $DB_NAME
MYSQL_USER: $DB_USER
MYSQL_PASSWORD: $DB_PASSWORD
volumes:
- ./dev_tools/mariadb-init:/docker-entrypoint-initdb.d # Place init .sql file(s) here.
php:
image: wodby/drupal-php:$PHP_TAG
container_name: "${PROJECT_NAME}_php"
environment:
PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
# PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S opensmtpd:25
DB_HOST: $DB_HOST
DB_PORT: $DB_PORT
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
DB_NAME: $DB_NAME
DB_DRIVER: $DB_DRIVER
PHP_FPM_USER: wodby
PHP_FPM_GROUP: wodby
COLUMNS: 80 # Set 80 columns for docker exec -it.
volumes:
- ./:/var/www/html/
command: sh /var/www/html/dev_tools/php-entrypoint.bash
apache:
image: wodby/apache:$APACHE_TAG
container_name: "${PROJECT_NAME}_apache"
depends_on:
- php
environment:
APACHE_LOG_LEVEL: debug
APACHE_BACKEND_HOST: php
APACHE_VHOST_PRESET: php
APACHE_DOCUMENT_ROOT: /var/www/html/docroot
volumes:
- ./:/var/www/html
labels:
- "traefik.http.routers.${PROJECT_NAME}_apache.rule=Host(`${PROJECT_BASE_URL}`)"

php cannot connect to mysql in docker-compose

Here is my docker-compose
version: '2'
services:
nginx:
image: nginx:1.11.8-alpine
ports:
- "8081:80"
volumes:
- ./code:/usr/share/nginx/html
- ./html:/myapp
- ./site.conf:/etc/nginx/conf.d/site.conf
- ./default.conf:/etc/nginx/conf.d/default.conf
- ./error.log:/var/log/nginx/error.log
- ./nginx.conf:/etc/nginx/nginx.conf
links:
- phpfpm
phpfpm:
image: php7-fpm:latest
ports:
- "9000:9000"
volumes:
- ./code:/usr/share/nginx/html
links:
- db_mysql
db_mysql:
image: mysql:5.7.17
volumes:
- db_data:/var/lib/mysql
# restart: no
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wp2017
MYSQL_USER: wp
MYSQL_PASSWORD: wp2017
volumes:
db_data:
Here is how I build my own php7-fpm:lastest
FROM php:7.1-fpm-alpine
RUN docker-php-ext-install mysqli
I cannot connect to mysql container
$serverName = 'localhost';
$userName = 'wp';
$password = 'wp2017';
$dbName = 'wp2017';
$link = mysqli_connect($serverName, $userName, $password, $dbName);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
I always get error: "
Warning: mysqli_connect(): (HY000/2002): No such file or directory in /usr/share/nginx/html/db.php on line 8
Connect failed: No such file or directory"
Where I run command
sudo netstat -tulpn | grep :3306
I get this
tcp6 0 0 :::3306 :::* LISTEN 15362/docker-proxy
Please help !
Change PHP script
$serverName = 'db_mysql';
It will work.