0

I am just getting into docker and have a simple mysql 5.5 set up using the official image. Here are my relevant files.

#Dockerfile
FROM mysql:5.5                                                                                                                             
ENV MYSQL_ROOT_PASSWORD password

#docker-compose.yml
db:                                                                                                                                        
  build: .

Now I run docker-compose build followed by docker-compose up which gives the following output

db_1 | 150828 14:04:17 [Note] mysqld: ready for connections.
db_1 | Version: '5.5.45'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)

Now in another terminal I run docker-compose run db bash and get into the bash shell. But in this shell if I try to connect the mysql client I get an error.

root@94a43b1f5be0:/# mysql -u root -p  

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

The same thing works with plain old docker. Example(Using the same Docker file):

docker build -t my-new-image .
docker run --name my-new-image-container -d my-new-image
docker exec -it my-new-image-container bash
root@8408282d162f:/# mysql -u root -p
mysql>

What am I doing wrong with docker-compose. How can I connect to my running mysql container and fire some queries. Any help would be appreciated.

1 Answer 1

2

Using docker-compose run actually starts up a new container, rather than connecting to the existing one. In this case, you've overridden the command with bash, so the mysql server hasn't started.

What you can do instead is just docker exec into the container given by docker-compose ps.

BTW, you don't need to build your own image here; you could just set the password in an environment variable in the docker-compose.yml. This would also have the advantage of not baking the password into the image.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah... this makes sense.. Thanks for the quick response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.