user2399453
user2399453

Reputation: 3081

Docker container exec errors out

I have a Dockerfile that builds a SQL Server image. I am copying a SQL create table script into the image and an execs.sh that invokes this SQL script using docker exec -it "execs.sh".

What I am seeing is that it errors out if I invoke execs.sh using "docker exec -it" but if I shell into the container and run execs.sh it works. What am I doing wrong?

Dockerfile:

FROM microsoft/mssql-server-linux
COPY create_tables.sql .
COPY execs.sh .
# set environment variables
ENV MSSQL_SA_PASSWORD=P@ssw0rd
ENV ACCEPT_EULA=Y    

execs.sh contents (this gets invoked from docker exec -it):

echo "Executing SQL scripts"
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd' -i ./create_tables.sql

Script build container, run it, and execute commands:

docker build -t mssql_test .
docker run -p 1433:1433 --name mssql2 -d mssql_test
docker exec -it mssql2 "/execs.sh"
docker exec -it mssql2 "ls"

Output:

OCI runtime exec failed: exec failed: container_linux.go:349: starting container process 
caused "exec format error": unknown
bin       create_tables.sql  execs.sh    lib    mnt   root  srv  usr
boot          dev            home    lib64  opt   run   sys  var
etc          install.sh  media  proc  sbin  tmp

Upvotes: 1

Views: 1299

Answers (2)

BMitch
BMitch

Reputation: 263636

You're missing an interpreter at the top of the script. When you run that from a shell like bash or sh, it assumes you want to run it as a shell script with the current shell. But when you run it with an OS exec there's no shell assuming how you want to run it. Therefore you want the following in the first line of your script:

#!/bin/sh

And be sure that's done with Linux linefeeds, not Windows linefeeds, else it will give you a file not found.

Upvotes: 2

PassionateDeveloper
PassionateDeveloper

Reputation: 15138

The problem is, that the command which your are try to force when the docker container starts (in your case: ./create_tables.sql) will be run at the same second the docker starts.

The SQL Server hasnt started yet, so the sql file could not even run against this service.

You have to work with either a great waiting time within your script or build a loop (e.g. check every second if the SQL server is reachable 100 times before exit).

Upvotes: 0

Related Questions