j-money
j-money

Reputation: 529

Connect to docker sqlserver via ssh

I've created a docker container that contains a mssql Database. On the command line ip a gives an ip address for the container, however trying to ssh into it username@docker_ip_address yields ssh: connect to host ip_address port 22: Connection refused. So I'm wondering if I am even able to ssh into the container so I don't have to always be using the docker tool docker exec .... and if so how would I go about doing that?

Upvotes: 0

Views: 1477

Answers (3)

David Maze
David Maze

Reputation: 159677

If your container contains a database server, the normal way to interact with will be through an SQL client that connects to it; Google suggests SQL Server Management Studio and that connector libraries exist for popular languages. I'm not clear what you would do given a shell in the container, and my main recommendation here would be to focus on working with the server in the normal way.

Docker containers normally run a single process, and that's normally the main server process. In this case, the container runs only SQL Server. As some other answers here suggest, you'd need to significantly rearchitect the container to even have it be possible to run an ssh daemon, at which point you need to worry about a bunch of other things like ssh host keys and user accounts and passwords that a typical Docker image doesn't think about at all.

Also note that the Docker-internal IP address (what you got from ip addr; what docker inspect might tell you) is essentially useless. There are always better ways to reach a container (using inter-container DNS to communicate between containers; using the host's IP address or DNS name to reach published ports from the same or other hosts).

Upvotes: 1

trust512
trust512

Reputation: 2254

Basically, alter your Dockerfile to something like the following - that will install openssh-server, alter a prohibitive default configs and start the service:

# FROM a-image-with-mssql
RUN echo "root:toor" | chpasswd
RUN apt-get update
RUN apt-get install -y openssh-server
COPY entrypoint.sh .
RUN cd /;wget https://gist.githubusercontent.com/spekulant/e04521d6c6e1ccffbd3455c673518c5b/raw/1e4f6f2cb32caf3a4a9f73b02efdcbd5dde4ba7a/sshd_config
RUN rm /etc/ssh/sshd_config; cp sshd_config /etc/ssh/
ENTRYPOINT ["./entrypoint.sh"]
# further commands

Now you've got yourself an image with ssh server inside, all you have to do is start the service, you cant do RUN service ssh start because it won't work - docker specifics, refer to the documentation. You have to use a Entrypoint like the following:

#!/bin/bash
set -e
sh -c 'service ssh start'
exec "$@"

Put it in a file entrypoint.sh next to your Dockerfile - remember to chmod 755 entrypoint.sh it. There's one thing to mention here, you still wouldn't be able to ssh into the container - the default SSH server configuration doesn't allow login into root account using a password. So you either change the configs yourself and provide it to the image, or you can trust me and use the file I created - inspect it with the link from Dockerfile - nothing malicious there, only a change from prohibit-password to yes.

Fortunately for us - MSSQL official images start from Ubuntu so all the commands above fit perfectly into the environment.

Edit

Be sure to ask if something is unclear or I'm jumping too fast.

Upvotes: 0

Nuwan Attanayake
Nuwan Attanayake

Reputation: 1201

To ssh into container you should full-fill followings

  • SSH server(Openssh) should be installed within the container and ssh service should be running
  • Port 22 should be published from container (when you run the container).more info here > Publish ports on Docker
  • docker ps command should display mapped ports 22

Hope above information helps for you to understand the situation...

Upvotes: 1

Related Questions