docker mysql, send sql commands during exec - docker

i am creating a mysql 5.6 docker using bash script and i would like to change the password.
how can i send sql commands from bash to docker?
build:
sudo docker build -t mysql-5.6 -f ./.Dockerfile .
run.sh:
#!/bin/bash
sudo docker run --name=mysql1 -d mysql-5.6
sudo docker exec -it mysql1 mysql -uroot -p$base_password \
<<< SET PASSWORD FOR 'root'#'localhost' = PASSWORD('new_pass');

You need to bind MySQL-port like descriped here. To keep the port 3306 you can just expose it on your host the following way:
sudo docker run --name=mysql1 -p 3306:3306 -d mysql-5.6
After that you should be able to use mysql -u USER -p PASSWORD on your local host. This will then allow you to send commands to your docker-container.

Related

Problem running mariadb dump in docker container

I got a MariaDB database dump from a colleague, and he asked me to run it in a docker container.
So i executed the following:
docker pull mariadb:10.4.26
then created the container
docker run --name test_smdb -e MYSQL_ROOT_PASSWORD=<some_password> -p 3306:3306 -d mariadb:10.4.26
then connected to the container:
docker exec -it test_smdb mariadb --user root -p<some_password>
and created a database in it from the mariadb prompt:
MariaDB [(none)]> CREATE DATABASE smdb_dev;
So far, so good. But when i tried to import the dump into it via this command:
docker exec -i test_smdb mariadb -uroot -p<some_password> --force < C:\smdb-dev.sql
i get a lot of lines like
ERROR 1046 (3D000) at line 22: No database selected.
So i am not sure what exactly is the issue?
Should i define a database, in which the dump should be imported? If yes - how exactly, because i look at different pages, like:
https://hub.docker.com/_/mariadb, especially this:
$ docker exec -i some-mariadb sh -c 'exec mariadb -uroot -p"$MARIADB_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql
and i see no database mentioned here.
Or
2) The colleague has not created the dump in the correct way?
I do not use mariadb in a docker environment at my place, but I do use mariadb on a linux machine so it should be really similar.
You said you used this command :
docker exec -i test_smdb mariadb -uroot -p<some_password> --force < C:\smdb-dev.sql
If we breakthrough it :
docker exec -i test_smdb docker stuff where you ask docker to execute the following command on the test_smdb container (or close to it, I'm not a docker daily user).
mariadb -uroot -p<password> --force here is the interesting part. You ask your shell to open mariadb and login as root with then given password with an extra flag --force. But you never specify which database should be overridden.
In my gist, again for mariadb outside docker but I really think it should be the same, I've the following command mariadb -uusername -p<password> <DB_NAME> < /path/to/file.sql
So I would try something like :
docker exec -i test_smdb mariadb -uroot -p<some_password> smdb_dev --force < C:\smdb-dev.sql
Below command should work I believe
docker exec -i test_smdb sh -c "exec mariadb -uroot -pPASSWORD smdb_dev" < /some/path/on/your/host/all-databases.sql
Reference: Import local database to remote host Docker container

can't control terminal after using the "-t" option in Docker

I am working through the Docker tutorial from here: https://docs.docker.com/language/python/develop/
I have done the first few steps from the tutorial to create a MYSQL container:
$ docker volume create mysql
$ docker volume create mysql_config
$ docker network create mysqlnet
$ docker run --rm -d -v mysql:/var/lib/mysql \
-v mysql_config:/etc/mysql -p 3307:3306 \
### (note: I used 3307:3306 here instead of the specified 3306:3306 because port 3306 was already being used on my machine) ###
--network mysqlnet \
--name mysqldb \
-e MYSQL_ROOT_PASSWORD=p#ssw0rd1 \
mysql
I then followed the next step to check if the mysql container was running:
$ docker exec -ti mysqldb mysql -u root -p
the terminal then prompts:
Enter password:
and I am unable to enter anything. no commands seem to work. ctrl+C didn't even register. the only thing I could do was kill the terminal itself. I am very confused since I have been following the documentation very closely though I am sure it's something very dumb.
Thanks in advance!
Try:
$ docker exec -ti mysqldb bash
then:
mysql -u root -p
I think you can connect with shell first

cannot run container after commit changes

Just basic and simple steps illustrating what I have tried:
docker pull mysql/mysql-server
sudo docker run -i -t mysql/mysql-server:latest /bin/bash
yum install vi
vi /etc/my.cnf -> bind-address=0.0.0.0
exit
docker ps
docker commit new_image_name
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret -d new_image_name
docker ps -a STATUS - Exited (1)
Please let me know what I did wrong.
Instead of trying to modify an existing image, try and use (for testing) MYSQL_ROOT_HOST=%.
That would allow root login from any IP. (As seen in docker-library/mysql issue 241)
sudo docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_ROOT_HOST=% -d mysql/mysql-server:latest
The README mentions:
By default, MySQL creates the 'root'#'localhost' account.
This account can only be connected to from inside the container, requiring the use of the docker exec command as noted under Connect to MySQL from the MySQL Command Line Client.
To allow connections from other hosts, set this environment variable.
As an example, the value "172.17.0.1", which is the default Docker gateway IP, will allow connections from the Docker host machine.

Execute docker command with deis

I have deis(1.5.2) with 3 host and I want "app" with database. I want to use postgres, so I found this docker image https://registry.hub.docker.com/_/postgres/ . I did deploy without problems, but I don't know how can I connect into this app/container (create some db, users) and link with other app/container. They write commands for it but it's for docker. So how can I run these commands from deis:
docker run --name some-app --link some-postgres:postgres -d application-that-uses-postgres
docker run -it --link some-postgres:postgres --rm postgres sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
or do you have some other solution for using DB with deis?

Cannot access tomcat8 server running in docker container from host machine

I am trying to connect to a web app running on tomcat8 in a docker container.
I am able to access it from within the container doing lynx http://localhost:8080/myapp, but when I try to access it from the host I only get HTTP request sent; waiting for response.
I am exposing port 8080 in the Dockerfile, I am using sudo docker inspect mycontainer | grep IPAddress to get the ip address of the container.
The command I am using to run the docker container is this:
sudo docker run -ti --name myapp --link mysql1:mysql1 --link rabbitmq1:rabbitmq1 -e "MYSQL_HOST=mysql1" -e "MYSQL_USER=myuser" -e "MYSQL_PASSWORD=mysqlpassword" -e "MYSQL_USERNAME=mysqlusername" -e "MYSQL_ROOT_PASSWORD=rootpassword" -e "RABBITMQ_SERVER_ADDRESS=rabbitmq1" -e "MY_WEB_ENVIRONMENT_ID=qa" -e "MY_WEB_TENANT_ID=tenant1" -p "8080:8080" -d localhost:5000/myapp:latest
My Dockerfile:
FROM localhost:5000/web_base:latest
MAINTAINER "Me" <me#my_company.com>
#Install mysql client
RUN yum -y install mysql
#Add Run shell script
ADD run.sh /home/ec2-user/run.sh
RUN chmod +x /home/ec2-user/run.sh
EXPOSE 8080
ENTRYPOINT ["/bin/bash"]
CMD ["/home/ec2-user/run.sh"]
My run.sh:
sudo tomcat8 start && sudo tail -f /var/log/tomcat8/catalina.out
Any ideas why I can access it from within the container but not from the host?
Thanks
What does your docker run command look like? You still need to do -p 8080:8080. Expose in the dockerfile only exposes it for linked containers not to the host vm.
I am able to access the tomcat8 server from the host now. The problem was here:
sudo tomcat8 start && sudo tail -f /var/log/tomcat8/catalina.out
Tomcat8 must be started as a service instead:
sudo service tomcat8 start && sudo tail -f /var/log/tomcat8/catalina.out
Hit given command to find IP address of docker-machine
$ docker-machine ls
The output will be like :
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v1.10.3
Now run your application from host machine as : http://192.168.99.100:8080/myapp

Resources