dockerfile inheritance and RUN commands - mysql

I'm trying to create a dockerfile to build a LAMP server for development using the alpine-lamp base.
FROM glats/alpine-lamp
ENV MYSQL_ROOT_PASSWORD=password
RUN apk add wget && \
apk add php7-simplexml && \
mysql -u root -ppassword -e "CREATE DATABASE IF NOT EXISTS mydb"
When I run the docker image build -t testing . command to build my image I get an error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)
Is my mysql command being run before the "parent" dockerfile has completed running? I've been able to remove these lines and start the container, and then run the identical mysql command from the cli, so I'm thinking the mysql server part of the parent lamp stack just didn't get completely run. Is there a way to ensure my commands run after the parent?

At image build time, mysql will not be running. That is why you see the error message about not being able to connect.
This type of thing isn't something that should be run at build-time. Put the database initialization into an entrypoint script instead.

Related

MySQL docker image takes too long to start up the dbms server and socket. ERROR 2002 (HY000): Can’t connect to local MySQL server through socket

Issue type:
initialization bug with mysql:8 docker official image.
Versions:
Docker version 20.10.14, build a224086;
Linux Ubuntu 21.10 host machine OS;
Docker mysql:8 and mysql:oracle official image tags, both with same bug.
Steps to reproduce:
run mysql:8 docker official image from docker hub passing the needed args at the CLI env variables. Such as:
sudo docker run --name app-mysql-container -v [some-absolute-path-in-your-host-machine]:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=[some-root-password] -e MYSQL_DATABASE=[some-database-name] -e MYSQL_USER=[some-user-name] -e MYSQL_PASSWORD=[some-user-password] -d mysql:8
use another instance of the terminal to follow the logs live of the container just created:
sudo docker --follow logs app-mysql-container
then use at the other instance of the terminal:
sudo docker exec -it app-mysql-container bash , in order to get inside the container bash shell, and type:
mysql -u root -p
[type password]
ERROR HAPPENS CONTINUOUSLY until server finishes getting up - about 8 minutes !!
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)
after the 8 minutes, when the mysqld server finally gets ready for connection, with a ready socket connection at 3306 port, then everything starts working fine... both for access from inside the container (MySQL CLI) as from outside linked containers with applications connecting to the mysql server.
Eventually the MySQL server starts almost instantly, rather than after 8 minutes... something is wrong with this docker image...
Printscreens below:

Error : The command '/bin/sh returned a non-zero code: 1

When I am trying to build one of my projects by running a script written by previous team in my ubuntu 16.04
sudo ./build
I am getting error :
Step 8/24 : RUN service mysql start
---> Running in 3djjk653642d
* Starting MySQL database server mysqld
...fail!
The command '/bin/sh -c service mysql start' returned a non-zero code: 1
My Dockerfile looks like:
COPY schema.sql /tmp/schema.sql
### User with ALL accesses (winter/toor)
RUN service mysql start
RUN mysql < /tmp/schema.sql
RUN mysql -e "CREATE USER 'winter'#'%' IDENTIFIED BY 'toor'"
RUN service mysql start && mysql -e "GRANT ALL ON its.* TO 'winter'#'%'"
Please ,any help ?
RUN statements in a Dockerfile are used to run a command which will have some effect on the filesystem, that is then saved in another layer.
It's not normal to start a service like this, as the state of the memory (where the service is running) is not stored in the image, it can only be running in a running container.
The normal way to do stuff like this would be to write a bash script, (called start.sh, or something similar), copy it into the image and then run from an ENTRYPOINT / CMD line at the end of the Dockerfile. This will be run when the container is created in a docker run ... command
start.sh:
service mysql start
mysql < /tmp/schema.sql
mysql -e "CREATE USER 'winter'#'%' IDENTIFIED BY 'toor'"
service mysql start && mysql -e "GRANT ALL ON its.* TO 'winter'#'%'"
Dockerfile:
COPY schema.sql /tmp/schema.sql
COPY start.sh /
ENTRYPOINT ["/start.sh"]
Have a read here for some information on the difference between ENTRYPOINT & CMD and when each should be used.
Better still - use the official MySQL image from Docker hub. Through the use of environment variables, you could probably achieve all you require.
For me the error was:
yum -y install nginx' returned a non-zero code: 1
This docker file helped me:
FROM centos:7
MAINTAINER linuxtechlab
LABEL Remarks="This is a dockerfile example for Centos system"
RUN yum -y update
RUN yum -y install httpd
RUN yum clean all
RUN yum -y install nginx
EXPOSE 80
#ENV HOME /root
#WORKDIR /root
#ENTRYPOINT ["ping"]
#CMD ["google.com"]

