Can't figure out how i misconfigurated my docker-compose file - docker

I followed multiple guides and tutorials.
This is my docker-compose file.
It gives the following error,
services.mysql.environment.volumes contains ["mysql_data:/var/lib/mysql"], which is an invalid type, it should be a string, number, or a null
And it creates a volume with the name _volumename?
I just don't seem to get the mistake i made.
version : '3'
services:
mysql:
container_name: auth_db
restart: always
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: supersecret
MYSQL_DATABASE: name
MYSQL_USER: user
MYSQL_PASS: supersecret2
volumes:
- mysql_data:/var/lib/mysql
ports:
- 3306:3306
auth-service:
build: ./authservice
volumes:
- ./authservice:/usr/src/app
ports:
- 5000:5000
volumes:
mysql_data:
Some more information.
If i change the volume line to
volumes: "- mysql_data:/var/lib/mysql"
The docker-compose file will compile but
docker volume ls gives this output
local 4fa81a11596c2b67c2bb799d54afc6009ebcd82fcd10acae53a5aeefd005fd36
local 8636df909155569e8ebf0649f4c192616d0b6778d5eb7932b1f9542db55a07d8
local 832739c89f3b33ad0a1974ad7dc2ee9342373f904af3b2be5934331bae50b5e6
local e5bb4a869f54ce3200d5a1fe129bc1f8ee46515cf03d9dd2ff327430d792117b
local e35a38127fcb07702a58133883a021aa56c4aad6c439d254f32a119ad380d808
local hell_mysql_data
My current dir where the docker-compose file is located is HELL that why i think it got the prefix from

Your issue is regarding indentation, volumes and ports must be top level first inside your service definition, as follows:
version : '3'
services:
mysql:
container_name: auth_db
restart: always
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: supersecret
MYSQL_DATABASE: name
MYSQL_USER: user
MYSQL_PASS: supersecret2
volumes:
- mysql_data:/var/lib/mysql
ports:
- 3306:3306
auth-service:
build: ./authservice
volumes:
- ./authservice:/usr/src/app
ports:
- 5000:5000
volumes:
mysql_data:

Related

Docker-Compose: service "xxx" depends on undefined service "xxx": invalid compose project

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.

docker-compose volume not persisting database

I use docker-compose up -d to start my services then I use docker-compose down to stop it
problem; it seems that my data is not persisted; the "${SQL_INIT}:/docker-entrypoint-initdb.d" is executed ever time
I have setup volume db_data for persistence; docker volume ls returns
local backend_mariadb-data
local docker_db_data
local docker_db_logs
here is my docker-compose
version: "3"
services:
backend:
container_name: backend
image: backend
restart: always
build: images/backend/
env_file: .env
ports:
- "8000:8000"
depends_on:
- mariadb
networks:
- app_network
mariadb:
container_name: mariadb
image: "mariadb:${MARIADB_VERSION}"
restart: 'always'
env_file: .env
volumes:
- "${SQL_INIT}:/docker-entrypoint-initdb.d"
- "db_data:${MARIADB_DATA_DIR}"
- "db_logs:${MARIADB_LOG_DIR}"
environment:
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
MYSQL_DATABASE: "${MYSQL_DATABASE}"
MYSQL_USER: "${MYSQL_USER}"
MYSQL_PASSWORD: "${MYSQL_PASSWORD}"
ports:
- "3306:3306"
networks:
- app_network
volumes:
db_data:
db_logs:
networks:
app_network:
with .env file
MARIADB_VERSION="latest"
MARIADB_DATA_DIR="/var/database/mariadb"
MARIADB_LOG_DIR="/var/logs/mariadb"
MYSQL_DATABASE="app"
MYSQL_USER="app"
MYSQL_PASSWORD="password"
MYSQL_ROOT_PASSWORD="password"
SQL_INIT="./database/dev"
Remove quotes in env file. Docker-Compose treats them as part of the value.
See last Bulletpoint
Besides that the default path for the data is /var/lib/mysql and for logging is /var/log/mysql (but disabled on default)

When I ran docker-compose up -d error services must be a mapping windows indentation error

I follow the tutorial in here . When I'm in docker-compose.yml, I can't make the correct file, because the indentation not true. This is my docker-compose.yml:
# Version
version: '3.1'
# Setup
services:
# PHP
php:
depends_on:
- db
image: docker-php-dev
restart: always
ports:
- 5000:80
volumes:
- ./development:/var/www/html
# PHPMyAdmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 5050:80
environment:
PMA_HOST: db
# MySQL
db:
image: mysql:5.7
restart: always
volumes:
- wordpress_db:/var/lib/mysql
environment:
MYSQL_DATABASE: wordpress
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
# Volumes
volumes:
wordpress_db:
wordpress_uploads:
I always get error when I run docker-compose up -d. Can someone tell me how to make correct docker-compose.yml file?
You are referencing to a bad document which loss all indent, you should reference to compose file to see how a good compose file should look.
In basic, after write a compose file, you should use docker-compose config to check if the format is ok or not.
For your scenario, the workable fix is next, FYI:
# Version
version: '3.1'
# Setup
services:
# PHP
php:
depends_on:
- db
image: docker-php-dev
restart: always
ports:
- 5000:80
volumes:
- ./development:/var/www/html
# PHPMyAdmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 5050:80
environment:
PMA_HOST: db
# MySQL
db:
image: mysql:5.7
restart: always
volumes:
- wordpress_db:/var/lib/mysql
environment:
MYSQL_DATABASE: wordpress
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
# Volumes
volumes:
wordpress_db:
wordpress_uploads:

docker-compose variable format

I have the following docker-compose file. I have created the .env file as well with the relevant variables. when I attempt to run docker-compose using this file it complains about the variables. I know in Ansible yaml I declare the vars inside the file and can call them anywhere. Not sure what what I am doing wrong.
---
version: "3"
services:
${hostname01}:
image: apline:3.12
restart: unless-stopped
container_name: ${hostname01}
environment:
volumes:
- ${hostname01}_Data:/var/www/html
ports:
- 80:80
- 443:443
services:
${hostname02}:
image: mariadb
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: ${mySQLRootPass}
volumes:
- ${hostname02}_Data:/var/lib/mysql
ports:
- 3306:3306
volumes:
${hostname01}_Data:
${hostname02}_Data:
driver: local

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