DhiwaTdG
DhiwaTdG

Reputation: 788

Access a service from one container to other

I'm following a docker web application setup tutorial from here. As described in the docker-compose.yml file the MySQL container is linked to the app and it is accessible via both index.php and phpMyAdmin. No problem so far.

When I opened an interactive shell for my application using docker exec -it container-id bash and tried to access the MySQL service but I couldn't. What I'm I missing here?

Edit1: When I type MySQL, I get bash: mysql: command not found.

Edit2: Output of docker ps

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                  NAMES
1643f0ba5637        dockertutorial_php      "docker-php-entryp..."   9 seconds ago       Up 6 seconds        9000/tcp               dockertutorial_php_1
610f2c8bf4c4        phpmyadmin/phpmyadmin   "/run.sh phpmyadmin"     9 seconds ago       Up 6 seconds        0.0.0.0:8080->80/tcp   dockertutorial_phpmyadmin_1
29c552da473b        mysql:latest            "docker-entrypoint..."   10 seconds ago      Up 8 seconds        3306/tcp               dockertutorial_mysql_1

Edit3: Perhaps I should have started with this. I apologise for all the confusion caused/would cause (Especially to @Jay Blanchard & @mkasberg). I want to run a bash script inside the nginx container which creates a MySQL database. It works if I have installed MySQL in the nginx container but not if it is a separate container. Is it possible to achieve with the current approach? If not how should I correct it?

Upvotes: 1

Views: 493

Answers (1)

mkasberg
mkasberg

Reputation: 17292

mysql: command not found means that the mysql client binary is not available in whichever container you're logged in to. I suspect you're running docker exec on a container other than the MySQL one. The binary would be there in the MySQL container. While those other containers can connect do MySQL, they don't have the MySQL command line application available (to keep image size small).

Make sure you're connecting to the MySQL container with your docker exec command and it should work. In fact, docker-compose provides a way for you to do this. With your services running (after doing docker-compose up), try this:

$ docker-compose exec mysql /bin/bash

That should get you to a bash prompt in the mysql container. Before, you were getting to a bash prompt in the nginx container. Which can't run mysql on the command line.

Upvotes: 1

Related Questions