Error when creating a MySQL database in a Dockerfile

I want to create a simple container that contains a MySQL server with an initialized database. My Dockerfile currently looks like this:
FROM ubuntu:16.10
RUN apt-get update
# install MySQL; root user with no password
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get install -y mysql-server mysql-client
RUN service mysql start
RUN mysqladmin -u root create mydb
CMD [ "bash" ]
However, when I build the image via docker build -t mysql-test . I get the following error:
Step 7 : RUN mysqladmin -u root create mydb
---> Running in a35c3d176d4f
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
The command '/bin/sh -c mysqladmin -u root create mydb' returned a non-zero code: 1
When I comment the line where I want to create the database (RUN mysqladmin -u root create mydb) I can build the image, start it, and execute the mysqladmin command from the command line:
# this works
> docker run -it mysql-test
root#...> service mysql start
root#...> mysqladmin -u root create mydb
Why do I get the error when creating the database in the Dockerfile? Thank you!
Reading the documentation of the RUN instruction makes it clear:
The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
So each run instruction creates a new image and starting the MySQL server and creating the database should be combined in the same RUN instruction:
RUN service mysql start && mysqladmin -u root create mydb
This solves it.

Connecting to Dockerized MySQL from remote client

tl;dr: database used to be connectable from remote, but after dockerizing it, it isn't (though i can access from host).
I've set up a docker container running MySQL in an ec2 instance, using the following command:
sudo docker run --name mysql-csm -p 3306:3306 -v /db/mysql:/var/lib/mysql -v /db/mysql-config:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=password -d mysql
where: -v /db/mysql:/var/lib/mysql maps my host's database to the docker
and
-v /db/mysql-config:/etc/mysql/conf.d maps my host custom config file to the dockerized MySQL config. the host file is supposed to take precedent, and my custom config contains one line:
[mysqld]
bind-address = 0.0.0.0
I am able to connect to the database via host command-line using the following:
sudo docker run -it --link mysql-csm:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -u"user" -p"pw"'
From the MySQL command-line, all of the tables, etc., are present & look good. This database ran in a regular MySQL version previously, and I could connect remotely.
However, when trying to connect from my laptop using MySQL Workbench, I receive the error:
Table 'performance_schema.session_variables' doesn't exist
Googling suggests it's something to do with upgrading, but I'm not sure how to address that.
ETA: I get the same error when trying to login thru SSH -> TCP/IP via MySQL Workbench.

How to make mysql and php work together and without show the database password

start mysql container
$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
Connect to MySQL and manually create new database
$ docker run -it --link mysql:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
link the database and start php container
$ docker run -d --link mysql:mysql --name myapp -v "$PWD":/var/www/html -p 80:80 php:5.6-apache
first question:
When access my php website: http://localhost/index.php, I got below error:
Fatal Error: Mysql is not supported in your PHP, recompile and try again.
Here is the configure command shows in phpinfo page, seems mysql module has been included in compile.
Configure Command './configure' '--with-config-file-path=/usr/local/etc/php' '--with-config-file-scan-dir=/usr/local/etc/php/conf.d' '--with-apxs2' '--disable-cgi' '--enable-mysqlnd' '--with-curl' '--with-openssl' '--with-readline' '--with-recode' '--with-zlib'
Are there anything missed in official php image?
second question:
When access http://localhost/info.php, I can see phpinfo page.
But it also shows database password in session "Environment":
MYSQL_ENV_MYSQL_ROOT_PASSWORD my-secret-pw
and in session "PHP Variables"
_ENV["MYSQL_ENV_MYSQL_ROOT_PASSWORD"] my-secret-pw
So how to hide the password in phpinfo()?
I assume you're trying to run phplist in a docker environment.
The message you're seeing (Fatal Error: Mysql is not supported in your PHP, recompile and try again.) is a phplist error message hardcoded in both the ./admin/mysql.inc and ./admin/mysqli.inc files.
This message is displayed upon check for the mysql_connect and mysqli_connect functions being present. You are seeing this message because the functions are not present in your environment.
You have to find out what package offers this functionality and either install it on your docker image, or build a new docker image with this support present.
The official PHP docker image is based on Debian, which offers the php5-mysql package. This is not present in the docker image, so you install this package using apt-get, then use docker-php-ext-install, docker-php-ext-configure and docker-php-ext-enable to enable the mysql and mysqli extensions.