![]() This was caused by a bind-address line in /etc/mysql/mysql//mysqld.cnf not allowing connections from anywhere for mysql container. Ran into Lost connection to MySQL server at ‘ reading initial communication packet’, system error: 0 So if we were using our MySQL container’s name from above it would look like this: (It’s the name we set in our docker-compose.yml file from earlier) mysqli_connect(" devmysql", "my_user", "my_password", "my_db") Troubleshooting Tips Connecting to MySQL issue To connect to a MySQL Db from PHP, you simply use the containers name in your db connection Example Syntax mysqli_connect("container_name_here", "my_user", "my_password", "my_db") How to Connect to a MySQL container with PHP The Expose command allows whichever port you want to open up INTERNALLY, within the network of the docker containers, but not open to the outside network. By running Apache in the foreground it keeps the container from powering down. Unlike a normal server that will run 24/7 until manually powered off, a container will automatically turn itself once it’s finished running. In this case, we want to launch apache and run it in the foreground so the container stays persistent and active. The main purpose of a CMD is to provide defaults for an executing container. Syntax of Copy Command /path/to/local/file.txt /path/to/container/filepath/file.txt CMD Command Think of it like a scp command on a normal server. The COPY command copies a file from a local directory on your computer to the container’s file system. In this file, I was installing the php core and it’s corresponding extensions. The RUN commands are as if you are typing in a command in your terminal window. I was having my image build fail on a php extension that needed some input. In this case, 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. This would be the same as setting an environment variable while SSH’ed into a server. The line above is allowing you to set an environment variable within your new images. ENV Command ENV DEBIAN_FRONTEND=noninteractive The latest tag is used in most images to grab the most recent version of the image. The syntax for images in docker repository_name:tag_name In this case, we are building off of the latest version of ubuntu. In file above, you start with a base image to work from. I’m sure there are times you can, but they are probably pretty rare circumstances. You don’t usually start from complete scratch. The FROM command tells the docker file, which images to use as it’s base image. You are creating an image for your tiny server aka container. Docker Images are essentially the same concept. Think of this file as a script that you would run to set up your environment on a VirtualBox after installing the Host OS (Ubuntu,CentOS, and so forth) and then exporting the appliance for others to use. The DockerFilesĭockerFiles are files that you can build a docker image. ![]() So if you have some programs that you aren’t sure should be together, follow my little saying: Keep in mind that the smaller your container is the better. So it’s really up to you on how you want to build your containers (tiny servers). From what I have seen, there isn’t a “best practices” way of docker and you can build things a zillion different ways. However for this example, I combined the Apache/PHP programs together. 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. Usually in a server, you would have one instance with a bunch of programs (Apache, PHP, MySQL) installed and running. Think of docker containers as tiny servers that run only one specific thing. This was a fun research project to see how to create a dev box for my work using Docker and Docker Compose ( Docker’s tool for running an application that requires multiple containers). This is a walkthrough on how to build a LAMP stack in Docker.
0 Comments
Leave a Reply. |