I created a build on my local machine and was working great. Now
trying to do same in the server.It is not passing the host-ip set in
Dockerfile but passing 172.17.0.3 and throwing the bellow error
Here is the error:
Step 22/25 : RUN cd /usr/html/bin && ./magento setup:config:set --db-host=172.17.0.2 --db-name=mydb --db-user=tara --db-password=password
---> Running in 7bbe53f5d054
SQLSTATE[HY000] [1045] Access denied for user 'tara'#'172.17.0.3' (using password: YES)
[InvalidArgumentException]
Parameter validation failed
*Where the host ip comes from the IP address of another running container. which is 172.17.0.2 *
Why is it taking wrong IP address during connection
HERE is the dockerfile:
FROM docker-php:0.2
#mysqql setup
ENV DB_HOST 172.17.0.2
ENV DB_NAME mydb
ENV DB_USER admin
ENV DB_PASSWORD password
#magenot admin user
ENV ADMIN_USER tara
ENV ADMIN_PASSWORD password123
ENV ADMIN_FIRSTNAME tara
ENV ADMIN_LASTNAME gurung
ENV ADMIN_EMAIL tara.email#somelink.net
ADD ./magento2 /usr/html/
WORKDIR /usr/html/
RUN find var vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \;
RUN find var vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} \;
RUN chown -R :docker .
RUN chmod u+x bin/magento
RUN composer install
CMD echo "Success installed Magento"
ENTRYPOINT ["/home/docker/run.sh"]
#get into the magento isntallation dir
#get into the magento isntallation dir
#start setting the necessasry file permission first
RUN cd /usr/html && \
chmod 777 generated -R && \
chmod 777 var -R
RUN cd /usr && \
chown -R docker:docker html
#start running the magento commands to install
RUN cd /usr/html/bin && \
./magento setup:config:set --db-host=$DB_HOST --db-name=mydb --db-user=bhavi --db-password=password
#installing the magento with admin user created
RUN cd /usr/html/bin && \
./magento setup:install --admin-user=$ADMIN_USER --admin-password=$ADMIN_PASSWORD --admin-firstname=$ADMIN_FIRSTNAME --admin-lastname=$ADMIN_LASTNAME --admin-email=ADMIN_EMAIL --use-rewrites=1
RUN chmod 777 vendor -R
RUN cd /usr && \
chown -R docker:docker html
Related
FROM mysql:latest
RUN set -ex && apt-get update \
&& apt-get dist-upgrade -y \
&& apt-get install -y --no-install-recommends \
curl unzip
ADD my.sh /docker-entrypoint-initdb.d
RUN chown -R mysql:mysql /docker-entrypoint-initdb.d/
RUN chmod -R 777 docker-entrypoint-initdb.d
Content of my.sh file are :
#!/bin/bash
mkdir try1
The my.sh file has a command to create a directory and when the entrypoint.sh of the image runs this file, it throws a permission denied error. I have changed the owner of the directory as well.
What could be the reason.
Script in docker-entrypoint-initdb.d will be executed by mysql account, not by root, see source code.
You could verify it by change my.sh as next:
#!/bin/bash
echo "start"
pwd
id
mkdir try1
echo "done"
Then, the error log will be:
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/my.sh
start
/
uid=999(mysql) gid=999(mysql) groups=999(mysql)
mkdir: cannot create directory 'try1': Permission denied
You could see the user which run current my.sh is mysql, while you currently want to setup try1 folder in /, surely you won't have permission.
I am trying to create a customer mysql image from base image as alpine. I have installed the necessary packages for mysql via apk through dockerfile. My requirement is i want to run a fresh instance of mysql_alpine and create and modify tables in the db. My data files are located in /var/lib/mysql/ mounted as a volume and my init.sql is mounted as a volume into /docker-entrypoint-initdb.d/. Following is my dockerfile
FROM alpine
RUN apk update && \
apk --no-cache add mysql mysql-client && \
addgroup mysql mysql && \
mkdir docker-entrypoint-initdb.d && \
mkdir run/mysqld && \
chown -R mysql:mysql /run/mysqld && \
chown -R mysql:mysql /docker-entrypoint-initdb.d && \
chown -R mysql:mysql /var/lib/mysql && \
chmod -R +x /docker-entrypoint-initdb.d/ && \
mysql_install_db --user=mysql
ENV "MYSQL_ROOT_PASSWORD= " \
"MYSQL_DATABASE=mysql" \
"MYSQL_USER=test" \
"MYSQL_PASSWORD=mypassword"
VOLUME ~/mysql/init/:/docker-entrypoint-initdb.d/ && \
~/mysql/db/:/var/lib/mysql/
CMD /usr/bin/mysqld --user=mysql
After i build the container when i run the conatiner with the command:
docker run -d -p 3307:3306 --name mysql_alpine mysql_alpine
only the db starts but there is no init.sql file inside docker-entrypoint-intidb.d/ folder and so, the file never got loaded when initializing the db
My init.sql file which is not getting mounted into docker-entrypoint-initdb.d folder inside the container:
CREATE DATABASE IF NOT EXISTS mysql;
USE mysql;
CREATE USER 'test'#'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL ON my_db.* TO 'testdata'#'localhost';
FLUSH PRIVILEGES;
First of all, you need to fix your volume section, as :
VOLUME ~/mysql/init/:/docker-entrypoint-initdb.d/ && \
~/mysql/db/:/var/lib/mysql/
is not correct, check the documentation, instead you should use COPY instead:
COPY mysql/init/ /docker-entrypoint-initdb.d/
COPY mysql/db/ /var/lib/mysql/
And you need to make sure that mysql directory is on the same level as your Dockerfile.
Does anybody knows how to install mysql-server via dockerfile? I have written a Dockerfile, but the build ends with an error: /bin/sh: 1: /usr/bin/mysqld: not found
USER root
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server-5.7
# Remove pre-installed database
RUN rm -rf /var/lib/mysql/*
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/"/etc/mysql/my.cnf
ENV DB_USER example
ENV DB_PASSWORD example
ENV DB_NAME example
ENV VOLUME_HOME "/var/lib/mysql"
EXPOSE 3306
RUN cp /etc/mysql/my.cnf /usr/share/mysql/my-default.cnf
RUN /usr/bin/mysqld && sleep 5 && \
mysql -uroot -e "CREATE USER '${DB_USER}'#'%' IDENTIFIED BY '${DB_PASSWORD}'" && \
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '${DB_USER}'#'%' WITH GRANT OPTION" &&\
mysql -uroot -e "CREATE DATABASE ${DB_NAME}" && \
mysqladmin -uroot shutdown
For an ubuntu:16.04 base image, mysqld is found in /usr/sbin, not /usr/bin
If you can add a step RUN which mysqld before your final RUN command that will show you where the mysqld executable is found. It may vary depending on which base image/distro you're using.
You can also use RUN mysqld ... without a full path, if the file is in your $PATH
You may also need to update your RUN sed line as below, adding spaces around the quoted string:
RUN sed -i -e "s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/" /etc/mysql/my.cnf
Otherwise, you may see the following error:
The command '/bin/sh -c sed -i -e"s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/"/etc/mysql/my.cnf' returned a non-zero code: 1
I am trying to access mysql databases from my docker host to the container.
It's my own dockerfile which install a database expose on port 3306.
I launch my docker with docker-compose, and my compose file is mapping 3308 host port on 3306 container port.
I can access to mysql from the host like this :
mysql -h localhost -P 3308 -u root -pMyPassword
It's working well, but what I can't figure out, is why I can't see any datas from my container?
From inside the container, I have a test databases which I can connect to without any problem. But when I connect from the host to the container mysql process, It seems to show me the mysql datas from the host machine, not from the container one.
Any ideas?
Thanks :)
EDIT 1 :
So here is the first way I can connect to mysql into the container :
docker exec -it MyContainer mysql -uroot -pMyPassword
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test_db |
+--------------------+
It show me my db : test_db
But If i access from :
mysql -h localhost -P 3308 -u root -pMyPassword
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
My test_db isn't here.
And the result of docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0de6a691d72 MyContainer "docker-entrypoint.sh" 3 hours ago Up 3 hours 9000/tcp, 0.0.0.0:8085->80/tcp, 0.0.0.0:3308->3306/tcp, 0.0.0.0:8084->8000/tcp, 0.0.0.0:8086->8080/tcp MyContainer
EDIT 2 :
I am developing a standard docker container for web hosting production environnement. Each host is controlled by ajenti. The host work with an nginx reverse proxy which redistribute websites on correct container. Every thing is wokring well. So here is my Dockerfile :
FROM php:5.6-fpm
RUN apt-get update && apt-get install -y --no-install-recommends \
git \
libxml2-dev \
python \
build-essential \
make \
gcc \
python-dev \
locales \
python-pip
RUN dpkg-reconfigure locales && \
locale-gen C.UTF-8 && \
/usr/sbin/update-locale LANG=C.UTF-8
ENV LC_ALL C.UTF-8
ARG MYSQL_ROOT_PASSWORD
RUN export DEBIAN_FRONTEND=noninteractive; \
echo mysql-server mysql-server/root_password password $MYSQL_ROOT_PASSWORD | debconf-set-selections; \
echo mysql-server mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD | debconf-set-selections;
RUN apt-get update && apt-get install -y -q mysql-server php5-mysql
RUN rm /etc/apt/apt.conf.d/docker-gzip-indexes
RUN apt-get update && apt-get install -y wget
RUN wget http://repo.ajenti.org/debian/key -O- | apt-key add -
RUN echo "deb http://repo.ajenti.org/debian main main debian" > /etc/apt/sources.list.d/ajenti.list
RUN apt-get update && apt-get install -y ajenti cron unzip ajenti-v ajenti-v-php-fpm ajenti-v-mysql ajenti-v-nginx
RUN apt-get install -y python-setuptools python-dev \
&& easy_install -U gevent==1.1b3 \
&& sed -i -e s/ssl_version=PROTOCOL_SSLv3/ssl_version=PROTOCOL_SSLv23/ /usr/local/lib/python2.7/dist-packages/gevent-1.1b3-py2.7-linux-x86_64.egg/gevent/ssl.py
EXPOSE 80 8000 8080 3306
RUN mkdir /tmp/tempfiles \
&& mv /srv /tmp/tempfiles \
&& mv /var/lib/mysql /tmp/tempfiles \
&& mv /var/log /tmp/tempfiles \
&& mv /etc/ajenti /tmp/tempfiles
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s /usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]
As I said, I wanted to be able do deploy a new container easily. So I created a docker-entrypoint.sh which copy wanted files to my volume when I start the container :
#!/bin/bash
DIR="/var/lib/mysql"
# look for empty dir
if [ ! "$(ls -A $DIR)" ]; then
cp -avr /tmp/tempfiles/mysql /var/lib/
fi
# rest of the logic
DIR="/srv"
# look for empty dir
if [ ! "$(ls -A $DIR)" ]; then
cp -avr /tmp/tempfiles/srv /
fi
# rest of the logic
DIR="/var/log"
# look for empty dir
if [ ! "$(ls -A $DIR)" ]; then
cp -avr /tmp/tempfiles/log /var/
fi
# rest of the logic
DIR="/etc/ajenti"
# look for empty dir
if [ ! "$(ls -A $DIR)" ]; then
cp -avr /tmp/tempfiles/ajenti /etc/
fi
# rest of the logic
Finally, my docker-compose.yml to launch everything and map ports :
version: '2'
services:
ajenti:
build:
context: ./dockerfiles/
args:
MYSQL_ROOT_PASSWORD: MyPassword
volumes:
- ./logs:/var/log
- ./html:/srv
- ./ajenti:/etc/ajenti
- ./mysql-data:/var/lib/mysql
- ./apache2:/etc/apache2
ports:
- "8084:8000"
#NGINX
- "8085:80"
#APACHE
- "8086:8080"
- "3308:3306"
Hope this will help to find a solution !
I finally found a solution and it was pretty simple...
First of all, I need to let mysql bind external address, so I changed the line bind-address to '0.0.0.0' inside the container.
Next I just changed the command line with mysql -h 127.0.0.1 -P 3308 -u root -pMyPassword
Now it's fine, I can access container mysql data from the host.
Thanks all for your help :)
In my case I was confused because docker used a different host and port. So you need to find them then do this:
mysql -P <portnumber> -h <host IP> -u db_name -p
Most people would put the docker DB related variables into the environment of the docker container so do this:
sudo docker exec -it container_name env
See if there's a variable called DB_HOST or DB_PORT or something like that. If not then look thru the source code. If it's a PHP project then find a config directory and look in main.php and see
if you execute MySQL operation as entrypoint in the dockerfile file, you will only see that operation when you connect to the container. try changing the entrypoint.
https://docs.docker.com/engine/reference/builder/#entrypoint
Error while executing ansible ping module
bash ~ ansible webservers -i inventory -m ping -k -u root -vvvv
SSH password:
<~> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO ~
<my-lnx> ESTABLISH CONNECTION FOR USER: root on PORT 22 TO my-lnx
~ | FAILED => FAILED: [Errno 8] nodename nor servname provided, or not known
<my-lnx> REMOTE_MODULE ping
<my-lnx> EXEC /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1423302966.66-77716810353582 && echo $HOME/.ansible/tmp/ansible-tmp-1423302966.66-77716810353582'
<my-lnx> PUT /var/folders/8n/fftvnbbs51q834y16vfvb1q00000gn/T/tmpP6zwZj TO /root/.ansible/tmp/ansible-tmp-1423302966.66-77716810353582/ping
<my-lnx> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1423302966.66-77716810353582/ping; rm -rf /root/.ansible/tmp/ansible-tmp-1423302966.66-77716810353582/ >/dev/null 2>&1'
my-lnx | FAILED >> {
"failed": true,
"msg": "Error: ansible requires a json module, none found!",
"parsed": false
}
This is my inventory file
bash ~ cat inventory
[webservers]
my-lnx ansible_ssh_host=my-lnx ansible_ssh_port=22
I have installed simplejosn module also in the client as well as remote machine
bash ~ pip list | grep json
simple-json (1.1)
simplejson (3.6.5)
I think you need to install the python-simplejson module.
Try to run this command first and then your desired commands:
ansible webservers -i inventory -m raw -a "sudo yum install -y python-simplejson" -k -u root -vvvv
I am supposing that its old Red Hat/CentOS system.
If you don't want or can't install the python-simplejson module on remote servers, you can simply request the raw output instead:
> ansible webservers -i inventory -m ping -m raw
Or like I did, added it to my ~/.bash_profile
alias ansible="ansible -m raw"
# And then simply running:
> ansible webservers -i inventory -m ping
in centos 5.* version no python-simple json available on repo to donwload and install. you can simple use below mentioned method.
make sure both the source and destination should be accessed password less and from source to destination also password less.
use ssh-keygen -t rsa to generate key
ssh-copy-id user#host_ip
"---
- hosts: (ansible host)
become: yes
remote_user: root
gather_facts: false
tasks:
- name: copying copying temps
shell: ssh (source) && rsync -parv /root/temp/* root#(Destination):/root/temp/"