简体   繁体   English

有没有办法通过快递将我的 MySQL 数据库 Dockerize 并将该容器托管在数字海洋上……等等?

[英]Is there a way to Dockerize my MySQL DB with express and host that container on digital ocean .. etc?

I want to use Docker to make a container that contains a MySQL DB with Express to expose into certain ports for example 8080 so I can fetch that using React for the view.我想使用 Docker 制作一个包含 MySQL 数据库的容器,该数据库带有 Express 以暴露到某些端口,例如8080 ,因此我可以使用 React 获取该视图。

Right now I made simple server with express that fetches the data from a MySQL database and displays it on 'localhost:8000/posts' (posts is where i am using SQL Query to select everything from the table.现在我用 express 制作了一个简单的服务器,它从 MySQL 数据库中获取数据并将其显示在“localhost:8000/posts”上(帖子是我使用 SQL 查询到 Z99938282F040718592941 中的所有内容的地方。

This is a index.ts file:这是一个 index.ts 文件:

//Main File
import express from "express";
import { connection } from "./db-connect";
const app = express();
const port = 8000;

var cors = require("cors");
/*
cors was installed so I could get the data
and display it to react, otherwise I was getting an error
in the console.
*/

app.use(cors());

app.listen(80, function() {
  console.log("CORS-enabled web server listening on port 80");
});

app.get("/", (req, res) => {
  res.send("Go to /posts to see the Data");
});

app.get("/posts", (req, res) => {
  connection.query("SELECT * FROM blog.posts", (err, result) => {
    if (err) {
      return res.send(err);
    } else {
      return res.json(result);
    }
  });
});

app.listen(port, err => {
  if (err) {
    return console.error(err);
  }
  return console.log(`server is listening on ${port}`);
});

So I have a database named blog with table called posts With this I am getting an array with data as an object which is I wanted.所以我有一个名为blog的数据库,其表名为posts有了这个,我得到了一个数组,其中包含我想要的 object 数据。

What I wanted to learn is Dockerize this into a container.我想学习的是把它Dockerize到一个容器中。 With MySQL that has blog database with posts table that contains id, title, body and user. MySQL 具有blog数据库和包含 id、标题、正文和用户的posts表。

After I'd like to host the Docker that contains the db and deploy it to digitalocean or similar service.在我想托管包含数据库的 Docker 并将其部署到 digitalocean 或类似服务之后。

I'd suggest to use docker-compose for this.我建议为此使用docker-compose

You'd then have a.yml file that looks something like this:然后你会有一个看起来像这样的 .yml 文件:

version: '3.3'
services:
  db:
    image: mysql:5.7
    restart: always
    environment:
    # Use this to connect to your dockerized mysql container
      MYSQL_DATABASE: 'db'
      MYSQL_USER: 'user'
      MYSQL_PASSWORD: 'password'
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      - '3306:3306'
    expose:
      # Use this port in your express app
      - '3306'
    volumes:
      - my-db:/var/lib/mysql

volumes:
  my-db:

Then you need to dockerize your express app and include it in the docker-compose.yml file.然后,您需要对您的 express 应用程序进行 docker 化,并将其包含在docker-compose.yml文件中。 Here is a guide on how to dockerize a node.js app such as yours.是有关如何对 node.js 应用程序(例如您的应用程序)进行 dockerize 的指南。

Once you succeeded to connect the two containers in docker-compose, deploying it on a Digital Ocean server is just as easy as installing Docker-Compose on the server (if it's not already installed), copying your code to the server and running docker-compose up --build .成功连接 docker-compose 中的两个容器后,在 Digital Ocean 服务器上部署它就像在服务器上安装 Docker-Compose(如果尚未安装),将代码复制到服务器并运行ZBAEDB53E845AE21F1397 --build.AE9Z05 up 一样简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM