Having a place other than your own machine to run docker containers can be handy.
It helps you to speed up the development of your personal projects or relieve the
heavy lifting from your machine.
Recently I was introduced Oracle cloud, and its free tier for an unlimited time.
- 2 AMD based Compute VMs with 1/8 OCPU** and 1 GB memory each
- 4 Arm-based Ampere A1 cores and 24 GB of memory usable as one VM or up to 4 VMs
- 2 Block Volumes Storage, 200 GB total
- 10 GB Object Storage - Standard
- 10 GB Object Storage - Infrequent Access
- 10 GB Archive Storage
- Resource Manager: managed Terraform
- 5 OCI Bastions
As you can see they are very generous with their free tier, and we can do a lot with this.
- Create an Oracle Cloud Infrastructure account (just follow this link).
- Install terraform.
- Install OCI CLI.
- Configure OCI credentials.
As you can suspect by now, we are going to use terraform to provision our Docker service. Basically terraform will create one VM instance as big as the free tear allow us, and let it ready so we can ssh into the machine and with docker ready to start our containers.
- Checkout this project with the terraform configuration
- Run
terraform init
andterraform apply
If you get an error like this:
timeout -- last error: dial tcp IP_ADDRESS:22: connect: connection refused
try to add the id_rsa generated by terraform to your ssh-agent ( ssh-add id_rsa
) and run terraform apply one more time.
If you were able to reach this point, you now have a VM running that you can run whatever you want (check Oracle's terms and conditions for this service). At the end of the process terraform is configured to show the public IP of newly created instance.
What I like to do is to add this to /etc/hosts so I don't need to call it by the IP address.
Connect to the remote server using ssh and run docker ps to check if docker is running as expected.
OK, now you want to start running containers there from your local machine, but you also run some containers locally for whatever reason. So we need to do it in a way that is easy to switch between the two contexts. Docker has a tool for that embedded on its client, to check the contexts that you have available run:
docker context ls
Let's create a context for our remote docker service:
docker context create remote --docker "host=ssh://docker@IP_PROVIDED_BY_TERRAFORM
Now if you list your contexts again you should see your newly created context. The next step is to switch between contexts:
docker context use remote
Now you can run any command that you would normally do locally. To check if it is working try to deploy a nginx container.
docker run -d -p 8080:80 nginx
After this you should be able to open on your browser: http://IP_PROVIDED_BY_TERRAFORM:8080 and you should see the default index.html from docker.
This project was shamelessly based on Jérôme Petazzoni's project, where he describes how to build a Kubernetes cluster on Oracle infrastructure using the free tier. His project was very helpful to understand terraform and the configuration of Kubernetes. I highly recommend you to check it out.
I made this spinoff of his project because, it is great to learn how to deploy a K8S cluster, but it was not that useful for me to have it laying around. Docker service is more useful to have it when you are developing.
And I can see how that can be helpful, especially when your machine is at its limit.ssh