Docker on Windows gives error when trying to start mysql container - mysql

I'm new on Docker and trying now to create my first docker-compose file.
The apache works so far but i'm struggeling a bit with mysql.
Here is my docker-compose.yaml
version: '3'
services:
web:
build: .
ports:
- "8083:80"
volumes:
- ./public_html/:/usr/local/apache2/htdocs/
networks:
- appnet
db:
image: mysql
volumes:
- db-data:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- appnet
adminer:
image: adminer
restart: always
ports:
- 8080:8080
volumes:
db-data:
networks:
appnet:
The error is:
[ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
I alread tried to remove the volume totally or added a /data at the end. But it gives me all the time the same error. Also Google didn't give me any good hints.
Does someone has an idea?

I figured out now. I'm running Docker on a Windows Machine, so i had to reset the credentials ond Docker App and type in again. Now it works. Hope that will help others as well. It seems on Docker for Windows, when something not work as expected it make sense to reset the credentials.

Related

DB in Docker container keeps getting deleted

I'm new to docekr and I'm setting up a Codeigniter docker image on my local machine
bitnami/codeigniter:3
Everything works fine but the mysql container looses it's DB and all the data everytime I perform
sudo docker-compose down
This is docker-compose file
version: '3.3'
services:
myapp:
image: docker.io/bitnami/codeigniter:3
container_name: app-backend
ports:
- '8000:8000'
volumes:
- '.:/app'
depends_on:
- mariadb
mariadb:
image: docker.io/bitnami/mariadb:10.3
container_name: app-marriadb
volumes:
- app_dbdata:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app_db
ports:
- '3307:3306'
environment:
- ALLOW_EMPTY_PASSWORD=yes
volumes:
app_dbdata:
I tried multiple possiblities but not sure where I'm going wrong. My OS is Ubuntu 18.04
The bitnami/mariadb image uses a different path inside the container for its database storage. Quoting the "Persisting your database" section of its Docker Hub page:
For persistence you should mount a directory at the /bitnami/mariadb path. If the mounted directory is empty, it will be initialized on the first run.
So in your Compose setup, specify that path as the mount directory:
version: '3.8'
services:
mariadb:
volumes:
- app_dbdata:/bitnami/mariadb # <-- not /var/lib/mysql
volumes:
app_dbdata: # unchanged

Can't connect to MySQL container from other container

I'm trying to connect my FASTAPI app container to a MySQL database container using the docker-compose file. In the Docker documentation it says that docker creates a default network for both containers. However, I would like to use a pre-existing network that I've created(app-net).
This is my docker-compose file:
version: '3.4'
services:
mysql:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
container_name: mysql
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql-data:/var/lib/mysql
app:
build: .
image: app:1.0
container_name: app
ports:
- 8000:8000
environment:
PASSWORD: password
USERNAME: root
networks:
default:
external: true
name: app-net
volumes:
mysql-data:
driver: local
this is the output I get when i run docker inspect mysql -f "{{json .NetworkSettings.Networks }}":
{"app-net":{"IPAMConfig":null,"Links":null,"Aliases":["mysql","5e998f9fb646"],"NetworkID":"7f60c83e4c88d25e674461521446ec9fa98baca8639c782c79671c4fcb77ba88","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"","DriverOpts":null}}
However, when I run each container individually using CMD with the --network app-net the output is different:
{"app-net":{"IPAMConfig":null,"Links":null,"Aliases":["46157e588c87"],"NetworkID":"7f60c83e4c88d25e674461521446ec9fa98baca8639c782c79671c4fcb77ba88","EndpointID":"6a6922a9a6ea8f9d113447decbbb927cb93ddffd3b9563ee882fa2e44970cde5","Gateway":"172.20.0.1","IPAddress":"172.20.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:14:00:02","DriverOpts":null}}
In my app code in order to connect the mysql server, I specified the container name as the hostname since they are supposed to share the same network. But, as I mentioned it seems both containers can't talk to each other.
I'm pretty sure that is the reason I can't connect the database through my app and get that error when I run:
docker-compose -f docker-compose.yml up
I get this error:
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2005, "Unknown MySQL server host 'mysql' (0)")
What am I missing?
If the error you're getting is specifically "unknown host" or something similar, this can happen if your application container starts before the database container exists. You can work around this situation by telling Compose about the dependency:
version: '3.8'
services:
mysql: { ... }
app:
depends_on:
- mysql
This has two key effects. If you try to start only the application container docker-compose up app, it will also start the mysql container, following the depends_on: chain. When Compose does start containers, it at least creates the mysql container before creating the app container.
Note that this doesn't guarantee the database will actually be running by the time your application starts up. If you do encounter this, you will get a different error like "connection refused". You can work around this by embedding a script in your image that waits for the database to be available, or by using a version 2 Compose file with health-check support; see Docker Compose wait for container X before starting Y for more details on this specific problem.
You could add the key networks to both containers, this way:
version: '3.4'
services:
mysql:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
container_name: mysql
restart: always
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql-data:/var/lib/mysql
networks:
- app-net
app:
build: .
image: app:1.0
container_name: app
ports:
- 8000:8000
environment:
PASSWORD: password
USERNAME: root
networks:
- app-net
networks:
default:
external: true
name: app-net
volumes:
mysql-data:
driver: local

Docker Wordpress not connecting with docker Mysql Compose

I am trying to set up multiple WordPress sites being hosting on the same server using docker using nginx reverse proxy server. I have a network setup with the nginx proxy listening on port 80 called nginx-proxy. I confirmed they are all on the same docker network. However, I am still getting WordPress is unable to communicate with the database. I can confirm that the credentials are correct and that MySQL is running on the other docker container.
With my testing, the forwarding proxy is working if I update the virtual host and open it in the web browser it does open the error message on that page. I can ping both the containers from within their shell. I am not sure what I am doing wrong I am newer to dockers networking within version 3. I don't believe docker has differenced between platforms, but in case it does I am running it on Mac OS.
I know there are other posts similar to this using Link and version 2 however, I was not able to find a version 3 with information that fixed my issue.
version: "3.3"
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
restart: always
environment:
VIRTUAL_HOST: domain.com
WORDPRESS_DB_HOST: db_node_domain:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: PASSWORD
container_name: wordpress
volumes:
db_data: {}
networks:
default:
external:
name: nginx-proxy
Please let me know if any more information is required. Thank you for any assistance you might be able to provide.
I don't see any problem with your docker-compose file, also it's working on my local machine which is macOS.
One thing, 'wordpress' container will fail to bring up several times with MySQL Connection Error: (2002) Connection refused on the first bootup due to MySQL initialization. It should connect to 'wordpress_db' after a few retrials.
If it doesn't connect even after retrials, can you attach logs of 'wordpress' container?
You can use this as your docker compose file below.
Reference:
https://github.com/docker/awesome-compose/tree/master/wordpress-mysql
version: '3.7'
services:
db:
image: mysql:8.0.19
command: '--default-authentication-plugin=mysql_native_password'
restart: always
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
wordpress:
image: wordpress:latest
ports:
- 80:80
restart: always
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
volumes:
db_data:
I have isolated the issue. It was the first time I was playing the docker volumes. I forgot to clear out the volumes when I updated the database information. So I was connecting to the old database with the new information which was leading to the error (This is why your don't try to learn something new late into the night on little sleep). Once I removed the volumes recomposed the docker containers it works perfectly.
Thank you for every one who assisted me with this.

Docker compose mysql exited with code 0

I'm trying to add mysql to a docker compose file, but every time it gives me the error some_name exited with code 0. I tried diffent configurations and even took the following config from the docker docs:
# Use root/example as user/password credentials
version: '3.1'
services:
db:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
adminer:
image: adminer
restart: always
ports:
- 8080:8080
In this case adminer works but mysql doesn't.
My config:
mysqldb:
image: mysql
container_name: ${MYSQL_HOST}
env_file:
- ".env"
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
ports:
- "8989:3306"
Both my config and that from the docker docs keep giving me the same error. Although any other services are working fine. Any ideas?
How unfortunate. After hours I deleted my mysql image and after that everything worked fine.

Why did mysql data ownership change to systemd-journal-remote after running a docker container

I have the mysql database stored in /home/mysql instead of /var/lib/mysql. The directory used to be owned by mysql. However, when I run the command docker-compose up with this yml file:
version: '3'
services:
mariadb:
image: mariadb
restart: always
volumes:
- /home/mysql:/var/lib/mysql
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.6.4
environment:
- "ES_JAVA_OPTS=-Xms750m -Xmx750m"
- bootstrap.memory_lock=false
site:
build: .
volumes:
- "./app:/app"
links:
- mariadb:mysql
environment:
- DOCKER_IP=172.19.0.2
depends_on: ['elasticsearch','mariadb']
ports:
- "3000:3000"
The docker container is able to run, but the entire folder and files in /home/mysql are owned by systemd-journal-remote, which causes the node server fails to connect to mariadb. I have to stop the docker instance, restore the mysql folder ownership and delete ib_logfile0 and ib_logfile1.
Why does mounting /home/mysql cause such a fatal problem?
Update:
My solution is to add user: "mysql":
version: '3'
services:
mariadb:
image: mariadb
restart: always
volumes:
- /home/mysql:/var/lib/mysql
user: "mysql"
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.6.4
environment:
- "ES_JAVA_OPTS=-Xms750m -Xmx750m"
- bootstrap.memory_lock=false
site:
build: .
volumes:
- "./app:/app"
links:
- mariadb:mysql
environment:
- DOCKER_IP=172.19.0.2
depends_on: ['elasticsearch','mariadb']
ports:
- "3000:3000"
You should start Docker's container with --user parameter. If you do this and set the same uid:gid as owner of the MySQL storage you will no have problems with permissions. You have to check how exactly to do this in Docker Compose because I show you example for normal command line execution
Most likely, uid of your user systemd-journal-remote is the same as uid of user mysqld in container. Check with ls -n. To avoid confusion, either use common uids, perhaps test as root:root with chmod o+rwx.