Docker Mysql Lost Connection to Mysql Server at 'reading Initial Communication Packet
Docker -> Building a LAMP stack
This is a walkthrough on how to build a LAMP stack in Docker. This was a fun enquiry project to see how to create a dev box for my piece of work using Docker and Docker Compose ( Docker's tool for running an application that requires multiple containers).
Think of docker containers as tiny servers that run only one specific thing. Unremarkably in a server, you lot would have i instance with a bunch of programs (Apache, PHP, MySQL) installed and running. In Docker, each one of these programs are separated out to their own tiny server that shares resources with the other containers on their host computer/server. In this instance, we would take 3 containers
- Apache
- PHP
- MySQL
Yet for this example, I combined the Apache/PHP programs together. From what I take seen, at that place isn't a "best practices" style of docker and you can build things a zillion unlike means. So it's really upwardly to y'all on how you want to build your containers (tiny servers). Proceed in mind that the smaller your container is the ameliorate. And then if you have some programs that yous aren't sure should exist together, follow my little maxim:
When in doubt, split them out.
The DockerFiles
DockerFiles are files that you lot can build a docker paradigm. Retrieve of this file as a script that you would run to set up up your environment on a VirtualBox later on installing the Host OS (Ubuntu,CentOS, and then forth) and then exporting the appliance for others to use. Docker Images are essentially the same concept. You are creating an image for your tiny server aka container.
DevBox.DockerFile
FROM Control
The FROM command tells the docker file, which images to utilise as it'due south base of operations image. You don't unremarkably commencement from complete scratch. I'm sure there are times y'all can, just they are probably pretty rare circumstances.
In file above, you lot beginning with a base image to work from. In this instance, we are building off of the latest version of ubuntu.
The syntax for images in docker
repository_name:tag_name
In this case its ubuntu:latest. The latest tag is used in about images to grab the most recent version of the image.
ENV Command
ENV DEBIAN_FRONTEND=noninteractive
The line above is allowing yous to set an surroundings variable within your new images. This would exist the same as setting an environment variable while SSH'ed into a server. In this instance, this makes it so that when we are installing other software and there is a prompt that requires user input, that it will be bypassed and not impeded the software's installation. I was having my prototype build neglect on a php extension that needed some input. This immune me to bypass that.
RUN Command
The RUN commands are as if y'all are typing in a command in your terminal window. In this file, I was installing the php core and information technology'due south corresponding extensions.
COPY Command
The COPY command copies a file from a local directory on your computer to the container's file organization. Call up of information technology like a scp command on a normal server.
Syntax of Copy Command
/path/to/local/file.txt /path/to/container/filepath/file.txt
CMD Control
The main purpose of a CMD is to provide defaults for an executing container. In this case, we want to launch apache and run it in the foreground then the container stays persistent and active. Dissimilar a normal server that will run 24/vii until manually powered off, a container will automatically plow itself in one case it'south finished running. By running Apache in the foreground it keeps the container from powering downward.
EXPOSE Command
The Expose command allows whichever port yous want to open up up INTERNALLY, inside the network of the docker containers, but not open up to the outside network.
MySQL.DockerFile
Building the prototype
Now that we have the DockerFiles ready for use, we can build the images into actual container a few dissimilar means. The manner I will show y'all is by utilizing a CLI tool chosen Docker Etch.
Docker Etch allows y'all to write a YAML file to spin up multiple containers at once so yous aren't writing a ton of docker commands in your final window. You also perform all of this work by writing a bash script with the docker commands, but using Docker Composer makes the script much easier to maintain.
The build control
To run a build using docker-compose (while in the same file directory every bit your docker-composer.yml file)
docker-etch build
without using any other flags, the docker-compose program will look for the default .yml file name of docker-compose.yml. If you proper noun your .yml a differnt name you would use the -f in the command
docker-compose -f /path/to/docker-etch.yml/file build
The docker-compose.yml
Below is a breakdown of what each line of the YAML file does
More than info on Docker Compose File
How to turn on the containers
When you are ready to start your containers (and afterward you lot take built the containers), you can run the control ( in the same file directory as your docker-compose.yml file)
docker-composer up
Like the control above when you build your images, you can as well specify what your docker-compose.yml filename is past using the -f flag in your command
docker-etch -f /path/to/docker-compose.yml/file build
More info on Docker Compose
How to Connect to a MySQL container with PHP
To connect to a MySQL Db from PHP, y'all but use the containers name in your db connectedness
Example Syntax
mysqli_connect("container_name_here", "my_user", "my_password", "my_db");
So if we were using our MySQL container's proper name from above it would wait like this: (Information technology'south the name nosotros set up in our docker-compose.yml file from earlier)
mysqli_connect("devmysql", "my_user", "my_password", "my_db");
Troubleshooting Tips
Connecting to MySQL result
Ran into Lost connection to MySQL server at 'reading initial communication package', system fault: 0
This was acquired by a demark-accost line in /etc/mysql/mysql/mysql.conf.d/mysqld.cnf not allowing connections from anywhere for mysql container. I commented it out and restarted the containers and I was able to access MySQL with my Database customer (Sequel Pro on Mac)
Terminal Thoughts
As along as you installed all your web apps dependencies (php-gyre, php-json, etc) and made sure your Apache is running, and your MySQL container is running besides; you should be able to visit localhost and run into your system running. If you demand to, you tin can always hack your /etc/hosts file to have a domain name bespeak to your localhost (127.0.0.1) Again, but you know what needs to be ready in your environment to get your app to piece of work.
All of the things you would run in concluding to get your web app to run, can exist placed in your DockerFile, that way it can be performed automatically during your build process.
You use the docker-etch.yml file to trigger booting upwardly all of your containers. Think of information technology as a script that turns on all of your servers and sets them upwardly to talk to each other.
I hope this elementary and quick tutorial will get you a simple LAMP stack box running. From here there are tons more complex builds you can do and even pushing your custom image up to public repos for others to you. Check out Docker Hub to see a list of images that people accept posted for yous to use.
Happy DevOp-ing!
Source: https://medium.com/code-kings/docker-building-a-lamp-stack-9503c62d9214
0 Response to "Docker Mysql Lost Connection to Mysql Server at 'reading Initial Communication Packet"
Post a Comment