Docker compose and mysql issues connecting to local db - mysql

Even though I googled as much as I could there's not really anything that worked out for me.
Hence my question:
I am learning docker and docker compose.
I made a dockerfile:
# base image
FROM mysql:8.0-debian
# creating user to access db
RUN groupadd -r group-mysql && useradd -r -g user-mysql group-mysql
# required env variable
ENV MYSQL_ROOT_PASSWORD ale123
ENV MYSQL_USER user-mysql
ENV MYSQL_PASSWORD pass123
# expose the default port
EXPOSE 3306
When I docker run the equivalent container and execute a command from the inside of the container the mysql part works perfectly and it allows me to create tables, users dbs and such. There is no connection error.
Now the issue, I created an (in my opinion) equivalent docker compose file
services:
mysql:
image: mysql:8.0-debian
command: sh -c "groupadd -r group-mysql &&
useradd -r -g user-mysql group-mysql &&
tail -f /dev/null"
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_USER: user-mysql
MYSQL_PASSWORD: pass123
ports:
- 3306:3306
When I run the docker compose up of the aforementioned .yml file and I try to run docker exec -it containername bash and then mysql -u root -p I get this connection error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
What am I doing wrong? Could someone point me in the right direction?
Thank you very much

Delete the command: line.
If you provide a Compose command: override, that runs instead of the normal main container command. In your case the command: override runs a couple of commands, then does tail -f /dev/null; that "keep the container running" command is running instead of the actual database server.
You don't actually need the additional user inside the container. Nothing in this setup makes use of that user. The default mysql image setup will run the MySQL server as a foreground process, and that's the behavior you need.

Related

cann't connect to the mysql docker container following the official instructures

Following steps in https://hub.docker.com/_/mysql/:
Start a mysql server instance
Starting a MySQL instance is simple:
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d
mysql:tag
... where some-mysql is the name you want to assign to your container,
my-secret-pw is the password to be set for the MySQL root user and tag
is the tag specifying the MySQL version you want. See the list above
for relevant tags. Connect to MySQL from the MySQL command line client
The following command starts another mysql container instance and runs
the mysql command line client against your original mysql container,
allowing you to execute SQL statements against your database instance:
$ docker run -it --network some-network --rm mysql mysql -hsome-mysql
-uexample-user -p
... where some-mysql is the name of your original mysql container
(connected to the some-network Docker network).
I started a mysql docker container, and then I tried to run another as the mysql client, but I don't know how to specific the --network parameter:
What should I input instead of some-network? I am newbie to Docker, and have no idea of Docker network. If I ommit this parameter, Unknown MySQL server host error is given.
Before you start the first container, you need to create a Docker network
docker network create some-network
You can use any name you want here, but I will use some-network for consistency with the question.
When you start the database container, it also needs to be attached to the same network
docker volume create mysql-data # this is essentially required too
docker run \
--name some-mysql \
-e MYSQL_ROOT_PASSWORD=my-secret-pw \
-d \
--net some-network \ # matches `docker network create`
-v mysql-data:/var/lib/mysql/data \ # don't lose data on restart
mysql:tag
(There is also a docker network connect command, but recreating containers to change settings is a pretty normal practice.)
You also don't need a second container to run a MySQL client: you can connect with the ordinary mysql command-line tool from the host. You need to publish a port out of the container
docker run \
-p 12345:3306 \
...
The first port number can be anything you want that doesn't conflict with another process on the host; the second number must be the standard MySQL port number 3306. You can then connect to that database with
mysql -h 127.0.0.1 -p 12345 -u example-user -p
Other answers to this question have endorsed Docker Compose as a setup. Compose will docker network create a network for you; Networking in Compose describes this setup in more detail. However, it's not great at running interactive terminal applications, and you might need to do something like docker-compose run db mysql -h db ... to get access to it this way. The published ports: approach will work too.
If you have more than one container which work together, you should read about docker-compose in order to config network, host, env var and so on...
// docker-compose.yml
version: "3.2"
services:
mysql_client:
depends_on:
- mysql_database
mysql_database:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: test
MYSQL_DATABASE: test
MYSQL_USER: test
MYSQL_PASSWORD: test
# exec this command to up your containers
docker-compose up
By default container are on the same network, in your mysql_client use mysql_database as hostname for mysql connection.
Via DockerHub I found this docker compose script to have Adminer and MySQL running in harmony.
# Use root/example as user/password credentials
version: '3.1'
services:
adminer:
image: adminer
restart: always
ports:
- 8080:8080
db:
image: mysql:5.6
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
Save it ti a file called docker-compose.yml and run it using docker-compose.
In cmd promt navigate to the directory containing the file and run the following:
docker-compose up
docker-compose reference

