3

Any attempt at this has failed. I have a large sql script in my laptop, and I want to run it in my docker container.

I believe I have to copy it first=:

docker cp ./my_cool_script.sql container_mssql_1:/my_cool_script.sql

This seems to run and doesn't give any success or failure so I assume its ok?

I then try:

docker exec container_mssql_1 -u sa psql databasename sa -f /my_cool_script.sql

but I just get:

rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:262: starting container process caused "exec: \"-u\": executable file not found in $PATH"

Any ideas, or is there a better way to do this. The file is quite large (30MB).

2
  • 1
    It would help if you could share the contents of your Dockerfile to get a better idea of the context, specifically from which base image you're building.... Rather than "manually" doing the cp step, you could add that to your Dockerfile (or a derivative Dockerfile). Commented Oct 25, 2017 at 10:23
  • this would be a 1 off that I would do locally to set up my local env, using mssql Commented Oct 27, 2017 at 14:21

4 Answers 4

2

You can do this with the sqlcmd utility that comes with the SQL Server image. You will indeed need to copy the script into the container first.

docker cp script.sql <container_id|container_name>:/tmp/

Then, use the sqlcmd utility inside the container.
docker exec -it <container_id|container_name> /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P <your_password> -i /tmp/script.sql

sqlcmd docs: https://learn.microsoft.com/en-us/sql/ssms/scripting/sqlcmd-run-transact-sql-script-files
Docker image: https://hub.docker.com/_/microsoft-mssql-server

Tested on image :2019-CU8-ubuntu-16.04 with a ~700mb script

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

Comments

2

https://docs.docker.com/engine/reference/commandline/exec/

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

The -u option needs to come before the name of the container.

docker exec -u sa container_mssql_1 psql databasename sa -f /my_cool_script.sql

That's why it was complaining "there's no executable on my PATH named -u". It thought you wanted to run a command called -u.

6 Comments

I get this: unable to find user sa: no matching entries in passwd file
looks like your original error is now solved, and now you have a new error. is your question "solve every error until my whole use-case works"? that's too broad for this forum. StackOverflow is for minimal, reproducible problems. it would be appropriate to open a new question for this next error, but only after demonstrating sufficient research (I say this because the new error — like the first one — is a really clear, simple error; please demonstrate that you actually tried to solve this problem independently).
ask yourself: why am I writing -u sa? do you expect a user named sa to exist? the error is telling you that there's no such user.
are you perhaps referring to a PostgreSQL user? docker exec -u refers to users within the container's Operating System, not PostgreSQL users.
Thanks - its MSSQL not postgres. I was trying to execute the sql using the sa account
|
0

When using docker exec you're ultimatively executing a command inside the container. The syntax for docker exec is

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

With the command you've used it sees "-u" as COMMAND, which is not an executable it can find in the $PATH.

I'm not an expert in mssql or psql, so I don't know what you're trying to achieve with -u sa before the psql command.

Comments

0

If you look at your output, you can see that the executable that docker is trying to run is -u, which corresponds to your attempt to set the user - essentially you've just got your docker command parameters in the wrong order!

Try putting the user option straight after the exec and before the container name...

docker exec -u sa container_mssql_1 psql databasename sa -f /my_cool_script.sql

You also might not actually need to explicitly specify the user with the -u option.

Comments

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.