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.
Related
Docker-Compose file:
version: '3.8'
networks:
app-tier:
driver: bridge
services:
mysql_node:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'sample_db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- mysql_db:/var/lib/mysql
app:
depends_on:
- mysql_node
build: .
ports:
- 3000:3000
environment:
- MYSQL_HOST=mysql_node
- MYSQL_USER=root
- MYSQL_PASSWORD=password
- MYSQL_NAME=sample_db
- MYSQL_PORT=3306
- MYSQL_HOST_IP=mysql_node
networks:
- app-tier
volumes:
- .:/app
volumes:
mysql_db:
docker file:
FROM node:latest
RUN mkdir app
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["node", "app.js"]
Sourcecode:
const http = require('http')
var mysql = require('mysql');
const hostname = '0.0.0.0'
const port = 3000
console.log(process.env.MYSQL_HOST_IP)
console.log(process.env.MYSQL_PORT)
var con = mysql.createConnection({
host: "process.env.MYSQL_HOST_IP",
user: "user",
password: "password",
port: 3306
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
When I run docker-compose up the MySQL container successfully launches. But the app exits, with error code Error: connect ETIMEDOUT.
When I run the app locally via node app.js I end up getting a successful "Connected!" message output to my console.
I'm able to connect to the database via MySQLWorkbench as well. It's just the app is unable to connect to the db when the app is ran as a container.
You've specified that the app should be on a named network and the database shouldn't. Then the database service goes on the default network and the two containers can't talk to each other.
I'd remove the named network from the app, since it's probably not needed.
app:
depends_on:
- mysql_node
build: .
ports:
- 3000:3000
environment:
- MYSQL_HOST=mysql_node
- MYSQL_USER=root
- MYSQL_PASSWORD=password
- MYSQL_NAME=sample_db
- MYSQL_PORT=3306
- MYSQL_HOST_IP=mysql_node
volumes:
- .:/app
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
I am trying to setup some containers for my NestJS + TypeORM + MySQL environment by using Docker Compose in a Windows 10 host, but I am getting an ECONNREFUSED error:
connect ECONNREFUSED 127.0.0.1:3306 +2ms
backend_1 | Error: connect ECONNREFUSED 127.0.0.1:3306
backend_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
backend_1 | --------------------
backend_1 | at Protocol._enqueue (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:144:48)
backend_1 | at Protocol.handshake (/usr/src/app/node_modules/mysql/lib/protocol/Protocol.js:51:23)
backend_1 | at PoolConnection.connect (/usr/src/app/node_modules/mysql/lib/Connection.js:116:18)
backend_1 | at Pool.getConnection (/usr/src/app/node_modules/mysql/lib/Pool.js:48:16)
backend_1 | at /usr/src/app/node_modules/typeorm/driver/mysql/MysqlDriver.js:793:18
backend_1 | at new Promise (<anonymous>)
backend_1 | at MysqlDriver.createPool (/usr/src/app/node_modules/typeorm/driver/mysql/MysqlDriver.js:790:16)
backend_1 | at MysqlDriver.<anonymous> (/usr/src/app/node_modules/typeorm/driver/mysql/MysqlDriver.js:278:51)
backend_1 | at step (/usr/src/app/node_modules/typeorm/node_modules/tslib/tslib.js:141:27)
backend_1 | at Object.next (/usr/src/app/node_modules/typeorm/node_modules/tslib/tslib.js:122:57)
I have created the following Dockerfile to configure the NestJS API container:
FROM node:12-alpine
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
EXPOSE 3000
#CMD ["npm", "start"]
CMD /wait-for-it.sh db:3306 -- npm start
COPY . .
And then I reference this from Docker Compose with the following docker-compose.yml:
version: "3.8"
networks:
app-tier:
driver: bridge
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
expose:
- "3306"
ports:
- "3306:3306"
networks:
- app-tier
environment:
MYSQL_DATABASE: school
MYSQL_ALLOW_EMPTY_PASSWORD: ok
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbuser
MYSQL_ROOT_HOST: '%'
backend:
depends_on:
- db
build: .
ports:
- "3000:3000"
networks:
- app-tier
Finally, I set the TypeORM configuration to match with the Docker Compose file:
export const DB_CONFIG: TypeOrmModuleOptions = {
type: 'mysql',
host: 'db',
port: 3306,
username: 'dbuser',
password: 'dbuser',
database: 'school',
entities: [], // We specify the entities in the App Module.
synchronize: true,
};
I am kind of new to Docker Compose, but I have tried many things like changing the output port to 3307, setting an explicit network... and the port 3306 is free in my host OS when I run it. Any help?
Edit 1
I have included MYSQL_ROOT_HOST and wait-for-it.sh as suggested, but still no results.
I think your db is taking more time to start and your app is starting before the db, try something like this for your docker-compose.yml file:
version: "3.8"
networks:
app-tier:
driver: bridge
services:
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
expose:
- "3306"
ports:
- "3306:3306"
networks:
- app-tier
environment:
MYSQL_DATABASE: school
MYSQL_ALLOW_EMPTY_PASSWORD: ok
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbuser
MYSQL_ROOT_HOST: '%'
backend:
depends_on:
- db
build: .
command: bash -c 'while !</dev/tcp/db/3306; do sleep 1; done; npm start'
ports:
- "3000:3000"
networks:
- app-tier
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}`)"
When running queries in my Docker I get this error, for example
php artisan clear-compiled
In Connection.php line 664:
SQLSTATE[HY000] [2002] Connection timed out (SQL: select `id`, `name` from
`users` where `profile_is_public` = 1 and `status` = 1 order by `created_at
` desc limit 6)
What are common cases for the connection timed out error? What could be wrong with my config? Maybe it could be something with mysql socket? How to find out?
I think I entered correct user and password.
docker-compose.yml
version: '3'
services:
web:
build: ./webserver
ports:
- "80:80"
- "443:443"
volumes:
- //docker/dockertest/webserver/app:/var/www/vhosts/app
links:
- db:db
command:
- /usr/local/bin/apache2_install_composer_dependencies.sh
db:
image: mysql:8.0
container_name: db
ports:
- "3306:3306"
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: myDb
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
- //docker/dockertest/install/db_dump:/docker-entrypoint-initdb.d
- persistent:/var/lib/mysql
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
links:
- db:db
ports:
- 8000:80
environment:
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
volumes:
persistent:
Laravel .env
DB_CONNECTION=mysql
#host points to Docker container
DB_HOST=db
DB_PORT=3306
DB_DATABASE=myDb
DB_USERNAME=root
DB_PASSWORD=test
Update
Simple PHP Script like this works, but Laravel does not (still same error)
$servername = "db"; $username = "root"; $password = "test"; $dbname = "myDb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM Person";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
}
$conn->close();
Turned out I had wrong env variable name in config/database.php for DB_HOST (it was like 'host'=> env('DB_HOST_1', 'some ip'))... Did not notice at first. Now it works