Even after i stoped all docker containers and did docker system prune -a Docker is still downloading and executing tutum/php-apache by runing the command docker-compose up, here is my docker-compose.yml:
version: '2'
services:
postgres:
image: camptocamp/postgres
web:
image: nginx
ports:
- "8000:80"
volumes:
- '.:/usr/share/nginx/html
'
And here is runing containers:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3807234b989f camptocamp/postgres "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 5432/tcp ferrybox_postgres_1
551acc487c9d tutum/apache-php "/run.sh" 5 minutes ago Up 5 minutes 80/tcp, 0.0.0.0:32786->8000/tcp ferrybox_web_1
docker system prune -a will remove all unused images , so assuming that you when executing this command - it is possible that tutum/apache-php image was used by the running-container and it was not created/started by the current docker-compose.yml file (may be docker-compose.yml file had this entry before).
docker#default:~$ docker system prune --help
Usage: docker system prune [OPTIONS]
Remove unused data
Options:
-a, --all Remove all unused images not just dangling ones
--filter filter Provide filter values (e.g. 'label=<key>=<value>')
-f, --force Do not prompt for confirmation
--volumes Prune volumes
docker#default:~$
Can you stop the container (use container-id or name) and remove it,
just to ensure if tutum/apache-php container was created+started again when you invoke docker-compose up ?
#>docker rm -f 3807234b989f
#>docker rm -f 551acc487c9d
#>docker system prune -a
#>docker-compose up
#>docker-compose ps
#>docker ps
Let me know if you get the same output as you were getting earlier.
Related
I started a new Laravel 9.x + JsonApi project on docker-compose with 2 containters, 'web' and 'mysql'. Artisan migrations, seeding and db:show works fine. But when I'm trying to load a page from browser or Postman, it returns DB error
`SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mariadb failed: Temporary failure in name resolution
select * from redirects where redirects.id = 1 limit 1 `
The weird part is that my DB host is mysql and 'mariadb' string is never mentioned in my code (except some vendor files), nor in docker files, nor in .env file. I also checked container environment variables - nothing there.
Artisan scripts work fine.
Here are my settings, if it helps. Mariadb was never mentioned, it was mysql from the very beginning.
docker-compose.yml
version: "3.4"
services:
mysql:
image: mysql
volumes:
- mysql_db:/var/lib/mysql
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=***
- MYSQL_DATABASE=url_shortener
web:
build:
dockerfile: Dockerfile.web.dev
context: .
#image: php:alpine
volumes:
- "./:/app"
- "/app/src/vendor"
- web_root_home:/root
working_dir: /app/src/
command: "php artisan serve --host=0.0.0.0 --port=7999"
ports:
- 8000:7999
depends_on:
- mysql
volumes:
mysql_db:
web_root_home:
Dockerfile.web.dev
#FROM php
FROM bitnami/laravel
WORKDIR /app/src/
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN apt update
RUN apt install -y git build-essential vim mc mlocate
RUN pecl install xdebug
COPY ./src /app/src
COPY ./src/composer.json ./
COPY ./src/.env ./
RUN composer install
RUN php artisan key:generate
CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]
.env (DB part)
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=url_shortener
DB_USERNAME=root
DB_PASSWORD=***
app/config/database.php wasn't changed
artisan db:show
root#a3c873713ee1:/app/src# php artisan db:show
MySQL 8 ...................................................................
Database .................................................... url_shortener
Host ................................................................ mysql
Port ................................................................. 3306
Username ............................................................. root
URL .......................................................................
Open Connections ........................................................ 3
Tables .................................................................. 6
Total Size ........................................................ 0.09MiB
Table .......................................................... Size (MiB)
redirects ............................................................ 0.02
failed_jobs .......................................................... 0.02
migrations ........................................................... 0.02
personal_access_tokens ............................................... 0.02
password_resets ...................................................... 0.02
users ................................................................ 0.02
migrations and seeding works fine, I can connect to mysql from the host laptop. All seeds data are there
I tried to clear config cache, didn't help
root#a3c873713ee1:/app/src# php artisan config:clear
INFO Configuration cache cleared successfully.
I also reloaded containers, no luck as well
Stack trace isn't useful error screenshot
I'm out of ideas what to try and where it takes this mariadb host at all.
I could solve the problem by defining the DATABASE_URL=mysql://root:PASSWORD#mysql/url_shortener in my .env file. I still have no idea where it got mariaDB host from.
I can't container to listen on port 8000, even after adding the port mapping on my docker-compose.yml file.
All relevant files can be found here: https://github.com/salvatore-esposito/laravel-dockerized
I ran the following commands: docker-compose exec app php artisan serve and it has run successfully.
Anyway if I go inside the container, curl works as expected, but it doesn't work from the outside. The connection gets refused.
I fetched the ip using docker-machine ip
Please note that I mapped the outside-inside port in my container via docker-compose.yml even if in the repository there si no map.
I tried to copy all files to a built image and launch:
docker run --rm -p 8000:8000 --name laravel salvio/php-laravel php artisan serve
and
docker exec -it laravel bash
Once more time if a run "curl localhost:80" and "curl localhost:8000" the former doesn't work and the latter it does whereas if I take the container's ip via docker inspect name_container and digit curl ip_of_container:8000 nothing.
When using docker-compose exec a command keeps running until it's interactive session is stopped(by using ctrl-c or closing the terminal) because it isn't running as a service. To be able to keep the following command running
docker-compose exec app php artisan serve
you would have to open 2 terminals, 1 with the command and 1 to connect to the container and ping port 8000
If you want to access your container port 8000 you would have to expose the port 8000 in the Dockerfile:
# rest of docker file
# Copy existing application directory permissions
#COPY --chown=www-data:www-data ./code /var/www/html
# Change current user to www-data
#USER www-data
# Expose port 9000 and start php-fpm server
EXPOSE 80
EXPOSE 80000
and map it to your host in docker-compose(file):
app:
build:
context: .
dockerfile: .config/php/Dockerfile
image: salvio/php-composer-dev
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www/html
ports:
- "80:80"
- "8000:8000"
volumes:
- ./code/:/var/www/html
- .config/php/php.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- myproject-network
Please keep in mind php artisan serve binds to localhost:8000. This means this is only reachable within the container. Use
php artisan serve --host 0.0.0.0
to bind to the shared network interface. Checkout the following resources:
https://stackoverflow.com/a/54022753/6310593
How do you dockerize a WebSocket Server?
I have the following docker-compose.yml configuration file:
silex-twig:
image: php:7.1
command: php -S localhost:4000 /app/index.php
volumes:
- .:/app
ports:
- 3000:4000
When I run docker-compose up it downloads the base image but then it hangs here:
Recreating silextwig_silex-twig_1
Attaching to silextwig_silex-twig_1
What am I doing wrong? There is nothing available on port 3000.
I know there are setups with php-fpm + nginx but that seemed complicated for only development.
That is normal. It is attaching to the stdout of the container (for which there is no stdout being logged). At this point, the container is running.
If you want to run in the background you would run with docker-compose up -d instead of just docker-compose up.
The actual HTTP request to port 3000 won't work because PHP is listening only on localhost. You need to modify your command to be php -S 0.0.0.0:4000 /app/index.php so that it is listening on all IP addresses and can accept connections through the Docker NAT.
may be somebody know how to resolve my issue?
If I use my existing app with wp-content and existing db, every time I've get redirected to the localhost:80 port. How to launch it at another port, maybe 8000 for example?
I have a Wordpress app Dockerfile with next lines:
FROM wordpress:latest
COPY ./src /var/www/html
ENV WORDPRESS_DB_PASSWORD mypass
ENV WORDPRESS_DB_NAME mydb
ENV WORDPRESS_DB_HOST mysql:3306
MySQL Dockerfile with existing db dump:
FROM mariadb:10.1.20
COPY dump/dump.sql /docker-entrypoint-initdb.d
ENV MYSQL_ROOT_PASSWORD mypass
CMD ["mysqld"]
And docker-compose.yml with that:
version: '2'
services:
mysql:
build: mysql/
restart: always
volumes:
- db_data:/var/lib/mysql
wordpress:
depends_on:
- mysql
build: wpapp/
ports:
- 8000:80
restart: always
volumes:
db_data:
Thanks for help everyone!
Override the database siteurl and homeurl vales with these lines in you wp-config.php:
define('WP_HOME', 'http://localhost:8000/');
define('WP_SITEURL', 'http://localhost:8000/');
After many tests I can recap how to solve this issue.
We need to change option_value of 2 rows in db table wp_options with option_names siteurl and home to http://localhost:8000 in this case.
We need to trigger dockers build to mysql container with volume.
docker-compose down -v
optional for clean docker cache
docker rm $(docker ps -aq)
docker volume rm $(docker volume ls -q)
docker rmi $(docker images -q)
this is not optional ofcourse
docker-compose up -d --build
The main thing is to clean browser cache manually, because it invokes redirect faster then cache clean if we use CMD + R for example in Safari and it seems like nothing is working after changes.
I have such simple dockerfile for PHP:
# Base image
FROM php:7-fpm
# Update packages list
RUN apt-get --yes update;
# Install SSH server, set root password and allow root login
RUN apt-get --yes install openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:123' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# Run SSH server
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
And such docker-compose.yml file
web:
image: nginx:latest
volumes:
- /c/Users/marcin/dock-test/composers/l1.app/html/:/usr/share/nginx/html/
- /c/Users/marcin/dock-test/composers/l1.app/nginx/conf.d/:/etc/nginx/conf.d/
- /c/Users/marcin/dock-test/composers/l1.app/nginx/log/:/var/log/nginx/
ports:
- "8080:80"
working_dir: /usr/share/nginx/html/
links:
- php
- db
container_name: l1.web
environment:
- VIRTUAL_HOST=l1.app
php:
build: ../builds
dockerfile: Dockerfile-php7-fpm
volumes:
- /c/Users/marcin/dock-test/composers/l1.app/html/:/usr/share/nginx/html/
- /c/Users/marcin/dock-test/composers/l1.app/php/config/:/usr/local/etc/php/
working_dir: /usr/share/nginx/html/
links:
- db
container_name: l1.php
ports:
- "22020:22"
db:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=pass
- MYSQL_DATABASE=
- MYSQL_USER=
- MYSQL_PASSWORD=
expose:
- 3306
volumes:
- /c/Users/marcin/dock-test/composers/l1.app/mysql/data/:/var/lib/mysql/
- /c/Users/marcin/dock-test/composers/l1.app/mysql/conf.d/:/etc/mysql/conf.d/
- /c/Users/marcin/dock-test/composers/l1.app/mysql/log/:/var/log/mysql/
ports:
- "33060:3306"
container_name: l1.db
The problem - everything is working fine until I add in my dockerfile the last shown line:
CMD ["/usr/sbin/sshd", "-D"]
If I add this line, SSH is working fine but I'm getting 404 when displaying the page. When I comment this line, I'm getting page without a problem but obviously this SSH is not working.
What could be the problem with this? I just want to add I need this SSH service in PHP container (and running docker exec in this case is not an option)
The base image php-fpm ends with
CMD ["php-fpm"]
Your own CMD would override that (meaning php .
One workaround would be at least to ADD and call a wrapper script which would:
call php-fpm
launch sshd daemon
But that wouldn't play well with stop/kill signals, which would not stop everything. There are special images for managing more than one main process.
The OP Marcin Nabiałek confirms in the comments below:
I've created such file:
#!/bin/sh
# Start PHP
php-fpm -D
# Start SSH
/usr/sbin/sshd -D
and it seems to be working now without a problem.
A complete answer from #VonC and #Marcin Nabialek.
# php-fpm -D
# /usr/sbin/sshd -D
# use \n to make the content into multiple lines
RUN printf "php-fpm -D\n/usr/sbin/sshd -D" >> /start.sh
RUN chmod +x /start.sh
CMD ["/start.sh"]