如何通过Digitalocean Droplet将Node Express Server-Docker-image连接到MySQL Docker映像?
我有一个实验应用程序,可以在其中使用图像。目的是制作一个完整的工作应用程序,其中包括我在本地机器上设法进行的Docker容器和图像。即时通讯的问题是将其部署到Digitalocean时。
从 node-express-server im获得此错误响应:
{
"status": "error",
"statusCode": 500,
"message": {
"errno": "ENOTFOUND",
"code": "ENOTFOUND",
"syscall": "getaddrinfo",
"hostname": "dbcontainer",
"fatal": true
}
}
我使用docker-compose文件:
services:
mysql:
build:
context: ./DbScripts
dockerfile: ./Dockerfile.prod
command:
- "--default-authentication-plugin=mysql_native_password"
container_name: dbcontainer
cap_add:
- SYS_NICE
ports:
- 3307:3306
restart: always
networks:
- mynetwork
environment:
MYSQL_DATABASE: todo
MYSQL_USER: produser
MYSQL_ROOT_PASSWORD: Password!
MYSQL_PASSWORD: Password!
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 1s
retries: 120
phpmyadmin:
# build: .
image: phpmyadmin/phpmyadmin
container_name: dev_pma
networks:
- mynetwork
environment:
PMA_HOST: dbcontainer
PMA_PORT: 3307
PMA_ARBITRARY: 1
restart: always
ports:
- 8183:80
server:
container_name: server
build:
context: ./BE
dockerfile: ./Dockerfile.prod
networks:
- mynetwork
ports:
- 4000:4000
client:
container_name: FE
build:
context: ./FE
dockerfile: ./Dockerfile.prod
networks:
- mynetwork
ports:
- 3000:3000
environment:
- CHOKIDAR_USEPOLLING=true
tty: true
volumes:
db_data:
networks:
mynetwork:
我还使用docker-compose内部的dockerfiles。相关的是:
mysql dockerfile:
FROM mysql
ENV MYSQL_ROOT_PASSWORD Dsb2016!
ADD /DumpTodoOnly.sql /docker-entrypoint-initdb.d
EXPOSE 3306
上面im使用sql文件创建数据库和用户:
CREATE USER 'produser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password!';
GRANT ALL PRIVILEGES ON *.* TO 'produser'@'localhost' WITH GRANT OPTION;
CREATE DATABASE IF NOT EXISTS `todo` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `todo`;
-- MySQL dump 10.13 Distrib 8.0.18, for Win64 (x86_64)
--
-- Host: localhost Database: todo
-- ------------------------------------------------------
-- Server version 8.0.18
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `todos`
--
DROP TABLE IF EXISTS `todos`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `todos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`todotext` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `users` (
`Id` varchar(36) NOT NULL,
`Email` varchar(200) NOT NULL,
`Password` binary(60) NOT NULL,
`ChangedPassword` bit(1) NOT NULL,
`FirstName` varchar(45) NOT NULL,
`LastName` varchar(45) NOT NULL,
`ImageId` varchar(36) DEFAULT NULL,
`SeqRef` varchar(36) DEFAULT NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `Email_UNIQUE` (`Email`),
UNIQUE KEY `Id_UNIQUE` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci KEY_BLOCK_SIZE=2;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping routines for database 'todo'
--
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2022-04-12 0:18:13
我的节点express中的连接:
const connector = mysql.createPool({
connectionLimit: 20,
host: "dbcontainer",
database: "todo",
user: "produser",
password: "Password!",
});
images
i run docker -compose -d docker -compose -d -d -build < /code>并得到此:
我将服务器,sql和php的imageID复制,对于每个我标记了它:
docker tag imageId ghcr.io/userName/imageName:1
并将其推到github packages:
docker push ghcr.io/userName/imageName:1
digitalocean
我连接到我的github帐户,然后从github包装中取出图像。
我首先运行MySQL映像,然后进入容器,以查看我的数据库和用户是否创建了我可以验证它们具有的,因为TODO db和用户“ produser”都是在digitalocean上创建的:
--------------------
| Database |
--------------------
| information_schema |
| mysql |
| performance_schema |
| sys |
| todo |
--------------------
------------------
| user |
------------------
| root |
| mysql.infoschema |
| mysql.session |
| mysql.sys |
| produser |
| root |
------------------
digitalocean上的容器看起来像这是:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e8fc8145a30c ghcr.io/username/phpnewest:1 "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:8183->80/tcp, :::8183->80/tcp agitated_antonelli
dab65fd84ddb ghcr.io/username/serverlatest:1 "docker-entrypoint.s…" 11 minutes ago Up 11 minutes 0.0.0.0:4000->4000/tcp, :::4000->4000/tcp unruffled_goldberg
cc3fdf91e7ab dbcontainer "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp friendly_ardinghelli
同样,一切都在我的计算机上起作用,但是当推动Digitalocean时,我在上方和Phpadmin上获得了服务器错误:
mysqli :: real_connect():php_network_getaddresses:getAddrinfo失败: 名称或服务不知道mysqli :: real_connect():( hy000/2002): php_network_getaddresses:getAddrinfo失败:姓名或服务不 已知
我在哪里出了问题?我是否错过了服务器上某个地方的配置? 我在Ubuntu上与Docker 19.03.12一起运行一滴液滴。
我是新手的Docker-Compose,正如我所说的,我正在构建它,然后将图像分开标记并推动它们,这是正确的吗?任何指导都将不胜感激。
编辑:
不知道这有多相关,但是当我在DBContainer内部时,我检查了一下,以查看hon主机运行了什么,这是“ Localhost”。
使用以下命令为节点示例做同样的操作:
docker exec -it serverid /bash/sh
然后hostName
我刚刚恢复了容器ID。
EDIT2:
也许要对此明确说明: 我仅在本地计算机(我的PC)上运行Docker-Compose。然后,我将每个图像分开拍摄,然后尝试将它们连接到Digitaloceean上的液滴上。
我还试图将我的Docker-Compose更改为:
version: "3.7"
networks:
mynet:
volumes:
mysqldb_data:
phpmyadmin_data:
services:
mysql:
build:
context: ./DbScripts
dockerfile: ./Dockerfile.prod
command:
- "--default-authentication-plugin=mysql_native_password"
environment:
MYSQL_DATABASE: todo
MYSQL_USER: produser
MYSQL_ROOT_PASSWORD: Password!
MYSQL_PASSWORD: Password!
volumes:
- ./DbScripts/DumpTodoOnly.sql:/docker-entrypoint-initdb.d/DumpTodoOnly.sql
- mysqldb_data:/var/lib/mysql
networks:
- mynet
phpmyadmin:
image: phpmyadmin/phpmyadmin:fpm-alpine
environment:
PMA_HOST: mysql
volumes:
- phpmyadmin_data:/var/www/html/
networks:
- mynet
depends_on:
- mysql
ports:
- 8183:80
nginx:
image: nginx:1.17.4-alpine
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf:ro
- phpmyadmin_data:/var/www/html/:ro
ports:
- "90:80"
networks:
- mynet
depends_on:
- mysql
- phpmyadmin
可悲的是,这种Docker-Compose尝试导致PHP Admin无法在Localhost和Proplet上进行连接的地方工作。 数据库和用户仍然在服务器和本地计算机上创建。 NGINX没有在本地环境上运行,而是在液滴上运行。不知道在这种情况下应该做什么。
对于任何尝试ANSEWR的人,请给我一个逐步的指南。
目前,我正在做以下操作:
- 在本地环境上运行docker-compose
- 将每个单独的图像推向github软件包
- 连接到digitalocean,
- 请拉动图像
- 在特定端口上运行的每个图像。
我的过程必须显而易见,因为它不起作用。
I have an experimental app where I am working with images. The goal is to make a full working app with docker containers and images which I have managed to do on my local machine. The issue im having is when im deploying it to digitalocean.
from the node-express-server im getting this error response:
{
"status": "error",
"statusCode": 500,
"message": {
"errno": "ENOTFOUND",
"code": "ENOTFOUND",
"syscall": "getaddrinfo",
"hostname": "dbcontainer",
"fatal": true
}
}
Im using docker-compose file:
services:
mysql:
build:
context: ./DbScripts
dockerfile: ./Dockerfile.prod
command:
- "--default-authentication-plugin=mysql_native_password"
container_name: dbcontainer
cap_add:
- SYS_NICE
ports:
- 3307:3306
restart: always
networks:
- mynetwork
environment:
MYSQL_DATABASE: todo
MYSQL_USER: produser
MYSQL_ROOT_PASSWORD: Password!
MYSQL_PASSWORD: Password!
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 1s
retries: 120
phpmyadmin:
# build: .
image: phpmyadmin/phpmyadmin
container_name: dev_pma
networks:
- mynetwork
environment:
PMA_HOST: dbcontainer
PMA_PORT: 3307
PMA_ARBITRARY: 1
restart: always
ports:
- 8183:80
server:
container_name: server
build:
context: ./BE
dockerfile: ./Dockerfile.prod
networks:
- mynetwork
ports:
- 4000:4000
client:
container_name: FE
build:
context: ./FE
dockerfile: ./Dockerfile.prod
networks:
- mynetwork
ports:
- 3000:3000
environment:
- CHOKIDAR_USEPOLLING=true
tty: true
volumes:
db_data:
networks:
mynetwork:
Im also using dockerfiles inside of the docker-compose. The releveant ones are:
mysql Dockerfile:
FROM mysql
ENV MYSQL_ROOT_PASSWORD Dsb2016!
ADD /DumpTodoOnly.sql /docker-entrypoint-initdb.d
EXPOSE 3306
Above im using a sql file to create database and user:
CREATE USER 'produser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password!';
GRANT ALL PRIVILEGES ON *.* TO 'produser'@'localhost' WITH GRANT OPTION;
CREATE DATABASE IF NOT EXISTS `todo` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
USE `todo`;
-- MySQL dump 10.13 Distrib 8.0.18, for Win64 (x86_64)
--
-- Host: localhost Database: todo
-- ------------------------------------------------------
-- Server version 8.0.18
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `todos`
--
DROP TABLE IF EXISTS `todos`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `todos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`todotext` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `users` (
`Id` varchar(36) NOT NULL,
`Email` varchar(200) NOT NULL,
`Password` binary(60) NOT NULL,
`ChangedPassword` bit(1) NOT NULL,
`FirstName` varchar(45) NOT NULL,
`LastName` varchar(45) NOT NULL,
`ImageId` varchar(36) DEFAULT NULL,
`SeqRef` varchar(36) DEFAULT NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `Email_UNIQUE` (`Email`),
UNIQUE KEY `Id_UNIQUE` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci KEY_BLOCK_SIZE=2;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping routines for database 'todo'
--
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2022-04-12 0:18:13
The connection in my node express:
const connector = mysql.createPool({
connectionLimit: 20,
host: "dbcontainer",
database: "todo",
user: "produser",
password: "Password!",
});
Images
I run docker-compose up -d --build
and get this:
I copy the imageId of the server, sql and php and for each of these I tag it :
docker tag imageId ghcr.io/userName/imageName:1
And push it to github packages:
docker push ghcr.io/userName/imageName:1
DigitalOcean
I connect to my github account and pull the images from github packages.
I run mysql image first and after doing that I enter the container to see if my database and user got created which I can verify that they have since both the Todo db and user "produser" are created on digitalOcean:
--------------------
| Database |
--------------------
| information_schema |
| mysql |
| performance_schema |
| sys |
| todo |
--------------------
------------------
| user |
------------------
| root |
| mysql.infoschema |
| mysql.session |
| mysql.sys |
| produser |
| root |
------------------
The containers on digitalocean look like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e8fc8145a30c ghcr.io/username/phpnewest:1 "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 0.0.0.0:8183->80/tcp, :::8183->80/tcp agitated_antonelli
dab65fd84ddb ghcr.io/username/serverlatest:1 "docker-entrypoint.s…" 11 minutes ago Up 11 minutes 0.0.0.0:4000->4000/tcp, :::4000->4000/tcp unruffled_goldberg
cc3fdf91e7ab dbcontainer "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp, :::3307->3306/tcp friendly_ardinghelli
Again everything worked on my machine but when pushing to digitalocean I get the server error above and on the phpAdmin I get:
mysqli::real_connect(): php_network_getaddresses: getaddrinfo failed:
Name or service not known mysqli::real_connect(): (HY000/2002):
php_network_getaddresses: getaddrinfo failed: Name or service not
known
Where have I gone wrong? Have I missed a configuration somewhere on the server?
Im running a droplet with Docker 19.03.12 on ubuntu.
Im new to docker-compose and as I said im building it and then tagging the images seperatly and pushing them, is this correct? Any guidence is appreciated.
EDIT:
Dont know how relevant this is but when I was inside of the dbcontainer I checked to see hon what host the mysql was running and it was "localhost".
Doing the same for the node-express with the following command:
docker exec -it serverid /bash/sh
And then hostname
I just got back the container id.
EDIT2:
Maybe to ultral clear about this:
I run docker-compose on my local machine(my pc) only. Then I take each image seperatly and try to connect them on a droplet on digitalOceean.
I have also tried to change my docker-compose to this:
version: "3.7"
networks:
mynet:
volumes:
mysqldb_data:
phpmyadmin_data:
services:
mysql:
build:
context: ./DbScripts
dockerfile: ./Dockerfile.prod
command:
- "--default-authentication-plugin=mysql_native_password"
environment:
MYSQL_DATABASE: todo
MYSQL_USER: produser
MYSQL_ROOT_PASSWORD: Password!
MYSQL_PASSWORD: Password!
volumes:
- ./DbScripts/DumpTodoOnly.sql:/docker-entrypoint-initdb.d/DumpTodoOnly.sql
- mysqldb_data:/var/lib/mysql
networks:
- mynet
phpmyadmin:
image: phpmyadmin/phpmyadmin:fpm-alpine
environment:
PMA_HOST: mysql
volumes:
- phpmyadmin_data:/var/www/html/
networks:
- mynet
depends_on:
- mysql
ports:
- 8183:80
nginx:
image: nginx:1.17.4-alpine
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf:ro
- phpmyadmin_data:/var/www/html/:ro
ports:
- "90:80"
networks:
- mynet
depends_on:
- mysql
- phpmyadmin
Sadly this docker-compose attempt resulted in the php admin not working where I get connection_refused both on localhost and on the droplet.
the datbase and user still get created on the server and local machine.
The nginx did not run on local enviorment but did run on the droplet. Not sure what it should do in this scenario though.
For anyone attempting an ansewr please give me a step by step guide.
Right now I am doing the following:
- Run docker-compose on local enviorment
- Push each individual image to github packages
- Connect to digitalOcean
- Pull the images
- Run each image on specific port.
My process must be obviosly faulty since it does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过2周的尝试,我设法进行了旋转。
核心问题是我不太了解Docker-Compose的使用。我的直觉想法是,它创建的图像可以用作独立,然后它们会自动链接。
解决方案很简单,
我使用此Docker-Compose:
我使用
Docker Image LS
我标记这些图像并将其推入我的GitHub Repo。
现在,我创建了一个新的docker-compose文件,用于生产文件,该文件指的是我完成的图像:
现在,这基本上是魔术发生的地方:
一旦进入digitalocean液滴内部,我创建了一个docker-compose文件,由
nano docker.compose.pose.yml < /code>
我在内部和液滴中添加了生产文件的内容,我运行
docker-compose
现在,我无法说出此刻的最佳状态,而不是您不应该显示您的设想变量直接在docker-compose文件中,但起作用。
After 2 weeks of trying I managed to get this spinning.
The core problem was that I did not quite understand the use of docker-compose. My intial thought was that the images it created could just be used as standalone and then they would autmactially link.
The solution is quite simple
I use this docker-compose:
I list my images with
docker image ls
I tag these images and push them to my github repo.
Now I create a new docker-compose file for production which refers to my finished images:
Now this is basically where the magic happened:
Once inside of DigitalOcean droplet I created a docker-compose file by
nano docker.compose.yml
I add the contents of the production file inside and on the droplet I run
docker-compose up
Now I can not say how optimal all of this is at this moment more than you should not show your enviorment variables directly in the docker-compose file but It works.