Hello, this post will explain how Docker works and its basics. We'll use a Vagrant and Virual Box to create a Virtual Machine dedicated to docker. You can get the vagrantfile here
What is it??
As explained in opensource.com Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.
In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they're running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.
How to use it
In order to be able to run a container we need to have it available for us. So, to start we'll need to pull the wanted container and for this we will go to DockerHub.
For the sake of the example I'll use a Nginx and a Grafana containers.
Type in your terminal (where you runned your vagrantfile) the following command:
Nginx:
docker pull nginx
Check out the DockerHub Nginx page for more detail here
Grafana:
docker pull grafana/grafana
Check out the DockerHub Grafana page for more detail here
Now that we pulled the containers, they're "installed" in your machine so that you are able to use them.
But now we need to run our containers and we need to attribute them with specific ports. To do so we'll need to use the -p command.
So, let's check how do we run a container that we have available and how do we give it a port number of our choosing. For that, copy paste the following line in your terminal:
docker run -d --name=grafana -p 3000:3000 grafana/grafana
This will run Grafana in the port 3000. To confirm this we'll need to go to your brower and tyoe 127.0.0.1:3000 Doing so should open your personal Grafana's site.
Note that when you do -p 3000:3000 the second number is the designated port in your host. the option is "-p container_port_number:host_port_mapped" For example, if we do -p 3000:6000 then the ip for your grafana site will be 127.0.0.1:6000
TIP: If you did all of this and still got an error opening the web page, go check your VM preferences in VIrtualBox and make sure that you add port 3000 in the Port Redirections. To reach Port Redirections just open VirtaulBox, go to the Configuration of your Docker VM to the Network part. You'll find a Advance section and inside there will be the Port Redirection. VB > VM Configuration > Network > Advance > Port Redirection