I'm having some problems with a docker-compose file and haven't been able to figure out why.
This is the file:
version: '3'
services:
db:
image: mysql:8.0.0
environment:
MYSQL_ROOT_PASSWORD: ******
MYSQL_DATABASE: ******
volumes:
- /opt/mysql_auth:/var/lib/mysql
restart: always
auth:
image: authdotnetcore
build:
context: .
dockerfile: Auth/Dockerfile
depends_on:
- db
restart: always
networks:
- web
networks:
web:
external: true
The auth service is a ASP.NET Core application which connect to the MySQL database using db as server on the appsettings. The compose file works fine if I remove the external web network, but when I add it I get a: Unable to connect to any of the specified MySQL hosts.
It should work because the compose default network linking the containers is still created.
As per the docker-compose documentation, Please find more details Here.
DB service was missing the networks section:
version: '3'
services:
db:
image: 'mysql:8.0.0'
environment:
MYSQL_ROOT_PASSWORD: null
MYSQL_DATABASE: null
volumes:
- '/opt/mysql_auth:/var/lib/mysql'
restart: always
networks:
- web
auth:
image: authdotnetcore
build:
context: .
dockerfile: Auth/Dockerfile
depends_on:
- db
restart: always
links:
- db
networks:
- web
networks:
web:
external: true
Related
Im trying to deploy with docker compose an app; mi yml is:
version: '3.9'
services:
db:
image: mongo
restart: always
volumes:
- 'dbdata:/data/db'
container_name: database
server:
build: .
restart: always
ports:
- '2000:2000'
depends_on:
- db
container_name: api
links:
- database
frontend:
build: ./client
restart: always
ports:
- '3000:3000'
depends_on:
- server
container_name: client
links:
- api
volumes:
dbdata:
When i docker compose up -d this in my PC it works correctly, but when i do the same thing in a oracle linux 8.6 fedora; seems like is working fine until came to the lines:
Network soccer_default Created
no such service: database
And stop the process. Is something related with the machine or docker version?
I want to know how to configure correctly the backend endpoint.
I have a docker images that runs different containers:
Backend
Frontend
Nginx for backend
DB
From my understanding, since all containers are running on the same machine, I should be able to reach the backend with "host.docker.internal".
Indeed I can successfully do it on the local machine where Docker is running on.
By the way the frontend is not able to resolve the endpoint "host.docker.internal" if I try to make a request from another machine. Please note that I'm able to reach the frontend from another machine, it's just a matter of endpoint configuration.
Note that "192.168.1.11" is the IP of the machine where Docker is running, and "8888" it's the port where the frontend is.
Obviously I can succesfully make the requests from other machines too if I put the static IP address instead of "host.docker.internal". But the question is: since the React frontend application is served on Docker itself, shouldn't it be able to resolve the "host.docker.internal" endpoint?
Just for reference, here it is my docker compose:
version: "3.8"
services:
db: #mysqldb
image: mysql:5.7
container_name: ${DB_SERVICE_NAME}
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
ports:
- $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- backend
mrmfrontend:
build:
context: ./mrmfrontend
args:
- REACT_APP_API_BASE_URL=$CLIENT_API_BASE_URL
- REACT_APP_BACKEND_ENDPOINT=$REACT_APP_BACKEND_ENDPOINT
- REACT_APP_FRONTEND_ENDPOINT=$REACT_APP_FRONTEND_ENDPOINT
- REACT_APP_FRONTEND_ENDPOINT_ERROR=$REACT_APP_FRONTEND_ENDPOINT_ERROR
- REACT_APP_CUSTOMER=$REACT_APP_CUSTOMER
- REACT_APP_NAME=$REACT_APP_NAME
- REACT_APP_OWNER=""
ports:
- $REACT_LOCAL_PORT:$REACT_DOCKER_PORT
networks:
- frontend
volumes:
- ./docker-compose/nginx/frontend:/etc/nginx/conf.d/
app:
build:
args:
user: admin
uid: 1000
context: ./MRMBackend
dockerfile: Dockerfile
image: backend
container_name: backend-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./MRMBackend:/var/www
networks:
- backend
nginx:
image: nginx:alpine
container_name: backend-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./MRMBackend:/var/www
- ./docker-compose/nginx/backend:/etc/nginx/conf.d/
networks:
- backend
- frontend
volumes:
db:
networks:
frontend:
driver: bridge
backend:
driver: bridge
The endpoint is configured in this way in the .env:
REACT_APP_BACKEND_ENDPOINT="http://host.docker.internal:8000"
services:
postgres:
container_name: 'lh-postgres'
image: 'postgres:13'
environment:
POSTGRES_PASSWORD: root
redis:
container_name: 'lh-redis'
image: 'redis:6'
nginx:
container_name: 'lh-nginx'
build: ./nginx
depends_on:
- php-fpm
volumes:
- ./src/lh-app:/var/www/html/app
- ./src/lh-api:/var/www/html/api
ports:
- "80:80"
- "443:443"
php-fpm:
container_name: 'lh-php'
image: docker.io/bitnami/php-fpm:8.0
user: '1000:1000'
build:
context: ./php-fpm
args:
- PHP_ENV= development
depends_on:
- postgres
- redis
volumes:
- ./src/lh-app:/var/www/html/app
- ./src/lh-api:/var/www/html/api
ERROR: The Compose file './docker-compose.yml' is invalid because:
Unsupported config option for services: 'postgres'
getting this error
I think you are missing some ENV vars.
This is our docker-compose.yml for Postgres
version: '3.9'
services:
db:
image: postgres:latest
restart: "no"
container_name: db
volumes:
- ./database:/var/lib/postgresql/data
ports:
- "8002:5432"
environment:
POSTGRES_PASSWORD: verySecurePassword34058
POSTGRES_USER: root
POSTGRES_DB: myDatabase
networks:
default:
external: true
name: our-network
Other parts of the application, (like Redis, the NodeJS App, etc) are in other docker-compose.yml files, But since they share the same network, they talk to each other.
You have not mentioned version in docker-composer.yml
version: '2'
services:
postgres:
container_name: 'lh-postgres'
image: 'postgres:13'
environment:
POSTGRES_PASSWORD: root
redis:
container_name: 'lh-redis'
image: 'redis:6'
You should include your docker and docker-compose version in the querstion to help us answer you.
It would also be wise to define the version: 'x' element at the top of your compose file.
You may be suffering from an old version of the cli, akin to this question:
docker-compose : Unsupported config option for services service: 'web'
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 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.