Need MySQL and phpMyAdmin in Docker Image (Discord Bot (JS))

Holla,
I am new to Docker and I have spent two days on it, but I still haven’t found how to solve my problem.

I have a TrueNAS server at home and I want to host my Discord bot (JS) on it.

I have specified on my TrueNAS the repo image that is requested, but when I launch the application on the TrueNAS, the bot connects to Discord without problem, except that my MySQL and my phpMyAdmin are not in the repo.

So I don’t know how to do it.
My bot needs a database, so I have to make sure that I have in the repo image, my bot, my MySQL and my phpMyAdmin.

How to do it please?
Thanks.

dockerfile:

FROM node:18-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
RUN npm install && npm cache clean --force
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]

docker-composer:

version: "3.9"

services:
  bot:
    build: .
    container_name: costa-rosta-bot
    restart: always
    environment:
      - NODE_ENV=production
    depends_on:
      - mysql
    networks:
      - costa-rosta-network
    volumes:
      - .:/usr/src/app
    command: ["node", "index.js"]

  mysql:
    image: mysql:8.0
    container_name: costa-rosta-mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: costa_rosta
    networks:
      - costa-rosta-network
    ports:
      - "3306:3306"
    volumes:
      - mysql-data:/var/lib/mysql
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: costa-rosta-phpmyadmin
    restart: always
    environment:
      PMA_HOST: mysql
      PMA_USER: root
      PMA_PASSWORD: root_password
    ports:
      - "8080:80"
    networks:
      - costa-rosta-network
    depends_on:
      - mysql

networks:
  costa-rosta-network:
    driver: bridge

volumes:
  mysql-data:
    driver: local

No. Docker is about isolation. Every application in its own container. That way you can better control dependencies and security and upgrade them individually.

So you spin up you application and DB in separate containers. Best practice is to connect them via a Docker network, access each other via the service name. Only the frontend publishes ports to the outside world.

Hello,
Ok, so if I understand correctly, I have to make 3 different images then?

One of my bot, one of my MySQL and one of my phpMyAdmin?
I then create 3 different applications on my TrueNAS?

Yes, you should run 3 different containers, but only build your own image for your application.

Holla,
Ok, great, that’s what I did, but now I’m facing another problem.

If I launch my bot as well as my MySQL, everything goes well, the connection is OK, everything is fine.
Except that managing a database with MySQL commands in a shell is a bit complex.

So I added in addition, a new image “costa-rosta-pma-prod” (phpMyAdmin) and when I launch it, the bot loses the connection with MySQL.
It’s like there is a conflict with MySQL and phpMyAdmin.
So, when I go to the phpMyAdmin panel, I get this error:
I will put a lot of images for understanding, but I don’t see where the problem is with the phpMyAdmin panel error.

MySQL said:

Cannot connect: invalid settings.

mysqli::real_connect(): php_network_getaddresses: getaddrinfo for mysql failed: Name or service not known

mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo for mysql failed: Name or service not known

phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server.

Here is what I did

Hope you use hub.docker.com/_/phpmyadmin.

There is no reasonable way that the new container can disrupt the running database container. Except when your containers have the same IP.

Which shouldn’t happen, as Docker usually takes care of assigning different IPs automatically within the Docker network.

Thanks for the info, yes I use it.
But I still have trouble understanding!
So I should have only 2 images? My bot and the phpMyAdmin one?

Does the phpMyAdmin one contain MySQL?

Why would it? Have you clicked on the link @bluepuma77 shared? All he wanted to say that running a new container will not break the connection to another unless the new container has the same IP on the same network. I would add that the same service name or network alias can break it too , but your error message is about “name or service not known” so it is not that the domain name points to multiple ip addresses, but it points to nothing. So there is some information missing or it is something that is related to the NAS, not Docker.

I also don’t understand your description of the issue. It could be that I haven’t read it carefully enough, but

But then you shared a PHPMyAdmin error message. Or is the beginning of your quoted error message is from the bot container and only the end of it is from PHPMyAdmin?

