Deployment with only SSH Key and dockerfile
Asked Answered
N

1

8

Excuse my dev ops naiveté but I assume all you need to deploy to a machine is a proper SSH key, a port to expose, the machine's IP address a login and the code to deploy.

So are there any simple solutions that deploy code to a remote server with the only input being an SSH key, a Dockerfile and the code itself? I'm thinking it could be set up in a deterministic (almost functional) manner where the input is server IP address, login, and the output is a running server.

I've tried setting up Dokku on digital ocean (https://www.digitalocean.com/community/tutorials/how-to-use-the-digitalocean-dokku-application) and that requires a DNS record, and git. I don't need those as dependencies.

Thanks

Neoptolemus answered 23/9, 2015 at 22:22 Comment(6)
How will others access your server?Glassblowing
For now via ip address/portNeoptolemus
I would suggest evaluating Docker machine: docs.docker.com/machine it has a generic driver that work over SSH see: docs.docker.com/machine/drivers/genericSocialist
Dokku doesn't require a dns record - you can definitely just use an ip - and re: the git requirement, you could push a tarball in via stdin if you want.Urban
Took a look at docker machine. It looks cool and works with digital ocean, aws and online providers. I wish it had a bare metal set up for just sshing into a machine. I haven't seen a way to set up Dokku with ip and tarball. I'll keep researching.Neoptolemus
Wait I missed that docker machine has a generic driver! docs.docker.com/machine/drivers/genericNeoptolemus
L
2

If I understand your question correctly, you don't needed anything more than scp, ssh and a couple of shell scripts.

Let's say you want to deploy your code from serverA to serverB.

On serverB, create a directory with you Dockerfile. Also, create a shell script, let's call it build_image.sh, that runs your docker build command using sudo.

Also, on serverB, create a shell script that builds your code from source (if necessary).

Finally, on serverB, create a shell script that calls your code build script, your docker build script and at the end runs your new docker image. Let call this script do_it_all.sh.

Make sure that you chmod 755 all shell scripts.

Now, on serverA, you have a directory with the source code. scp that directory to serverB into the directory with the Dockerfile.

Next, from serverA use ssh to call do_it_all.sh on serverB. This will build your code, build your image and deploy a container without the need for extra software, packages, git, DNS records, etc.

You can even automate this process using cron or something else to have nightly deployments, if you wish, or deployments under other conditions.

Example scripts/commands:

On serverB:

build_image.sh:

#!/bin/bash
sudo docker build -t my_image

build_code.sh (optional, adjust to your code):

#!/bin/bash
cd /path/to/my/code
./configure
make

do_it_all.sh:

#!/bin/bash
cd /path/to/my/dockerfile
sudo docker stop my_container #stop the old container
sudo docker rm my_container #remove the old container
sudo docker rmi my_image #remove the old image
./build_code.sh #comment out if not needed
./build_image.sh
sudo docker run -d --name my_container my_image

On serverA:

scp -r /path/to/my/code serverB:/path/to/my/dockerfile
ssh serverB '/path/to/my/dockerfile/do_it_all.sh'

That should be it. Adjust for your system.

To deploy to a brand new system, just write a script on serverA that uses ssh to copy create necessary directories on serverB ssh serverB 'mkdir /path/to/dockerfile'. Next, copy your Dockerfile and your build scripts and your code from serverA to serverB using scp. Then run do_it_all.sh on serverB from serverA using ssh.

Latrena answered 1/10, 2015 at 22:55 Comment(2)
Thanks for giving a full fledged answer so here's the bounty. I've been working on solution with Docker-Machine (even though yours has fewer dependencies). github.com/GTDev87/quick-deploy/blob/master/quick-deployNeoptolemus
Anytime! I am a big fan of using what's at hand as much as possible. It would be good to see your Docker Machine solution when you get there. Thank you for the bounty.Latrena

© 2022 - 2024 — McMap. All rights reserved.