I am setting up the following docker containers with the following 2 docker-compose files:
version: '3.7'
services:
mysql:
image: mysql:5.7
restart: on-failure
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
volumes:
- db_data:/var/lib/mysql:rw
ports:
- '${MYSQL_PORT}:3306'
networks:
- shared_mysql
php:
build:
context: .
dockerfile: docker/php/Dockerfile
restart: on-failure
volumes:
- '../:/usr/src/app'
user: ${LOCAL_USER}
networks:
- shared_mysql
api_nginx:
image: nginx:1.15.3-alpine
restart: on-failure
volumes:
- '../public/:/usr/src/app'
- './docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro'
ports:
- '21180:80'
depends_on:
- php
networks:
- shared_mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: on-failure
ports:
- '${PHPMYADMIN_PORT}:80'
environment:
PMA_HOST: mysql
MYSQL_USERNAME: ${MYSQL_USER}
MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
networks:
- shared_mysql
volumes:
db_data:
networks:
shared_mysql:
version: '3.7'
services:
php:
build:
context: .
dockerfile: docker/php/Dockerfile
restart: on-failure
volumes:
- '../:/usr/src/app'
user: ${LOCAL_USER}
networks:
- api_21s_shared_mysql
auth_nginx:
image: nginx:1.15.3-alpine
restart: on-failure
volumes:
- '../public/:/usr/src/app'
- './docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro'
ports:
- '21181:80'
depends_on:
- php
networks:
- api_21s_shared_mysql
volumes:
db_data:
networks:
api_21s_shared_mysql:
external: true
When I visit http://localhost:21181/, I always get the correct website.
But when I visit http://localhost:21182/, I get http://localhost:21181/ or http://localhost:21182/ random.
I tried to set up the network sepperate.
I'd like it to work with the portnumbers, but I don't want them to be mixed up.
I am hoping someone can help me. Thank you in advance.
When services are started with docker-compose, they are discoverable within the docker network in different ways: by service name, by container name, by IP, etc.
In your case you use the discovery by service name, since in your nginx configuration you have the reference "php:9000".
At this point docker looks for a service named "php" and finds 2. It interprets them as replicas of the same service and sends traffic to them following a round-robin pattern (first request to first instance of the service, second to the second instance of the service, third to the first instance, forth to the second instance, etc.)
Solution
Name the services differently, just like you already do with your nginx services (auth_nginx and api_nginx).
Then in your default.conf for both services change the line referring to php:9000 accordingly.
Related
So I looked at this article: link
And I don't see the problem I made, so that's why I am asking.
The error I am getting is:
service "Appback" depends on undefined service -Sql-server: invalid compose project
And my docker-compose.yml file looks like this:
version: '3.8'
services:
Sql-server:
image: mysql:latest
environment:
MYSQL_DATABASE: *****
MYSQL_USER: ****
MYSQL_PASSWORD: *****
MYSQL_ROOT_PASSWORD: *****
ports:
- 3306:3306
expose:
- 3306
volumes:
- db:/var/lib/mysql
networks:
- appnetwork
Appback:
build:
context: ./AppBack
dockerfile: Dockerfile
ports:
- 8080:8080
depends_on:
-Sql-server:
condition: service_started
networks:
- appnetwork
appfront:
build:
context: ./appfront
dockerfile: Dockerfile
ports:
- 3000:3000
depends_on:
-Appback:
condition: service_started
networks:
- appnetwork
volumes:
db:
networks:
appnetwork:
driver: bridge
I don't see where the problem might be as I am also new to docker compose.
You should separate the -Sql-server: with a space - Sql-server: from the first hyphen character.
I saw the same error when you have -Appback: in the next few lines.
i've installed docker (windows 10) with wsl2 (ubuntu distro) and added my docker-compose.yml
version: '3'
services:
web:
image: nginx:1.20.1
container_name: web
restart: always
ports:
- "80:80"
volumes:
- ./nginx.d.conf:/etc/nginx/conf.d/nginx.conf
- ./nginx.conf:/etc/nginx/nginx.conf
- ./www/my-app:/app
php:
build:
context: .
dockerfile: myphp.dockerFile
container_name: php
restart: always
depends_on:
- web
volumes:
- ./www/my-app:/app
mysql:
image: mariadb:10.3.28
container_name: mysql
restart: always
depends_on:
- php
environment:
MYSQL_ROOT_PASSWORD: '******'
MYSQL_USER: 'root'
MYSQL_PASSWORD: '******'
MYSQL_DATABASE: 'my-database'
command: ["--default-authentication-plugin=mysql_native_password"]
volumes:
- mysqldata:/var/lib/mysql
- ./my.cnf:/etc/mysql/my.cnf
ports:
- 3306:3306
cache:
image: redis:5.0.3
container_name: cache
restart: always
ports:
- 6379:6379
networks:
- my-network
volumes:
- ./cache:/cache
volumes:
mysqldata: {}
networks:
my-network:
driver: "bridge"
So my symfony code is in the /www/my-app window's folder. This includes the /www/my-app/vendor too.
My application is running extremely slow (50-70 seconds). If i'm correct it's because the vendor folder is huge (80MB) and docker creates an image of it every time. Other discussions mentioned that vendor folder sould be moved into a new volume, and here i'm stuck with it. How to move and mount that in this case, and how should the docker-compose.yml look like after it?
and excuse me for my English.
I'm using docker in wsl 2 and I have a docker application with three images: laravel, phpmyadmin and mysql
my problem is what I can not access to phpmyadmin.¿can I help me please?
Attached image of the docker application running.
Whe I tye to access to phpmyadmin, appearc not found page.
docker-compose.yml
version: '3.8'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}' #empty(niether)
MYSQL_DATABASE: '${DB_DATABASE}' #fastfood
# MYSQL_USER: '${DB_USERNAME}' #root
MYSQL_PASSWORD: '${DB_PASSWORD}' #empty
MYSQL_HOST: '${DB_HOST}' #localhost
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
phpmyadmin:
image: 'phpmyadmin'
container_name: pma
environment:
PMA_HOST: '${DB_HOST}' #localhost
PMA_PASSWORD: '${DB_PASSWORD}' #empty(niether)
PMA_ARBITRARY: 1
restart: always
ports:
- 8081:80
depends_on:
- mysql
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
information
windows 10 19052.1052
docker 3.5.1
Use http://localhost:8081/ instead.
/phpmyadmin is just a route that set for the server software like wamp for you to access phpmyadmin easily.
Since you have defined in your docker-compose.yml
phpmyadmin:
image: 'phpmyadmin'
...
ports:
- 8081:80
...
which is port 8081
i have a problem with docker container.
That's my docker-compose file with 5 services
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8088:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- mysql
- php
networks:
- laravel
mysql:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "4306:3306"
environment:
MYSQL_DATABASE: homestead
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secret
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
redis:
image: redis:5.0.0-alpine
restart: always
container_name: redis
ports:
- "6379:6379"
networks:
- laravel
composer:
image: composer:latest
container_name: composer
volumes:
- ./src:/var/www/html
tty: true
working_dir: /var/www/html
networks:
- laravel
then i run
docker-compose up -d
and then
docker-compose ps
to see my container and i always get the composer contaier down with code 0. that's the screenshot
:
can someone explain me why i can't put this container up. Thanks a lot
composer isn't a program that stays alive. It's a program that does specific some work and then exits.
There's not much purpose in keeping it "up", since it's not going to do anything like the other processes do (nginx intercepts web traffic and writes response, mysql accepts database connections and reads/writes from a database, php serves web content, redis can be connected to as a cache).
I'm using jwilder/nginx-proxy to host multiple (web)apps from a single server. This is working great except that all services can communicate with each other because they are all on the same network because that is required for the proxy to work.
Proxy docker-compose.yaml
version: "3"
services:
nginx-proxy:
image: jwilder/nginx-proxy:alpine
container_name: nginx-proxy
labels:
- "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy"
ports:
- "80:80"
- "443:443"
volumes:
- ./data/certs:/etc/nginx/certs:ro
- ./data/nginx/vhost.d:/etc/nginx/vhost.d
- ./data/share/nginx/html:/usr/share/nginx/html
- /var/run/docker.sock:/tmp/docker.sock:ro
restart: always
letsencrypt-proxy:
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: letsencrypt-proxy
depends_on:
- nginx-proxy
volumes:
- ./data/nginx/vhost.d:/etc/nginx/vhost.d
- ./data/share/nginx/html:/usr/share/nginx/html
- ./data/certs:/etc/nginx/certs:rw
- /var/run/docker.sock:/var/run/docker.sock:ro
restart: always
networks:
default:
external:
name: nginx-proxy
App 1 docker-compose.yaml
version: "3"
services:
app:
image: nginx:latest
depends_on:
- db
- cache
expose:
- 80
volumes:
- ./application:/var/www/html
restart: always
working_dir: /var/www/html
environment:
VIRTUAL_HOST: app1.example.com
LETSENCRYPT_HOST: app1.example.com
LETSENCRYPT_EMAIL: user#example.com
cache:
image: redis:alpine
restart: always
volumes:
- cachedata:/data
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpasswd
MYSQL_DATABASE: database_name
MYSQL_USER: database_user
MYSQL_PASSWORD: database_passwd
volumes:
- dbdata:/var/lib/mysql
networks:
default:
external:
name: nginx-proxy
volumes:
dbdata:
driver: local
cachedata:
driver: local
App 2 docker-compose.yaml
version: "3"
services:
app:
image: nginx:latest
depends_on:
- db
- cache
expose:
- 80
volumes:
- ./application:/var/www/html
restart: always
working_dir: /var/www/html
environment:
VIRTUAL_HOST: app2.example.com
LETSENCRYPT_HOST: app2.example.com
LETSENCRYPT_EMAIL: user#example.com
cache:
image: redis:alpine
restart: always
volumes:
- cachedata:/data
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootpasswd
MYSQL_DATABASE: database_name
MYSQL_USER: database_user
MYSQL_PASSWORD: database_passwd
volumes:
- dbdata:/var/lib/mysql
networks:
default:
external:
name: nginx-proxy
volumes:
dbdata:
driver: local
cachedata:
driver: local
With this setup both applications will use de db and cache instance of App 1. The only way to solve that is to give those services unique names like app_1_db and app_2_db. But then App 1 is still able to connect to the app_2_db which I would like to prevent.
Is there a way to isolate all services within their docker-composer.yaml file and still use the nginx proxy?
Docker version 18.09.0, build 4d60db4
docker-compose version 1.21.2, build a133471
You can connect only the app(nginx) container from your apps to the nginx-proxy network. The only edit needed should be in the app's docker-compose:
version: '3'
services:
app:
networks:
- default
- nginx-proxy
networks:
nginx-proxy:
external: true
That way the app service will be connected to nginx-proxy and default networks at the same time. (If you omit networks key, service is always connected to the default network)
Resolving service names to containers ip's then works as expected as long as no container can see (across all networks it's connected to) two containers with same service name.
If you want even more isolation, you can create nginx-proxy network for every app.
So in your nginx-proxy docker-compose you will have:
version: "3"
services:
nginx-proxy:
networks:
- default
- nginx-proxy_app1
- nginx-proxy_app2
# letsencrypt-proxy service doesn't have to have networks key
networks:
nginx-proxy_app1:
external: true
nginx-proxy_app2:
external: true
and in your apps:
version: '3'
services:
app:
networks:
- default
- nginx-proxy_app1
networks:
nginx-proxy_app1:
external: true
and
version: '3'
services:
app:
networks:
- default
- nginx-proxy_app2
networks:
nginx-proxy_app2:
external: true
That way in every "proxy" network there is only one (if you are not using docker-compose scaling) app container and the nginx-proxy container.
More reading:
https://docs.docker.com/compose/networking/
https://docs.docker.com/network/overlay/#operations-for-standalone-containers-on-overlay-networks