Cannot connect to mysql in Docker container from host

I'm running a Docker mysql container on my Mac laptop. Previously I was able to connect to it from the host OS with the mysql client. However, somehow it got deleted, and now after I re-created it, I can no longer to connect to it. I've searched dozens of similar questions, but am completely stumped.
Here's how I created it:
docker container run --name mysql-zhxw-dev -p 3306:3306 --expose=3306 -v zhxw-local-db-:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql:5.7.30
Every time I run mysql -u root -h 127.0.0.1 the following from my host OS, I get:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)
I can login to the container, and connect to mysql from within:
docker container exec -it mysql-zhxw-dev bash
mysql -u root <-- connects fine
I've tried:
Omitting the named volume
Specifying a password
Various versions of mysql, including 5.6 and 5.7
Logging in to the container with docker container exec, installing vi, editing /etc/mysql/mysql.conf.d/mysqld.cnf and uncommenting the line that contains bind-address. I tried it with both bind-address = 0.0.0.0 and bind-address = 127.0.0.1, then obviously exiting and running docker container restart mysql-zhxw-dev.
Specifying port to connect to with -P 3306
Connecting with -h localhost, -h 127.0.0.1, -h 0.0.0.0, and omitting the -h
Specifying --protocol=TCP when connecting
I'm at a loss as to what else to try.
i have a template in docker-compose with mysql, maybe it can help you.
docker-compose.yml
version: "3.2"
services:
mysql:
image: mysql:latest
ports:
- "3306:3306"
volumes:
- /path-persistent-volumen:/var/lib/mysql/
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
It turns out docker must have been in a strange state. Rebooting my laptop solved the problem.
Before rebooting, I tried restarting Docker Desktop, and that did not fix it. Only a full reboot resolved it.
One thing that I did notice was before the reboot, when I ran docker container ls -a, there were no containers, apart from the one mysql one I was trying to get working. I thought I had perhaps pruned them from some cleanup command. After the reboot, all my containers came back.
I did recently upgrade docker using Homebrew, so perhaps that put it in a weird state.
This error generally occurs due to problems related to port on the host.
Try these things:
Check logs of the container
Start the container in attached mode using -a flag
Run docker inspect mysql-zhxw-dev and check HostPort and HostIp and try to find something anomalous.
You can also change the host port in port mapping to something else like -v 3308:3306.
Also, this might help https://stackoverflow.com/a/32361238/9586997
P.S. I have copied and run the same command you have given, and it is running fine.
I had this issue after modifying the docker images and container configuration. Turns out the local copy of the MySQL data instance was corrupt.
Removing the ./data directory noted in this compose file and rebuilding worked.
The compose file
# /docker/docker-compose.yml
---
services:
db:
container_name: 'wp-mysql'
image: 'mysql:5.7.37'
platform: linux/amd64
volumes:
- './data/mysql:/var/lib/mysql'
ports:
- "18766:3306"
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress_db
MYSQL_USER: wordpress_user
MYSQL_PASSWORD: wordpress_password
The rebuild command for docker
docker-compose -f "docker-compose.yml" up -d --build

Mysql docker container exited after start, option `-d` has no effect

I have problem with mysql-server container.
Post mysql with Exited(1) from docker has no solution for me included.
Here my workflow on windows 10.
0. Docker version:
Docker version 17.12.0-ce, build c97c6d6
1. My Dockerfile:
FROM mysql/mysql-server
ENV MYSQL_ROOT_PASSWORD root
ENV MYSQL_DATABASE blockchain
ENV MYSQL_USER block
ENV MYSQL_PASSWORD blockchain
COPY create_schema.sql /docker-entrypoint-initdb.d/create_schema.sql
EXPOSE 3306
Build command:
docker build -t mysqlserver .
Run command (option -d is used):
docker run -ti -p 3306:3306 --name mysqlserver1 -v C:/Users/user/sandbox/mysql:/var/lib/mysql -d --net testnetwork mysqlserver --innodb_use_native_aio=0
But result after start is: Exited (1) 11 minutes ago, declared path folder for database is initialized.
Where is my error ?
Thx for help
For mysql docker container, you need to specify the following parameters in your Dockerfile;
MYSQL_ROOT_PASSWORD
Initial Database to be created
Expose Port from the docker container to the host machine
For instance if you're creating the running mysql docker image using terminal, it could be: sudo docker run -e MYSQL_ROOT_PASSWORD=dev --name testdb -p 3800:3600 -d mysql:8.0.17

