Access a service from one container to other - php

I'm following a docker web application setup tutorial from here. As described in the docker-compose.yml file the MySQL container is linked to the app and it is accessible via both index.php and phpMyAdmin. No problem so far.
When I opened an interactive shell for my application using docker exec -it container-id bash and tried to access the MySQL service but I couldn't. What I'm I missing here?
Edit1: When I type MySQL, I get bash: mysql: command not found.
Edit2: Output of docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1643f0ba5637 dockertutorial_php "docker-php-entryp..." 9 seconds ago Up 6 seconds 9000/tcp dockertutorial_php_1
610f2c8bf4c4 phpmyadmin/phpmyadmin "/run.sh phpmyadmin" 9 seconds ago Up 6 seconds 0.0.0.0:8080->80/tcp dockertutorial_phpmyadmin_1
29c552da473b mysql:latest "docker-entrypoint..." 10 seconds ago Up 8 seconds 3306/tcp dockertutorial_mysql_1
Edit3: Perhaps I should have started with this. I apologise for all the confusion caused/would cause (Especially to #Jay Blanchard & #mkasberg). I want to run a bash script inside the nginx container which creates a MySQL database. It works if I have installed MySQL in the nginx container but not if it is a separate container. Is it possible to achieve with the current approach? If not how should I correct it?

mysql: command not found means that the mysql client binary is not available in whichever container you're logged in to. I suspect you're running docker exec on a container other than the MySQL one. The binary would be there in the MySQL container. While those other containers can connect do MySQL, they don't have the MySQL command line application available (to keep image size small).
Make sure you're connecting to the MySQL container with your docker exec command and it should work. In fact, docker-compose provides a way for you to do this. With your services running (after doing docker-compose up), try this:
$ docker-compose exec mysql /bin/bash
That should get you to a bash prompt in the mysql container. Before, you were getting to a bash prompt in the nginx container. Which can't run mysql on the command line.

Related

Php script won't run on docker localhost

I am very new to Docker so please forgive any technical wording mistakes I make regarding this.
I am trying to get a very basic "Hello World!" php script to run through a localhost docker container. I am trying to run this script to test out this image that contains php and oracle: https://hub.docker.com/r/silencesys/php-oci8
I pulled it using this command: docker pull silencesys/php-oci8
Then I tried to run it with numerous commands, some of them giving me this:
[16-Jun-2022 17:57:12] NOTICE: fpm is running, pid 1
[16-Jun-2022 17:57:12] NOTICE: ready to handle connections
And then some of them give a random string of letters and numbers. I don't know what it refers to but I know that it means that it is running the container. When I open the container's terminal and run 'ls' I can see my index.php file. I can even run 'cat index.php' and see that it contains the proper code in there to run "Hello World!" I will even include the code in here as a sanity check:
<?php
echo "Hello World!";
?>
But everytime that I open up the localhost in my browser, I get this:
This page isn’t working right now
localhost didn’t send any data.
ERR_EMPTY_RESPONSE
I have tried so many variations of this base docker run command that I found here:
docker run -d -p 80:80 --rm --name temp-container -v "$PWD":/var/www/html silencesys/php-oci8
Any help is greatly appreciated!
The docker image you are referring to does contain PHP, but it does not contain a web server.
Servers that serve web pages using PHP consist of a webserver software, that accepts HTTP-requests and communicates to PHP running in the background if needed. It can be thought of an intermediary between the Browser and PHP, and if it is missing the browser cannot access your PHP script.
I haven't found an image that could help you right away, however I stumbled upon this, and it tells that for your usecase you should go for a PHP image that has Apache included (i.e. the name will contain apache in the tag).
The way I solved this was by using this image in conjunction with the image I was using in the Question. Here is a step by step guide of what I did:
Create folder in home/[username] directory
docker pull soivangoi/nginx-php-adminer-oci8
docker pull silencesys/php-oci8
docker run -dit -p 8080:80 soivangoi/nginx-php-adminer-oci8 -v "$PWD":/var/www/ silencesys/php-oci8
4.1. If that doesn't work try this "docker run -d -it -p 8080:80 -v "$PWD":/var/www/ silencesys/php-oci8"
docker run -it -p 8080:80 --rm --name test-container -v "$PWD":/var/www/ soivangoi/nginx-php-adminer-oci8
Bash into the container. Either by command line or by clicking the terminal button on the container.
cd to where your php code is stored.
Run it!
Just make sure that whatever network you are on has access to the oracle database or else it will not let you run your code still.

Debug Symfony CLI application with PhpStorm inside Docker container

My current setup involve PhpStorm IDE in which I have imported Symfony 3 projects which is basically CLI tool. On the host machine I don't have PHP installed so I'm running the application from Docker container which has PHP and Xdebug installed.
I don't have issues to debug web applications from Docker containers but with Symfony and this CLI tool it seems a little bit more tricky.
My question is how to properly set this up and debug it from PhpStorm? I tried to create a new debug configuration (PHP Remote Debug) but breakpoints are not trigged.
Suppossing you have followed into the instructions mentioned into the following links:
Can't connect PhpStorm with xdebug with Docker
How to setup Docker + PhpStorm + xdebug on Ubuntu 16.04
Or similar questions
Then you need to follow theese steps:
Step1:
Get shell access to your container via running:
docker exec -ti ^container_id^ /bin/sh
Or if running a debian/ubuntu based one (or you installed manually bash):
docker exec -ti ^container_id^ /bin/bash
The ^container_id^ can be found via docker ps command it is the first column of the table. If running on a small window just pipe int into less -S resulting the command:
docker ps | less -S
Then export the following enviromental variables:
export PHP_IDE_CONFIG="serverName=0.0.0.0:5092"
export XDEBUG_CONFIG="idekey=PHPSTORM"
Please keep in mind to setup the correct value specified into Servers section as you see in the image:
It is important in order not to run into the problem specified in this question.
Then just enable debugger listentin into the phpstorm and spawn the cli as you do when you run a symfony application.

Docker + PHP - ERROR: Couldn't connect to Docker daemon?

I'm following this guide to set up a PHP development environment with Docker.
I have created a folder on my desktop docker-php and added a docker-compose.yml file into it, with this content:
nginx:
image: nginx:latest
ports:
- 80:80
On my terminal:
$ cd /home/my-username/Desktop/docker-php/
$ docker-compose up -d
I get this error:
ERROR: Couldn't connect to Docker daemon at
http+docker://localunixsocket - is it running?
If it's at a non-standard location, specify the URL with the
DOCKER_HOST environment variable.
I'm on Xubuntu 16.04.
Or perhaps I should put the folder in the specific location that is required by Docker? If so, which is it?
The most common reason for this error is that you ran 'docker-compose up' without sudo. As long as there is docker installed and is up and running, you are likely missing sudo in the docker command.
You could use native Docker
One option is to abandon docker-machine and use a native Docker setup on your system. Since you are on Linux (Xubuntu), this is an option for you. docker-machine is most often used by people who can't run Docker natively (Mac or Windows), and use it to install a Docker-capable VM and some local commands on their OS to talk to it.
You can find install docs for Docker on Linux here.
However, you already have docker-machine installed, so this may be the most disruptive option for you.
Your docker-machine may not be running
The error you are getting is saying the Docker client cannot talk to the server. One potential reason for this is that your docker-machine VM isn't running. You should verify it is running, and if not, start it.
To get a list of your docker-machines (may be one or more):
docker-machine ls
You will probably have one machine named default, but you may have more, depending on how you did your setup.
You can get the current status with:
docker-machine status <machine-name>
And you can use stop, start, restart to manage the docker-machine.
(More in the Docker Machine CLI reference.)
You need the proper environment set
docker-machine relies on environment variables to work properly. Because you may have multiple docker-machine setups, you have to tell the client which one to use.
To set the environment, you can get it from the docker-machine command.
docker-machine env <machine-name>
And you can automatically inject it into the environment (this may be a useful thing to put into your shell startup file).
eval "$(docker-machine env <machine-name>)"
You should end up with env vars similar to these:
DOCKER_HOST=tcp://192.168.99.101:2376
DOCKER_CERT_PATH=/Users/nathanleclaire/.docker/machines/.client
DOCKER_TLS_VERIFY=1
DOCKER_MACHINE_NAME=dev
Keep in mind you should use the eval form here, not just run the env command and paste the output into your shell setup; it may change on a docker-machine restart, etc, so you can't rely on an old setup to still work later.
If your docker-machine is running, and these env vars are set, your docker and docker-compose commands should work.
Solution:
sudo usermod -a -G docker USERNAME

Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use

I have to make Laravel app and to deliver a Dockerfile, but I'm really stuck with this. Before that I had a nightmare wile installing laravel on my machine.
I'm trying to get dockervel image and following the steps here:
http://www.spiralout.eu/2015/12/dockervel-laravel-development.html
But when I run dartisan make:auth it gives this error below:
**ERROR:** for dockervel_mysql_1 **Cannot restart container** c258b418c03cbd6ec02c349c12cf09403f0eaf42fa9248019af7860d037d6474: **driver failed programming external connectivity on endpoint dockervel_mysql_1** (da3dd576458aa1fe3af7b539c48b9d61d97432cf5e9ee02d78562851f53981ae): E**rror starting userland proxy: listen tcp0.0.0.0:3306: bind: address already in use.**
I have tried to Change the default port in the docker-compose.yml
ports:
- "8084:80"
Still nothing, also tried to stop apache2 (service apache2 stop) on my machine ,also tried docker-compose restart and removing docker container dockervel_mysql_1.
I have to mention that I have already one Laravel proj. in /var/www/laravel.
Please help!
I had the same problem and
sudo netstat -nlpt |grep 3306
showed me the PID and which service it was started by (mysgld). Whenever I tried to kill the PID then it was started again. But the problem was fixed when I stopped the service by
sudo service mysql stop
Notice that you will have to use mysql and not mysqld.
I hope that this will do it for you - I was able to run docker-compose up without any problems
Try to kill all the processes using the port 3306:
sudo kill `sudo lsof -t -i:3306`
Then, run your docker containers:
sudo docker-compose up
Probably you have already a MySQL service running in port 3306. You should close it first.
Then try to end docker-compose down and restart it with docker-compose up.
Remember also to change the permissions after you add a file in your project (like dartisan make:auth) with dpermit
UPDATE:
since you have changed the port to "8084" you should go to localhost:8084
If you see the apache default then you probably are browsing another server since dockervel is build upon nginx.
You have also probably have some gaps on Docker. Don't mix your local storage with docker storage. /var/www in a container is different than your local /var/www. in docker-compose.yml you mount the local ~/dockervel/www to containers /var/www.
I would suggest that you start all over again and revert the changes you've made to your apache server. Shut it down, you don't need it. Dockervel will provide you with an NginX server in a container.
My fix for this issue was to go into
docker-compose.yml
and change
ports: -3306:3306 to ports: -3307:3306
then run this command again:
docker-compose up
On Ubuntu, running this command will stop your mysql from running for your docker container to work:
sudo service mysql stop
Then, if your apache2 is running, you need to stop the service especially when you want to work with nginx:
sudo service apache2 stop
Then, you can run your docker-compose up -d ... command
So for me when I was trying to load and run MySQL image in docker container, I was getting the same error:
And even after stopping local mysql server in system preferences didn't help:
Cause the port 3306 was used by my tomcat server, so basically you have to make sure the port (in this case 3306) that the docker command wants to use should not be in use by any other service otherwise the command will fail
First Solution :
sudo service mysql stop
and then run
docker-compose up
to start the application , It is an quick and fast solution to run this mysql stop command to stop the current MySQL service on port 3306 port so the same port can be available for your docker application.
Scenario 1 : Problem will come when you want to run both application one which was running previously and next you want to run now at this time it won't work.
Second Solution to Scenario 1:
If your next/current application coming from docker then try below it will work without disturbing first application mysql service of 3306 port
Open and changed the MySQL port from docker-compose.yml file
Default configuration
ports:
- ${SERVER_PORT_DB:-3306}:3306
changed port
ports:
- ${SERVER_PORT_DB:-3307}:3306
and now run below command to start the application
docker-composer up
The error you are seeing is from a local mysql instance listening on port 3306 (currently on pid 1370 from your comments). You won't be able to run a container that publishes on this host port while it's already in use by another process. Solutions are to either stop mysql on the local host, or to change/remove the published port in your container. If the port is only needed by other containers, you can leave it unpublished and they can communicate directly on the docker private network (by default, this is "bridge").
This option did it for me:
sudo pkill mysql
You need to change the mysql port
because you are installing mysql on your machine and it takes the default port 3306
and now you are trying to make dockervel_mysql_1 run to the same port 3306 , this is why you see in the error "Address already in use"
so if you change dockervel_mysql_1 port to 3307 for example it will works fine , without stopping the mysql that is running on you machine
Running this command fixed the issue for me:
docker swarm leave --force
Explanation:
I had started docker swarm service as a master node in my localhost.
Swarm was taking network priority and making use of this ports already
If tomcat is running on your machine which is connected to MySQL 3306 port then check by killing the tomcat first and then trying to do docker-compose up.
I used two different versions of MySQL, MySQL 5 on my local machine and 8 on docker. So when your connected to mysql5 on 3306 in tomcat and if you just stop the mysql5, then the process isn't completed yet since tomcat is still connected to 3306. Kill the tomcat and then up the docker it should work.
Happy coding!!
I know this question has been quite old but someone still looking for answers. You don't have to run any kill command instead you can use docker --remove-orphans flag and it will clean it up for you. For example
docker-compose up -d --build --remove-orphans
This worked for me, just changed the app's port to 8084:80, like described here.

MySQL Server crashes and fails to start - Can't connect to local MySQL server through socket '/tmp/mysql.sock

I have been using mySQL for a few weeks now with PHPMyAdmin with no problems. But just lately it stopped working - MySQL server will run for a few seconds when I start my machine (Mac OS X 10.9.5)then it crashes and wont restart. It also wont let me log in PHPMyAdmin.
When i try to run mysql through terminal it returns the following error:
Can't connect to local MySQL server through socket '/tmp/mysql.sock
I'm not really sure what could cause this after it working for so long.
mysql on the command line/terminal is just another MySQL client, like phpMyAdmin or your web application. The error you're seeing is the same error as phpMyAdmin is displaying. On linux/OSX the command is actually mysqld (d for Daemon or server).
Homebrew/Macports/etc installs
During installation it should have installed a launchctl script. Find it on the terminal with ls ~/Library/LaunchAgents/ and inspect the output, in my case:
homebrew.mxcl.lighttpd.plist
homebrew.mxcl.mysql.plist
homebrew.mxcl.php55.plist
There in the middle - now I can use launchctl to start it at the terminal:
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Although the handy launchctl script is called "mysql" it's actually launching mysqld, usually from /usr/local/bin/mysqld
Standard MySQL install
If you installed MySQL directly from their website, you should have a "MySQL" option in your System Preferences. From there you can stop/start the service.
If you didn't install mysqld with a package manager, perhaps you installed/compiled it yourself, then search your disk for mysqld and launch it from the command line or find an init.d folder with a mysqld script inside.

Categories