John Davidson

php - Connecting Jenkins Container to MySQL Container

0 comments
Message:


I am trying to do CI/CD on my Laravel application. In order to do this I have to use Jenkins and one command is php artisan migrate:fresh --seed. That means that I need to use a SQL database in order to achieve this.


I am using Portainer. I have created both a Jenkins image with PHP 8 and Composer in it and also a MySQL image. Both are connected through a network called jenkins-mysql. I created both containers for the images.


I am trying to get access to the MySQL container inside the Jenkins container console. Aka I want to do the migration through the MySQL container inside the Jenkins container.


All I have found on the internet is simply to connect them through a network (I already did) and to install MySQL inside the jenkins container, but I have found problems with it and I much rather go the MySQL container route. I haven't found a way to connect directly to the MySQL container from the Jenkins container console through the network.


As extra context, here are the Dockerfiles for both images:


jenkins:


FROM jenkins/jenkins:lts-jdk11
USER root
RUN apt -y update && apt -y upgrade
RUN apt install -y lsb-release ca-certificates apt-transport-https software-properties-common gnupg2 wget
RUN echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/sury-php.list
RUN wget -qO - https://packages.sury.org/php/apt.gpg | apt-key add -
RUN apt -y update
RUN apt install -y php8.0-cli php8.0-common php8.0-opcache php8.0-readline php8.0 libapache2-mod-php8.0 php-common php8.0-xml php8.0-gd php8.0-curl php8.0-zip unzip
RUN apt -y update
RUN wget -O composer-setup.php https://getcomposer.org/installer
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
EXPOSE 8080:8080/tcp
EXPOSE 9010:80/tcp
EXPOSE 50000:50000/tcp

mysql:


FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE aguas
EXPOSE 3306


My Answer:
RUN apt -y install php8.0 php8.0-mysql php8.0-mbstring php8.0-xml php8.0-zip php8.0-curl php8.0-json php8.0-gd php8.0-bcmath php8.0-intl php8.0-xmlrpc php8.0-soap php8.0-ldap php8.0-imagick php8.0-xdebug composer

mysql:
FROM mysql:latest

ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_DATABASE=mydatabase
ENV MYSQL_USER=myuser
ENV MYSQL_PASSWORD=mypassword

COPY ./database.sql /docker-entrypoint-initdb.d/

You can try using the following steps to connect the Jenkins container to the MySQL container:

1. Check the IP address of the MySQL container by running the following command in the terminal:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'

2. Use the IP address obtained in step 1 to connect to the MySQL container from the Jenkins container. You can do this by running the following command in the Jenkins container console:
mysql -h -u myuser -p mydatabase

3. You can now run the php artisan migrate:fresh --seed command in the Jenkins container console to migrate the database using the MySQL container.

By following these steps, you should be able to connect the Jenkins container to the MySQL container and perform the database migration for your Laravel application.

Rate this post

5 of 5 based on 8365 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us