afzalex
afzalex

Reputation: 8652

Redirect host output to docker container

I have a mysql container running in docker container, which is intentionally not accessible through any port, but is accessible to other docker container.

For example consider following mysql container

docker run --name test_db mysql

Now I want to run a dump file in that mysql that is present on my host machine.

cat mydumpfile.sql | docker exec -it test_db mysql --password=${MY_MYSQL_PASSWORD}

But I am getting following in response

the input device is not a TTY

How to pipe output from my host machine to docker container?

Please notice one thing, it is not about error, by removing -it or -i, I don't get error, but I am not able to pipe output to docker container.

The question is how to pipe any output from host to docker container.

Error "The input device is not a TTY" -- that doesn't solve the problem.

Upvotes: 0

Views: 144

Answers (1)

Mihai
Mihai

Reputation: 10757

This is a working example:

script.sql

CREATE DATABASE test;

use test;

create table test (
    id varchar(64) not null,
    name varchar(64) not null default ""
);

create container:

docker run --name mysql -e MYSQL_ROOT_PASSWORD=test -d mysql

run the sql script:

docker exec -i mysql mysql -uroot -ptest < script.sql

login and check the new database was created:

docker exec -ti mysql \
  mysql -uroot -ptest -e"show databases; use test; show tables; desc test;"

cleanup

docker container rm -f mysql

Upvotes: 2

Related Questions