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

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

Related

Website hosted in docker container is not updating live

I am developping a website which I want to host inside a php-apache docker container.
I use the following command to run the container:
docker run -dit --restart unless-stopped --name my_www -p 8080:80 -v /path/to/repo:/var/www/html/ php:7.4-apache
Since I bind the repository containing the code as a volume to the container, I expect the website to "update live" when I change the code locally. I had this right behaviour last time I tried but I am now unable to get it back.
When I check the website locally at 127.0.0.1 everything is ok and changes are taken into account normaly, but they do not propagate into the docker container...
For some reason, the files in the docker are stuck to an old version of the code, an old "stat" of the repository...
Any ideas how I can manage to fix this and preview changes live ?
Credits to #Don't Panic for helping to debug.
The browser was caching everything so I couldn't see changes live.
The solution was to enable the "expires" apache module inside the docker container:
$ docker exec -it <container_id> bash
root#<container_id>:# a2enmod expires
root#<container_id>:# exit
$ docker restart my_www
Et voilĂ  :)

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.

Using Docker Nginx, PHP, MySQL on Mac

I'm just starting to get my head around docker and want to use it for a project.
I have followed https://docs.docker.com/docker-for-mac/#explore-the-application-and-run-examples and have NGINX running fine and can see the NGINX landing page.
Do I need to install php-fpm and mySQL within my container since my container is only NGINX at this stage?
How do I configure my project on a custom domain e.g. project.dev. Do I need to edit an entry in /etc/hosts for 127.0.0.1 project.dev and then listen for that URL in an NGINX config?
Lastly do I need a dockerfile? I already have my container up and my understanding is a dockerfile is only for defining your container?
An example of a dockerfile for NGINX, PHP and mySQL would be helpful to look at as well.
Thanks
No, this guide just show using nginx container in docker. But I see the container don't have php installed. And you cannot install php-fpm inside this container.
So, if you want to use nginx, php, and MySQL using docker you should pull:
Container which run Nginx + PHP-FPM (I recommend this image https://hub.docker.com/r/richarvey/nginx-php-fpm/)
Container run MySQL (https://hub.docker.com/_/mysql/)
Download images
docker pull richarvey/nginx-php-fpm
docker pull mysql:5.6
Run MySQL Instance. Name it mysql56, and expose using port 3360
docker run -tid -p 3360:3306 --name mysql56 -e MYSQL_ROOT_PASSWORD=123456 -v /root/docker/mysql56/data/mysql:/var/lib/mysql -d mysql:5.6
Run Nginx PHP+FPM instance. Link it to MySQL Instance, and name it project-dev
docker run -tid --name project-dev --link mysql56:mysql -v $(pwd):/var/www/html -p 8888:80 richarvey/nginx-php-fpm:latest
Run docker ps -a to see the running containers.
To make nginx can be accessed with address project.dev, just map it on /etc/hosts. Then access it on web browser http://project.dev:8888
Note:
-v /root/docker/mysql56/data/mysql:/var/lib/mysql it mean I have /root/docker/mysql56/data/mysql on my mac, and map it to /var/lib/mysql in mysql56 container. So all mysql data will be backup on my local data, and will not lose when I remove the container.
-v $(pwd):/var/www/html mean your current directory will be mapped to
container. So, whatever you write in this directory will be exist on
/var/www/html container.
I use port 8888 to avoid conflict with existing web server, you can
change it as you want

set PHP path from host to docker container

i know this is rather a stupid question, but i have the following problem. i use Docker above a year and a editor to change my programm which is hostet as a volume.
i dont have installed php because it only runs inside of the containers, like almost all other of my server programms (like sql, apache). now i installed visual studio code and it cannot find the path to php to use intellisense.
i know that i can set an environment path inside my docker-compose or Dockerfile to set an environment for my container. but the container is, if its run, isolated to the outside, except for commands like docker cp.
is it possible to set a path from my host machine to the container machine, so that visual studio code can find PHP inside of the container and use it for intellisense? or do i have to install php on my host machine? but this would destroy the usage of the Docker containers in my opinion.
for example in visual studio code config settings.json
"php.validate.executablePath": DOCKERCONTAINER/usr/bin/php
The trick is to create a Bash file that calls to our PHP container.
At first, start a PHP7 container and keep it running by using this docker-compose.yml
version: "3"
services:
python:
image: php:7.2
container_name: php7-vscode
restart: always #this option will keep your container always running, auto start after turn on your host machine
stdin_open: true
networks:
- proxy
networks:
proxy:
external: true
Create a file named php in /usr/local/bin
Chmod to make it executable
sudo chmod +x php
This file will contain this script that use our running container to process php
#!/bin/bash
docker exec -i --user=1000:1000 php7-vscode php "$#"
1000:1000 is our user id and our user group on our host machine. We have to run as our current user on host machine so that the container won't modify our file's owner.
That's it. Now you can type
php -v
to see the result.

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.

Categories