DEV Community

loading...
Cover image for DEPLOY YOUR WEB APPLICATIONS IN A MINUTE - MADE WITH πŸ’– FOR DOCKERISTS

DEPLOY YOUR WEB APPLICATIONS IN A MINUTE - MADE WITH πŸ’– FOR DOCKERISTS

CrazyOptimist
https://crazyoptimist.net/about
・Updated on ・2 min read

As a software engineer or a DevOps engineer, you may come up with boring tasks frequently, one of which is setting up a new cloud environment for deployment of your awesome web apps/micro services.

Using the following script, you will lose the dull pain. Just grab the script and run it with bash, then you will get the complete environment with docker/docker-compose and nginx which are all on the latest stable version.

Note: This is just for Ubuntu Bionic/Focal. Buzz me anytime in case you want to get the same one for any other distro, I'd love to help!

#!/bin/bash
# upgrades the system
sudo apt-get update && sudo apt-get upgrade -y

# install docker latest
sudo apt remove docker docker-engine docker.io containerd runc  # purging the legacy version if exists
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update && sudo apt install docker-ce docker-ce-cli containerd.io -y
# install docker-compose latest
#curl -L "https://github.com/docker/compose/releases/download/1.27.0/docker-compose-$(uname -s)-$(uname -m)" -o docker-compose
curl -s https://api.github.com/repos/docker/compose/releases/latest \
| grep -v ".sha256" \
| grep browser_download_url \
| grep "docker-compose-$(uname -s)-$(uname -m)" \
| cut -d '"' -f 4 \
| xargs curl -L -o docker-compose
chmod +x docker-compose
sudo mv docker-compose /usr/sbin/    # in case of Bionic or Focal
# add the current non-root user to docker group
sudo groupadd -f docker
sudo usermod -aG docker $USER
sudo docker -v
sudo docker-compose -v

# install nginx latest
echo "
deb [arch=amd64] http://nginx.org/packages/mainline/ubuntu/ bionic nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ bionic nginx
" | sudo tee /etc/apt/sources.list.d/nginx.list
wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key
sudo apt update
sudo apt remove nginx nginx-common nginx-full nginx-core -y
sudo apt install nginx -y
rm *.sh *.key

echo -e "\e[32mPlease reboot your machine .. \e[0m"

Enter fullscreen mode Exit fullscreen mode

You can also get the latest version of this script from my gist anytime.

Happy containerizing gents! 😎

Ported from my blog post here.

Discussion (9)

Collapse
aimerib profile image
Aimeri Baddouh

I love this! Will certainly adopt it, and might even turn this into an ansible recipe. My only two pieces of suggestion:

If you want to keep only one nginx per host, but have multiple applications running in container, perhaps you could look into using swarm. Have one stack for your proxy, and deploy other apps in their own stack, but have them communicate via a dedicated nginx network. You already have all the tools necessary, and deploying it becomes a matter of having the right docker-compose.yml files, which you can keep in a repo. It reduces the amount of dependencies on the host, and makes it a little faster to upgrade versions, or replacing proxy/load balancers down the road.

in your script you have the latest version of docker-compose hardcoded, but if you wanted to always get the latest version you could replace the line:

curl -L "https://github.com/docker/compose/releases/download/1.27.0/docker-compose-$(uname -s)-$(uname -m)" -o docker-compose

with:

curl -s https://api.github.com/repos/docker/compose/releases/latest \
| grep -v ".sha256" \
| grep browser_download_url \
| grep "docker-compose-$(uname -s)-$(uname -m)" \
| cut -d '"' -f 4 \
| xargs curl -L -o docker-compose

The above is going to query the github api to get a list of the most recent release, ignore the .sha256 file, filter out any fields that aren't download urls, find the right download for your distro, get only the actual url, and curl download it. Everything else in the script remains the same.

Collapse
crazyoptimist profile image
CrazyOptimist Author

Updated according to your advice, thanks!

Collapse
crazyoptimist profile image
CrazyOptimist Author

Great!

Collapse
robkenis profile image
Rob Kenis

Why are you running nginx native on the machine and not in a docker container ?

Collapse
crazyoptimist profile image
CrazyOptimist Author • Edited

When it comes to hosting multiple web apps on one machine, it makes me to avoid redundant nginx containers and a few more benefits ..
I found it more painless. Is there any reason to use nginx containers every time or any security concerns? Let me know, I'd love to learn from you!

Collapse
robkenis profile image
Rob Kenis

There's no security concern I can think of right now. I was thinking more about using the same setup when developing and in production. For example, when I have 2 projects on my machine with a different nginx config, it would be easier to run both in a container and manage the applications and nginx in docker-compose. Then after it works locally, you can bring your entire nginx container to production.

Thread Thread
crazyoptimist profile image
CrazyOptimist Author

I agree with you.
Different nginx configuration across multiple apps on the same machine. πŸ‘

Collapse
mertsenel profile image
MertSenel

Thanks I always need to go into docker docs each time for this task. Can I recommend converting this to "cloudinit" format. That way you can just run the script while creating the VM.

Collapse
crazyoptimist profile image
CrazyOptimist Author

Thanks too! Ya, it's a cool idea! Let me test it soon.