error insert sql dump to mysql using docker - mysql

I try to import my existing backup.sql.gz in mysql using docker. this is my docker-compose.yml
mysql:
image: mysql:5.7
container_name: ${APP_NAME}-mysql
restart: unless-stopped
volumes:
- ./database:/var/lib/mysql
- ./mysqldumps/backup.sql.gz:/docker-entrypoint-initdb.d
environment:
MYSQL_DATABASE: ${DB_NAME}
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
when I try to run docker-compose up i got an error:
ERROR: for 52bf24fa5ad1_project Cannot start service mysql: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused "rootfs_linux.go:58: mounting \"project/mysqldumps/backup.sql\" to rootfs \"/var/lib/docker/overlay2/be2a6a81a405ab94e956086e578ecec2a98207ef53c8f85aaaf053d79d3c183a/merged\" ...
How can I fix it? thanks.

This indicates that the mount path of the volume is not existing.

Related

MySQL error: Can't initialize batch_readline - may be the input source is a directory or a block device

Mysql 5.7 fails trying to setup init script while starting in docker container. System is Mac ARM64
Docker compose looks like this:
mysql:
image: mysql:5.7
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: 'root'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- mysql:/var/lib/mysql:rw
As result Mysql throws error:
mysql | ERROR: Can't initialize batch_readline - may be the input source is a directory or a block device.
mysql exited with code 1
The problem is that docker compose doesn't recognize '.' as current directory.
Solution is:
volumes:
- $PWD/init.sql:/docker-entrypoint-initdb.d/init.sql

Docker CLI ERROR: "my_wordpress_wordpress_1 Cannot start service wordpress: driver failed"

It is the first time that I tried to install Wordpress in a container, I have used the following from the official docker documentation, but I received the following errors:
version: "3.9"
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
wordpress_data: {}
Error:
Creating my_wordpress_db_1 ... done
Creating my_wordpress_wordpress_1 ...
Creating my_wordpress_wordpress_1 ... error
ERROR: for my_wordpress_wordpress_1 Cannot start service wordpress: driver failed programming external connec tivity on endpoint my_wordpress_wordpress_1 (eaa2b38842baac2cbc2126426076f542fd8806509d5d5b19160e07a38f89f173) : Bind for 0.0.0.0:8000 failed: port is already allocated
ERROR: for wordpress Cannot start service wordpress: driver failed programming external connectivity on endpo int my_wordpress_wordpress_1 (eaa2b38842baac2cbc2126426076f542fd8806509d5d5b19160e07a38f89f173): Bind for 0.0. 0.0:8000 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
The error message you have pasted contains a lot of blank spaces which makes the output hard to read. But if you dig through the reason, Docker states the following why your Wordpress image will not work.
ERROR: for wordpress Cannot start service wordpress:
driver failed programming external connectivity on
endpoint my_wordpress_wordpress_1 (eaa2b38842baac2cbc2126426076f542fd8806509d5d5b19160e07a38f89f173):
Bind for 0.0.0.0:8000 failed: port is already allocated
The solution is to figure out which other resource is allocating port 8000 and close that program. It might be some other docker instance running, try docker ps -a to view all docker images. Alternatively you may specify a different port in your docker-compose.yml-file.
You can try
ports:
- "8080:80"
In your example you have 8000:80
You made a change to compiled information after running:
npm run build:dev
In my case I had manually modified /etc/hosts after compiling the build.
This broke referenced paths in the build.
Re-running npm run build:dev fixes the issue.

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

Error when accessing from one dotnet core docker container to a mysql docker container

I tried to build an application consisting on DotNet Core and a MySQL docker containers using docker-compose.
I used Entity Framework and I succeeded making the connection and updating database via command with this connection string:
"server=127.0.0.1;database=hellofreshgo;user=root;password=hellofreshgo;Port=3306"
But when I dockerize or containerize my netcore app and try to retrive data and connect with my mysql container with this connection string and the the following docker-compose file:
"server=mysql;database=hellofreshgo;user=root;password=hellofreshgo;Port=3306"
version: '3.4'
services:
hfgo.backendtest.api:
image: ${DOCKER_REGISTRY-}hfgobackendtestapi
build:
context: .
dockerfile: src/Presentations/HFGO.BackendTest.API/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:8080;http://+:80
volumes:
- ./:/server/http
links:
- mysql
ports:
- "8080:8080"
depends_on:
- mysql
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
mysql:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "3306:3306"
environment:
LC_ALL: C.UTF-8
MYSQL_ROOT_PASSWORD: hellofreshgo
MYSQL_DATABASE: hellofreshgo
I got the following error:
System.InvalidOperationException: An exception has been raised that is
likely due to a transient failure. Consider enabling transient error
resiliency by adding 'EnableRetryOnFailure()' to the 'UseMySql' call.
---> MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts. --->
System.AggregateException: One or more errors occurred. (Connection
refused 127.0.0.1:3306)
Before someone ask yes as the error mentions, I already added EnableRetryOnFailure() to UseMySQL()

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.