Docker Community Forums

Share and learn in the Docker community.

Same command works in terminal but not in a bash script


(Tim Uckun) #1

I have the following script

#!/bin/bash
set -x
docker stop ${MYSQL_CONTAINER_NAME}
docker rm ${MYSQL_CONTAINER_NAME}
docker run \
--detach \
--name ${MYSQL_CONTAINER_NAME} \
--env MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} \
--publish 3306:3306 \
 mysql:8

MYSQL_PORT_3306_TCP_ADDR=`docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${MYSQL_CONTAINER_NAME}`
MYSQL_PORT_3306_TCP_PORT=3306
docker exec -i ${MYSQL_CONTAINER_NAME} mysql -uroot  -p${MYSQL_ROOT_PASSWORD} -e "create database ${MYSQL_DATABASE_NAME}"

When I run this script I get the following error

mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

But after the script completes if I run the command in my terminal on the now running container it works just fine.

I also tried -h127.0.0.1 but that didn’t work either.

What is going on here. I don’t have any mysql related env vars in my shell so that can’t be it.

Using MacOSX


(Valkyriestudios) #2

There’s a couple of ideas that I have that could be going wrong here.

1 - Do you have a global configuration for bash scripts that kills a bash script after a warning pops up ? If so the warning from mysql will have killed the script when you tried running your last docker exec statement.

2 - Could you adjust the bash script to print out the value of MYSQL_CONTAINER_NAME before you call docker stop on it ? If this doesn’t contain anything then that’s the issue, in this case you could either pass the required variables to the shell script and create local variables, or define them hardcoded in the script itself ( your pick :slight_smile: ).

Another solution is to pipe the warning of the mysql container to the unix null device by using >> 2>&1 >/dev/null

Hope this helps!

Using ArchLinux


(Tim Uckun) #3

Do you have a global configuration for bash scripts that kills a bash script after a warning pops up ? If so the warning from mysql will have killed the script when you tried running your last docker exec statement.

No. All I have is set -x

Could you adjust the bash script to print out the value of MYSQL_CONTAINER_NAME before you call docker stop on it ?

The set -x prints out the commands so I know that it’s calling the right container.

The thing is that it’s actually connecting to the container and calling mysql client on it, The client is complaining it can’t reach the mysql in the container.

Makes no sense.


(Sam) #4

did u enable the mysql container to accept incoming requests from all IP addresses…
by default mysql ONLY allows connections from application on the same environment
container to container
docker host to docker host


(Tim Uckun) #5

Shouldn’t calling the mysql command via docker exec work though? it says it can’t reach the socket but the docker log shows it’s listening on the socket.

Also if I map a port to the container it should listen right?


(Sam) #6

there are two sides to the mysql conversation

if you map and try to access from the docker host(different ip address)

server (mysqld) and client (mysql)

the mysqld (server) by default will listen for connections ONLY in its environment (inside the container)
or inside the docker host…

unless YOU explicitly change that…

you can demonstrate the same problem on a normal system…

on machine A, start mysqld… in a command shell, use mysql and connect. all is good… (localhost:3306)

now go to another machine, and use mysql to connect to the same database you JUST connected to on machine A (machineA:3306…)

boom. you will not be able to connect… cause mysql is listening only for local connections, not over the network. and the user connect request MUST come from the authorized machine…

see https://stackoverflow.com/questions/10236000/allow-all-remote-connections-mysql


(Sam) #8

I just copied your script and ran it here… and got the same error… so I docker attached to the container,
and mysql is not up yet…

it took about 40 seconds…

the image runs ./usr/local/bin/docker-entrypoint.sh on startup which takes a while…


(Valkyriestudios) #9

And i’m guessing that it runs async as a subprocess ? Meaning the command in the script will probably have already completed while in fact the subcomponents where still installing ?


(Sam) #10

well, its a script… so something is running… the entrypoint command CANNOT return.
when it does, the container shuts down.

how long it takes to get to ready state depends on the design…

i have an image that unbundles a big (1.3gig) tar file in its startup… then launches the code, in the folder created by the unbundle…

takes time to get going


(Tim Uckun) #11

What’s a good strategy for waiting for a container to be ready? Should I just poll the mysql until I get a non error response?


(Sam) #12