Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deployment with only SSH Key and dockerfile

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

like image 978
GTDev Avatar asked Sep 23 '15 22:09

GTDev


1 Answers

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.

like image 120
keda Avatar answered Oct 28 '22 14:10

keda