Automate your Docker deployments with Ansible

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i recently made some videos about ansible which is an awesome tool to automate infrastructure and configure a servers and of course docker which is a containerizing engine that makes the deployment of our applications and servers easier and more secure and in this video we will bring those two worlds together and i will show you how i have used docker and ansible to automate my web server deployments so let's go [Music] hey everyone this is christian and if you want to watch more content and tutorials for it professionals and learn more about docker and devops processes then make sure you're liking this video and of course subscribe to the channel if you're not so familiar with ansible in docker yet then don't worry i will explain all the steps we are doing in this tutorial to you but if you have never heard of it before and you want to learn more about it then go and check out my other videos on the channel and as always you don't need to remember any commands or templates i'm using in this tutorial just have a look at my blog where you can just read the full write-up i've put you all the links in the description down below for this tutorial i'm using ansible on my windows subsystem for linux and i'm connecting this to a virtual machine on my workstation but of course you can also deploy this in any other linux environment so i'm using the same templates to deploy containers on my cloud instances as well and of course you can also customize everything and deploy any docker container you want to deploy on your server the goal for today is to deploy a web server that is based on an ubuntu 20.04 lts linux distribution and we are going to run a few darker containers on it i usually always like to run portena which is an awesome graphical user interface to manage existing and running docker containers and deploy new ones as well and we are going to deploy a wordpress container with a database and of course watchtower which can automatically update all our running containers so we don't need to do that manually anymore before we start with the actual tutorial we also need to cover some prerequisites so what you need to consider before doing this so let's take a look at that first okay guys so i'm now connected to my windows subsystem for linux terminal and i'm using the ubuntu 20.04 lts and i already have installed ansible so the first thing before we can start is that we need to check if we are running the latest version of ansible because you won't always get the newest version on all linux distributions so in my ubuntu 20.04 lts for example i only got the version 2.9.6 of ansible via the app package manager so you first should check with ansible double dash version if you're running the latest version at least 2.10 is required by the docker extension we're going to use in this tutorial so if you have any older version installed and not the 2.10 version of ansible you need to remove the previously installed versions first so what you need to do is you need to enter sudo apt remove ansible and this will automatically remove all previous installed versions you can see that i'm currently not using ansible with the aptitude package manager and it is very important that we are not installing this on ubuntu via the app command because if we look at the package manager you can see the latest version of ansible that is available from the ubuntu repositories is only the 2.9.6 which is outdated and we need a newer version for the docker extension to run so therefore we need to install ansible via the python package manager pip so to get the newest version you simply just need to enter python3 and you need to take care that you're not entering this command with the sudo in front of that so enter dash m pip install ansible and you can see i've already insert the latest version so i'm fine if you want to upgrade this by the way you can see there is also a newer version and i want to update this we need to enter upgrade with a double dash in front of that and then you can also update to a newer version okay so now we can install the docker galaxy extension for ansible so that is required by ansible to manage darker containers on remote machines so if we go to the documentation you can see that there is a community docker extension and all community extensions need to be installed via the ansible galaxy hub so the ansible galaxy hub is basically just a hub to create and share new extensions with the community and this is also how you can extend the functionality of your ansible installation so what we need to do is we need to install that first so simply just type in ansible dash galaxy collection installed community.docker and you can see i've already installed this so when you first do that you will usually download the extension and install this okay so we have now installed all the prerequisites on our client machine so we can now start configuring our ansible environment and what i've done i've already created a fresh new installed virtual machine with ubuntu 20.04 lts running and i've not deployed anything on this machine so i'm using vagrant to do this automatically which is a very nice tool to create virtual machines on your local workstation automatically so you should check it out if you want to simplify the process of installing virtual machines locally and now we need to configure our ansible environment so therefore i already created a project folder the ansible dash docker.wordpress folder and i already connected this to my visual studio code so what we first need to do is in order to manage our remote server we need to create a new inventory file where we just put in the public ip address of our server so how we can connect to this machine so just create a new file it doesn't need to have any file extension it just can be a plain text file where there's just one single ip address if you want to manage multiple servers i've explained this in my ansible tutorial on youtube but in case we are just using one single server we can just enter the plain ip address t now we need to create an ansible.cfg file so this will overwrite our default configuration file and we can put anything in here and what we need to customize for this project so we first start with defaults here and then we need to enter our inventory file because i'm just using the same name i can use the inventory file it usually searches in the same folder if there is this file and then includes all the ip addresses in configurations here now i also want to disable the host key checking and set this to false so this is optional but if you haven't connected to your server before you usually will see an ssh prompt where you need to store the fingerprint of your ssh key of the server locally and this prompt is not working well with ansible so therefore i'm disabling the host key checking if you have already connected via ssh to this server before you can probably just set this to true it is recommended in production environments okay i also want to add one more thing that is a deprecation warnings equal false so this is just an annoying notification in the ansible terminal that will send us some duplication warnings for commands that we are not using anyway so we can just set this here and now we need to specify the remote user and because i've deployed this virtual machine via vagrant i'm just using the user vagrant but you could also use the root user if you have just enabled the root user or you have your own personal administrative user then use this username instead we also need to set up a private and public ssh key and if you don't know what that is then check out my youtube video put your link in the description below and if you're using vagrant there's a very very cool trick to authenticate with a vagrant private key that is automatically deployed on every new virtual machine you create with vagrant so you don't need to set up this on your machine you just simply can refer to this location here the vagrant machines default then v because i'm using hyper v for my uh hyperface on windows 10 and then the private key so now we can actually test if we are able to connect to our server so we can just in the terminal enter the command ansible all m ping so this should get us in a successful result you can see success we are now successfully connected and authenticated to our remote server and we now can execute any ansible playbooks so i've not configured anything on this virtual server i've not installed docker i've not installed any prerequisites or anything like this if you want to install docker on your remote server you can do this manually by just going through the official tutorial so just go to docs.docker engine install then select your platform usually an ubuntu server and then go through all the prerequisites install the repository then add the official gpg key and so on and for the docker extension you also need to install the docker sdk so when we go to the ansible docker documentation and click on docker container for example you can see that all the ansible docker commands to manage docker containers on our remote servers require the docker api in this new version in in and they also require the docker sdk for python so the docker sdk can be installed via the package manager but of course we need to install that on our remote server as well so there is a lot of stuff we would need to install there and why not just use ansible for that because we already configured our ansible environment we could just go and install docker the python package manager and the docker python sdk via ansible automatically on our server and what i did is i just created an ansible playbook for you where you can easily install that so just go to github and just go to my repository ansible boilerplates and there you will find a provision folder click on ubuntu and then you can see install docker.ymo so this eml script will basically install docker and the docker sdk for pythons so i've just translated all these docker installation instructions to an ansible playbook and you can just basically copy this and we will create a new playbook that we will call install underscore docker.yaml for example just paste everything in here and then we can simply just execute this playbook what you need to take care of you need to replace the vagrant user of course with your username that you're using on your server because i'm using vagrant i can just leave it like this but otherwise you need to change that username to execute this ansible playbook just enter ansible dash playbook and we will install yorker.yaml okay so let's run and test this and if everything was successful you should see a screen like this so everything was installed and let's connect to this server and test if everything was successfully installed because usually i want to check this so we can just connect um with ssh to the server i'm using the vagrant ssh command because that's very comfortable to connect to our virtual machine and then we can test if docker was installed successfully and if the python sdk is installed in the correct version so first let's execute a docker double dash version so then we should see the docker engine is installed and to check if the python sdk is installed in the correct version then we need to execute python3 and pip list and then we can just simply grab and this is a search command or the docker and you should see a package like this here this is called docker in the version 5.0.0 and i also installed the docker compose which is pretty useful if you want to manage docker compose templates with python sdk but for this tutorial we actually wouldn't need this okay so let's exit from our remote server because we now want to create our first ansible playbook to deploy some containers on this machine so let's do this and create a new file that is called install portainer.yaml because i usually like to run portena on any server because it is very nice graphical user interface to manage docker containers if you're not familiar with it then again check out my tutorials yeah i've done a lot of tutorials on portena and other management tools on my channel so note you usually shouldn't add a become yes yeah because this would become the root user and usually the root user hasn't access to the same python sdks and packages that are installed because it uses these different environments for every user so don't use the become yes command here you shouldn't need to do that then we need to enter a new task and we call it just deploy portainer for example and we are using the community dot docker dot docker underscore container because when we go to the documentation we can see that we have different options here so the docker module can manage different things here like images containers it can manage stacks volumes and networks and everything else right and therefore we need to access this here the docker container module inside the collection is managing darker containers on our remote servers and there you can look up all the different attributes and commands but let's go over this and i'll explain it step by step so we will use the name portena for our container and we are using the image portena slash fortener dash ce which is a community edition of portena and we want to expose the port 9000 and the port 8000 if you want to connect any agents to this installation later and of course we need to enter some volume so first we need to mount the docker socket inside our container because otherwise pertainer is not able to manage any containers on this local server and we also of course want to store our portlander data persistently in a new volume so ansible will also take care of these volumes and create them automatically for you so you don't need to specify them anywhere here and you can also set the restart policy to always okay so let's try and execute this playbook here and we simply just use the same command we just will change install docker to install portland.yaml and execute it okay so everything was successful here but sometimes i get an error message especially when i execute it right after the installation of the python sdk and i don't know for what reason this is happening but it usually tells me that i don't have the right permissions to execute it so if you see an error message like this you sometimes just need to wait a few minutes and then it surprisingly works i don't know why this is happening it sometimes has something to do with the python sdk installation and maybe it just needs to time out the previous session or whatever so just wait a few minutes if you see a permission error because it should just work with this configuration and without the become yes okay so that is great let's check if portena was deployed on our remote server and we just simply can enter the ip address that i've just forgotten so let's just simply copy this and open a new window with http on port 9000 and you can see we can now access portena so it's just deployed we can set it up and configure everything so that was pretty easy right but we are not done because we want to deploy a full web server stack here so what we need to do is we want to deploy a few more containers so let's just add another task here so for example i always want to deploy watchtower deploy watchtower which is a very nice tool to automatically update all our running docker containers on the server i've also done a separate video on this and we also want to add a volume here and we also want to set the restart policy to always so i also want to enter a command this is very specific because i want to schedule my update cycle here so i want to enter this every day at 5 00 am and now we can just simply execute the same playbook again and what is very very cool about ansible is if we just execute the same playbook again you can see that it doesn't really redeploy the porting our container because we didn't change anything here it just executed this new task here deploy watchtower and if we go to our remote server and execute the docker ps command you can see that both containers are currently running successfully so it's very very easy to manage containers with ansible so let's also use that to deploy a simple web server application and for a quick test i want to deploy a wordpress blog because we usually need two containers that are connected like a database container and a wordpress container so let's also create a new playbook because you can also separate this into a different task if you want to do that and let's add a wordpress template here so the first thing when we want to connect two containers to each other we need to create a new network usually so let's create a new network and we are using the community.docker docker and now we need underscore networks so if we go to the documentation you can see that there are different modules here and the docker underscore network can manage docker networks on the host so let's go and use that and we want to add a new network that is called wordpress so usually this is deployed with a default bridge interface it is an isolated network so let's create new docker containers and we first want to deploy a wordpress container here and now the syntax is a bit different than in docker compose files we need to take care of this it looks pretty similar but it is not the same also in the environment variables it's a bit different so check out the ansible documentation for the community docker collection so for the environment variables we need to enter env for example and i will just copy and paste some examples from my templates here and set the restart policy to always okay of course we also need a database server to store any entries here for the wordpress container so we need to deploy another container and we name it database and use the image mysql 5.7 just accordingly to the official wordpress documentation on the docker hub we also want to connect it to the same network and i've just recognized that i've missed something very important here on the wordpress container and this is a volume of course because we want to store our data persistently right otherwise our website is always reset it once we restart the docker container and we don't want this right and of course we also need persistent storage of our database otherwise will also get reset it so create a new volume that is mounted into the valip mysql folder and we also want to set up our environment variables here so what is pretty useful for the mysql environment variables is a mysql random root password you can just enter in one and it will generate a random root password for this mysql database because we usually don't need this root password we also want to set up the restart policy to always and let's try and run our playbook and let's see if we can deploy a wordpress block with this single command so exit our remote server and now we want to deploy wordpress.yml and you can see our wordpress blog is now running we can just now go and install it this is usually how i deploy my web servers because i'm using these ansible templates to just quickly and automate the process of any deployments of containers and networks and whatever i need on my remote server so this is pretty cool it's pretty decent to do that and it's very very comfortable because you can also store these templates somewhere and you don't need to reinvent the wheel okay guys so i hope this helped you to automate your web server deployments with ansible and docker and if you enjoyed this video please don't forget to hit the like button and subscribe because it's really helping out my channel and if you want to share your own templates and playbooks with us then just journal discord community and we have a special channel that's called share projects and there you can share all your projects or github repositories or playbooks and templates i would be very happy to add some of your templates and playbooks inside my own ansible boilerplates github repository because we can all benefit from each other and this is also helping everybody inside our community so thanks everybody for watching enjoy the rest of your day and i hope i see you soon take care bye bye
Info
Channel: The Digital Life
Views: 7,237
Rating: 4.9717317 out of 5
Keywords: ansible docker, ansible docker compose, ansible docker container, ansible docker deployment, ansible docker image, ansible docker playbook, ansible docker tutorial, ansible vs docker, how does ansible work, what does ansible do, configuration management
Id: CQk9AOPh5pw
Channel Id: undefined
Length: 20min 31sec (1231 seconds)
Published: Mon May 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.