Cannot connect to second docker container - port error - docker

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

Related

Open multiple Wordpress projects in the docker

I am creating containers for all my projects, mostly in Wordpress, and I was wondering if for example I could upload several projects at the same time, because in this configuration that I use the docker, I have to disable one, in order to activate another. Example project1.local and project2.local, how could you do it that way?
docker-compose.yml
version: '3.3'
services:
wordpress:
build: .
container_name: ${APP_NAME}-wordpress
restart: always
ports:
- 80:80
environment:
WORDPRESS_DB_HOST: ${DB_HOST}
WORDPRESS_DB_USER: ${DB_USER}
WORDPRESS_DB_PASSWORD: ${DB_PASS}
WORDPRESS_DB_NAME: ${DB_NAME}
WORDPRESS_TABLE_PREFIX: ${DB_TABLE_PREFIX}
WORDPRESS_CONFIG_EXTRA: |
/* Direct FTP */
define('FS_METHOD', 'direct');
volumes:
- ./src:/var/www/html/wp-content:rw
- ./config/php.ini:/usr/local/etc/php/conf.d/php.ini
depends_on:
- db
db:
image: mariadb
container_name: ${APP_NAME}-db
volumes:
- './database/db:/var/lib/mysql:delegated'
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASS}
restart: always
ports:
- '3306:3306'
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: ${APP_NAME}-phpmyadmin
volumes:
- ./config/phpmyadmin.ini:/usr/local/etc/php/conf.d/phpmyadmin.ini
environment:
PMA_HOST: ${DB_HOST}
PMA_PORT: 3306
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
ports:
- '8080:80'
links:
- db:db
Dockerfile
FROM wordpress
RUN chown -R www-data:www-data /var/www/html
There are some things that you can do:
1- Since you've declare:
ports:
- 80:80
You can't get to many wordpress at the same time with different docker-compose. So, you can delete the binding from your instance like this:
ports:
- 80
Doing it, you can make several docker-compose with different ports without worrying about the ports.
But there is another problem... Your database it's already up. So, in this case, if you want to use several docker-compose for each project you'll need another docker-compose for your database. So... the docker-compose solution is get another docker-compose for your database and in every docker-compose file add an external network:
With it, you can use the dns name as its always.
The other way is using the same template like this:
version: '3.3'
networks:
default:
external:
name: wordpresses
services:
wordpress_1:
build: .
container_name: ${APP_NAME}-wordpress
restart: always
ports:
- 80
environment:
WORDPRESS_DB_HOST: ${DB_HOST}
WORDPRESS_DB_USER: ${DB_USER}
WORDPRESS_DB_PASSWORD: ${DB_PASS}
WORDPRESS_DB_NAME: ${DB_NAME}
WORDPRESS_TABLE_PREFIX: ${DB_TABLE_PREFIX}
WORDPRESS_CONFIG_EXTRA: |
/* Direct FTP */
define('FS_METHOD', 'direct');
volumes:
- ./src/project1:/var/www/html/wp-content:rw
- ./config/php.ini:/usr/local/etc/php/conf.d/php.ini
depends_on:
- db
wordpress_2:
build: .
container_name: ${APP_NAME}-wordpress
restart: always
ports:
- 80
environment:
WORDPRESS_DB_HOST: ${DB_HOST}
WORDPRESS_DB_USER: ${DB_USER}
WORDPRESS_DB_PASSWORD: ${DB_PASS}
WORDPRESS_DB_NAME: ${DB_NAME}
WORDPRESS_TABLE_PREFIX: ${DB_TABLE_PREFIX}
WORDPRESS_CONFIG_EXTRA: |
/* Direct FTP */
define('FS_METHOD', 'direct');
volumes:
- ./src/project2:/var/www/html/wp-content:rw
- ./config/php.ini:/usr/local/etc/php/conf.d/php.ini
depends_on:
- db
db:
image: mariadb
container_name: ${APP_NAME}-db
volumes:
- './database/db:/var/lib/mysql:delegated'
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASS}
restart: always
ports:
- '3306:3306'
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: ${APP_NAME}-phpmyadmin
volumes:
- ./config/phpmyadmin.ini:/usr/local/etc/php/conf.d/phpmyadmin.ini
environment:
PMA_HOST: ${DB_HOST}
PMA_PORT: 3306
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
ports:
- '8080:80'
links:
- db:db
I know wordpress has some issues if you don't put the domain. In any case, you can use a NGINX in front of all wordpress as a reverse proxy like this Example.
Feel free to ask!

How can I use gitlab CI/CD variables to change in docker-compose.yml

I have been trying to understand this process of having an docker-compse.yml file that I can re-use for many production with diffrent hosts, sql password and user and so on.
I've been trying out on this file fx.
services:
db_node_domain:
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: PASSWORD
container_name: wordpress_db
wordpress:
depends_on:
- db_node_domain
image: wordpress:latest
expose:
- 80:80
restart: always
environment:
VIRTUAL_HOST: sub.domain.example
WORDPRESS_DB_HOST: db_node_domain:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: $TEST
container_name: wordpress
volumes:
db_data:
networks:
default:
external:
name: nginx-proxy
And then I've tried to use varibales in gitlab to change the WORDPRESS_DB_PASSWORD in many ways. This is just one example of trying.
I also tried to use sed -i in .gitlab-ci.yml to change password but I wanted to know if someone could help me out on how to do this.
Thx
You just need to replace hardcoded creds with environment variables.
Then each stages will have creds/settings.
In Gitlab go your project, then Settings > "CI / CD" > Variables.
Then populate each ENV variable needed, hey will be present on each run
services:
db_node_domain:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
container_name: wordpress_db
wordpress:
depends_on:
- db_node_domain
image: wordpress:latest
expose:
- 80:80
restart: always
environment:
VIRTUAL_HOST: ${VIRTUAL_HOST}
WORDPRESS_DB_HOST: ${WORDPRESS_DB_HOST}
WORDPRESS_DB_USER: ${WORDPRESS_DB_USER}
WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD}
container_name: wordpress
volumes:
db_data:
networks:
default:
external:
name: nginx-proxy

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
...

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.

Docker volume must be a mapping, not a string

I have the following file at ./wordpress/docker-compose.yaml:
version: '3.3'
serivces:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
evironment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
volumes:
- ./:/var/www/html
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data
When I run cd ./wordpress && docker-compose up -d I get the following error:
ERROR: In file './docker-compose.yaml', volume must be a mapping, not a string.
Can anyone tell me what I'm doing wrong?
There are certain typo errors first of, like serivces, evironment. They should spell services and environment. Also for the "... not string" error, just append ":" after your volume name like below
volumes:
db_data:
I had the same problem just now and the key was the indentation of the volume name i.e. db_data.
I fixed it by putting the volume name on the same level of indentation as the depends_on under the wordpress service in the example above. (hit TAB)
volumes:
mydata:
vs
volumes:
mydata:
that's will fix it and it works for me
volumes:
db_data:
driver: local
version: '3.3'
serivces:
db:
image: mysql:5.7
command: bash -c "mkdir -p /var/lib/mysql/"
volumes:
- db_data:/var/lib/mysql/
restart: always
evironment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
volumes:
- ./:/var/www/html
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data: {}
save as docker-compose.yml
cd wordpress
docker-compose up

Resources