Docker automatically changes port in URL - docker

I built two WordPress websites, one bound to port 9000 and one to 8000. They are both connected to the same MySQL database.
When I access the port 8000 it works perfectly and connects to both the website and the database.
On the other side, when I access the port 9000 it just automatically redirects me to the port 8000 and to its website. Any reason why this may happen?
This is the docker-compose file:
Tried to clear the cache and tried it on Firefox (default browser is Chrome) but it still happens.
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
second_wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "9000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}

Go to http://localhost:9000/wp-admin/install.php to install your second instance.
Be aware that the configuration is stored in files (in /var/www/html/) inside the container and will be lost when you stop the container. So you'll have to install it again when you restart.
If you want to save the configuration, you can create a volume and map it to /var/www/html using something like this
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
- wordpress:/var/www/html
and
volumes:
wordpress:

The problem was that both sites used the same DATABASE inside mySQL, the solution was to create different databases in mySQL and assign each website to it.
Then the wordpress sites will take the info from different databases and not route.

Related

How can I get a full Backup from a Docker Container

I'm new to Docker and created myself a Container using a Compose File.
Now I came to a point where I wanted to use my development result in production.
Is there any way to backup the whole content so that I can use it in a production environment ?
The compose file that I used to spin up my Container:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: myRootPassword
MYSQL_DATABASE: wordpress_oxygen
MYSQL_USER: wordpress_oxygen
MYSQL_PASSWORD: myDBPassword
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
Docker compose is not for production. For production try docker swarm.
Also you should add volume for wordpress, see example
https://hub.docker.com/_/wordpress
How backup volumes please see https://docs.docker.com/storage/volumes/#backup-restore-or-migrate-data-volumes

Connect to networked mysql docker container with local client

I have just followed a tutorial on using Docker. I started with installing different containers and when I got to mysql, i installed it by running
docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql
After the container has been created, I have been able to connect to MySQL with Heidi, by using 127.0.0.1, root and 123456 as password.
I eventually moved forward with another tutorial on installing WordPress with docker.
https://www.youtube.com/watch?v=pYhLEV-sRpY
The yaml for setting up the containers is bellow. Everything works just fine, but when it comes to connecting to the database using Heidi, I just do not get what to do - what connection data to use or what to change in the yaml to be able to connect from local machine.
version: '3'
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- '8000:80'
restart: always
volumes: ['./:/var/www/html']
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:
You should expose 3306 port to local machine. Then, you can connect to MySQL by using host=127.0.0.1.
Update the yaml file like this:
version: '3'
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
...

Cannot connect to second docker container - port error

I have two directories each running an identical docker build except for the allocated ports. I cannot connect to one of the containers in my localhost.
After running the docker ps command I see that 80/tcp being prepended to my second recipe-blog container. Below is my yml file, its nothing crazy, just setting up a database running php my admin and have it connect to a WordPress install. I also attached an image of the docker ps command.
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '9090:90'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:5.3.0
ports:
- '9000:90'
restart: always
working_dir: /var/www/html
volumes:
- ./wp-content:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wpsite
networks:
wpsite:
volumes:
db_data: {}
wp-content:
Once again - finalsandbox_ I can connect to fine, I cannot connect to recipe-blog_. The only difference between the two yml files is that for the recipe-blog_ I changed the ports to be 9090:90 instead of 8080:80 and 9000:90 instead of 8000:80.
Thanks in advance.
It works for me, example
version: "3"
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '9090:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: wordpress
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:5.3.0
ports:
- '9000:80'
restart: always
working_dir: /var/www/html
volumes:
- ./wp-content:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wpsite
networks:
wpsite:
volumes:
db_data: {}
wp-content:
Change all the ports assigned by 90 to 80.
docker-compose up -d
with your we-browser favorite, connect to http://127.0.0.1:9000 and http://127.0.0.1:9090

Unable to log into mysql with phpmyadmin

I'm new to the docker world and maybe I'm missing something.
I created the following docker-compose file
version: '3.3'
services:
db:
image: mysql:latest
restart: always
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
ports:
- '8080:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
wp:
depends_on:
- db
image: andreccosta/wordpress-xdebug
volumes:
- ./wp:/var/www/html
ports:
- 8000:80
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
XDEBUG_CONFIG: remote_host=172.17.0.1
networks:
- wpsite
networks:
wpsite:
volumes:
db_data:
When i run it with docker-compose up everything goes well, but when I try to access localhost:8080 (path for phpmyadmin) and try to login i'm not able to connect to mysql.
If i try to enter the mysql container with
docker exec -t -i <conatiner_name> bash
and try to login to mysql i'm able to do it.
I used this same file on a different computer and everything works well.
I'm running docker on ubuntu.
UPDATE
I solved the issue by changing the version of the mysql image from latest to 5.7.27.

Run command after docker-compose up for one image

I've got a docker-compose.yml that looks like this:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql2
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wptest
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
volumes:
- ./site/:/var/www/html/
- ~/playground/certs/:/etc/ssl/certs/
depends_on:
- db
image: wordpress:latest
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
It works and my wordpress installation is fine - except for the fact that there is a self-signed cert in the mix. So when I try to update my wordpress installation or something, it fails (same with plugins).
If I get into the bash shell of the container and run update-ca-certificates it finds my keys and installs them and then I can run updates without issue.
My question is - can I automate that, so it automatically pulls in my certs and runs the command after the container is up while still allowing me to use docker-compose up ?
You can create a simple Dockerfile that pulls from the wordpress image and add a RUN command with whatever you want to do.
FROM wordpress:latest
RUN your-command-here
And then change you docker-compose to use this new image instead of the official wordpress one, probably something like:
(notice the build arguments)
wordpress:
volumes:
- ./site/:/var/www/html/
- ~/playground/certs/:/etc/ssl/certs/
depends_on:
- db
build:
context: .
dockerfile: ./Dockerfile
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress

Resources