How to launch a service like mysql in a docker container?

So I've tried to create a docker-compose.yml file to create one container for my web app with laravel and an other container for my database with mariadb. I've tried to link those two container together so that laravel could fetch data from mariadb. The problem is that my laravel container does not have a mysql socket file, so it can't use mysql. I'm pretty sure that I've missed something, or that I don't understood something because nothing works. So my question is how do we propely connect a archlinux container with laravel to a mysql container? Here's my docker-compose file
version: '3'
services:
database:
build: ./database
container_name: database.dev
command: mysqld --user=root --verbose
ports:
- "3307:3306"
restart: always
web:
build: .
volumes:
- "/app"
ports:
- "8000:8000"
links:
- database
depends_on:
- database
When I'm trying to install mysql in my archlinux container, mysql won't create a service at /etc/init.d/mysql or event a socket file at /run/mysqld/mysqld.sock. I get this output when I try to use mysql in my container terminal.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory")
Here are my docker files:
database/Dockerfile
FROM mysql:latest
RUN mkdir -p /db
WORKDIR /db
COPY . /db
RUN cd /db
RUN /etc/init.d/mysql start
Dockerfile
FROM base/archlinux
RUN mkdir -p /app
WORKDIR /app
COPY . /app
RUN pacman -Syu --noconfirm
RUN pacman -S openssh composer php mariadb --noconfirm
RUN groupadd -g 89 mysql &>/dev/null
RUN useradd -u 89 -g mysql -d /var/lib/mysql -s /bin/false mysql &>/dev/null
#RUN chown -R mysql:mysql var/lib/mysql &>/dev/null
RUN mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
RUN COMPOSER_ALLOW_SUPERUSER=1
RUN cd /app
RUN /usr/bin/composer install
RUN echo APP_KEY= > .env
RUN echo DB_USERNAME=root >> .env
RUN echo DB_PASSWORD= >> .env
RUN cat php.ini > /etc/php/php.ini #my custom configuration for the project
RUN php artisan key:generate
RUN ln -srf storage/app/public public/storage
RUN php artisan storage:link
VOLUME ["/app"]
EXPOSE 8000
CMD php /app/artisan serve --host=0.0.0.0 --port=8000
I don't feel like I'm doing the proper thing, anyone can help me or show me the right way?
This repository on GitHub is an ArchLinux based image for docker, it includes PHP, PHP-composer, MySQL, PostgreSQL, and MariaDB. I just had to tweak it a bit to make it work and suits my needs!

mkdir error: Cannot create directory '' - when trying to start mysql container with docker-compose up

I'm having a baffling time with docker-compose and mysql. When I try to start my mysql container with docker-compose, I get an uninformative mkdir error. My Dockerfile and docker-compose.yml don't even seem to contain a mkdir command for mysql.
Here is configuration:
docker-compose.yml
db:
build: docker/mysql
volumes:
- ./database:/var/lib/mysql
environment:
- MYSQL_DATABASE=xxxx
- MYSQL_USER=xxxx
- MYSQL_PASSWORD=xxxx
- MYSQL_ROOT_PASSWORD=xxxx
privileged: true
hostname: "docker.mysql"
Dockerfile
FROM mysql:5.6
MAINTAINER "ABC" <developers#abc.com>
ADD my.cnf /etc/mysql/my.cnf
RUN usermod -u 1000 mysql
RUN usermod -G staff mysql
RUN chown mysql /var/run/mysqld
EXPOSE 3306
my.cnf pastebin
http://pastebin.com/iVSGxhGV
sudo docker --version
Docker version 1.10.0, build 590d5108
This is a docker setup built by my team mates, so I am not sure what the priviledged:true line is doing, but when I remove it, everything appears to work.
with privileged:true
sudo docker-compose up db
Creating appname_db_1
Attaching to appname_db_1
db_1 | mkdir: cannot create directory '': No such file or directory
appname_db_1 exited with code 1
Once privileged: true is removed, it seems to work.
Does anyone know why this might be?