What does the screenshot shows? Is it a TrueNAS admin interface or something like Portainer?

Please, don’t share screenshots of code. Harder to read, harder to compare, impossible to quote from by selecting the text and many of the users won’t even click on an picture, not to mentionclicking on an incorrect link and manually replacing part of the URL.

You did great in your first post with the code block. Keep that good habit :slight_smile:

Hella!
Yes, I understand all my apologies.
Otherwise, to return to the problem, I have just deleted everything related to Docker, no more repository, no more image, nothing, I will start from scratch and redo.

How do I do it?
I also redid my folder architecture as in the image.
So I have a BOT folder with all my files to make it work.

Then, I have an empty folder to host MySQL (An image)
After, a phpMyAdmin folder, here also to host it (An image)

So if I understand correctly, I do in MySQL > Dockerfile and docker-compose.
Likewise in phpMyAdmin, Dockerfile and docker-compose?

Below, my config file for the connection to the database.

const mysql = require('mysql2');
const retryTimeout = 5000;

// Connecting to TrueNAS Server
function connectToDatabase() {
  const db = mysql.createPool({
    host: '???',
    user: '???',
    password: '???',
    database: 'costa-rosta-db',
  });

  db.getConnection((err, connection) => {
    if (err) {
      console.error("❌ Error connecting to MySQL database: ", err);
      console.log(`Retrying in ${retryTimeout / 1000} seconds...`);
      setTimeout(connectToDatabase, retryTimeout);
    } else {
      console.log('✅ Connected to the MySQL database!');
      connection.release();
    }
  });
}

connectToDatabase();

So with all this I want to create, if I understood correctly, 3 images which will therefore be:

maybeange/costa-rosta-bot-prod
maybeange/costa-rosta-mysql-prod
maybeange/costa-rosta-pma-prod

The three will therefore be placed on my TrueNAS server.

Wording: You want to create 3 containers, which are based on images. Your own application image you need to build with a Dockerfile.

Share your docker-compose.yml. This is where IP addresses can be messed up.

And it is not possible to do the reverse like locally so a container with three images inside?

version: '3.9'

services:
  app:
    build:
      context: ./bot
    container_name: costa-rosta-app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: root_password
      MYSQL_DATABASE: costa-rosta-db
    depends_on:
      - mysql
    ports:
      - "8080:8080"

  mysql:
    image: mysql:8.0
    container_name: costa-rosta-mysql
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: costa-rosta-db
    volumes:
      - mysql-data:/var/lib/mysql
    ports:
      - "4306:4306"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    environment:
      PMA_HOST: mysql
      PMA_USER: root
      PMA_PASSWORD: root_password
    ports:
      - "8081:80"
    depends_on:
      - mysql

volumes:
  mysql-data:

Like that, locally it works like flash mcqueen, but as soon as I put everything on the server, nothing works anymore, but I think I’m the problem!
Here, I have a container with my three services? Am I crazy?
I can’t do the same thing on the server?

You have 3 containers.

They are launched from a single compose file. So you can see the combined log output of the containers via the compose project.

is a bit vague as error description.

As you use TrueNAS and they have their own container management, maybe you should check how Docker and compose works there.

Mhm, I figured out the problem, I can do the same problem locally.
Actually, when I run my MySQL image, it will be put in a container.
When I run my phpMyAdmin image, it will also be put in a container.
Finally, when I run my bot image, it is also put in a container.

The problem here is that the three services are in 3 different containers and I need to make them communicate with each other.
How to do this?

In the image below, I have launched the three services manually directly from the “Images” tab of Docker to launch them in 3 different containers.
Now, how do I make them communicate with each other when they are launched like this?

They should communicate via a Docker network.

When you launch the containers in the same compose file, they will be implicitly using a common Docker network.

If you launch them independently, you probably need to create a Docker network up front and attach them.

Docker doc is your friend.

I see you don’t really know what a container or image is and what the basic container and Docker cooncepts are. Let me share my template for learning the basics


Recommended links to learn the basics and concepts:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.