Setting up a Compute Instance in OCI

Alexis Hevia
6 min readMay 11, 2023

--

In this post I’ll show you how to get a compute instance up and running in Oracle Cloud Infrastructure (OCI).

This is the only post in the “Self Hosting Using Docker and a Cloud Provider” Series that is Oracle Cloud specific. Once we have a compute instance up and running, all Docker related instructions will be vendor independent.

Most cloud providers offer compute instances as a service, but they might have different names for it, i.e.: AWS - EC2 Instance, GCP - VM Instance, OVH - Compute Instance, DigitalOcean - Droplet.

Creating your OCI account

Visit https://www.oracle.com/cloud/ and register for a free account.

Note: Depending on the region you register in, it might take anywhere from a couple hours to a couple days between the time you register and the time your account is provisioned and ready to use — be patient.

Creating a VCN

Before you can launch a compute instance, you need to have a virtual cloud network (VCN) to launch it into.

Once your account is provisioned:

  1. Log in and click on Networking > Virtual Cloud Networks.
  2. Click Start VCN Wizard.
  3. Select Create VCN with Internet Connectivity, and then click Start VCN Wizard.
  4. Enter the following:
  • VCN Name: selfhost
  • Compartment: root

OCI uses “compartments” as a mechanism to organize resources. By default, you’ll see a root compartment was created when your account was provisioned. Since our setup is very simple, we can use this root compartment for everything.

  • VCN CIDR Block: 10.0.0.0/16
  • Public Subnet CIDR Block: 10.0.0.0/24
  • Private Subnet CIDR Block: 10.0.1.0/24
  • Accept the defaults for any other fields.

5. Click Next

6. After the components are created, click View VCN.

7. Click on public subnet-selfhost > Default Security List for selfhost

8. Click on Add Ingress Rules and enter the following Ingres Rules:

Ingress Rule 1
We’ll use this rule for accepting HTTP traffic.

  • Source Type: CIDR
  • Source CIDR: 0.0.0.0/0
  • IP Protocol: TCP
  • Source Port Range: All
  • Destination Port Range: 80

Ingress Rule 2
We’ll use this rule for accepting HTTPS traffic.

  • Source Type: CIDR
  • Source CIDR: 0.0.0.0/0
  • IP Protocol: TCP
  • Source Port Range: All
  • Destination Port Rage: 443

9. Click on Add Ingress Rules so the rules are created.

Creating a Compute Instance

  1. In the top navigation menu, go to Compute > Instances
  2. Click on Create instance
  3. In the Image and shapesection, click on Change shape , and select the Ampere Arm-based processor.
    You’ll notice you get 6GB memory on an “Always free” image. This is a pretty good deal.
  4. In the Networking section, click on Edit and select the selfhost Virtual cloud network (VCN) that we created before.
  5. In the Add SSH keys section, choose Upload public key files (.pub) and upload your own public key.
    If you don’t already have an SSH key, I recommend following Github’s guide on Generating a new SSH key.
  6. In the Boot volume section, clear (unselect) all options.
  7. Accept the defaults for any other fields, and click on Create .

Once the compute instance is provisioned, you’ll see the Public IP address and Usernameare displayed under the Instance access section.

You should be able to connect to your compute instance using the following command:
ssh <username>@<ip address>
eg: ssh opc@143.47.51.100
(your instance’s IP address will be different)

Installing Docker and Docker Compose

When you create a compute instance in OCI, it defaults to using the Oracle Linux operating system.

Oracle Linux uses a package manager called DNF, which we’ll leverage for installing Docker and Docker Compose.

  1. SSH into your compute instance: ssh opc@<ip address>
  2. Update dnf: sudo dnf update -y
  3. Install docker:
# add the docker repo to DNF
sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

# install the docker-ce package
sudo dnf install -y docker-ce --nobest

# enable and start the docker service
sudo systemctl enable docker.service
sudo systemctl start docker.service

# add your user to the "docker" group. This will allow you to run
# docker without using `sudo`
sudo usermod -a -G docker $USER

4. Install Docker Compose

# Download the docker-compose binary into /usr/local/bin
sudo curl -L "https://github.com/docker/compose/releases/download/v2.17.3/docker-compose-$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m | tr '[:upper:]' '[:lower:]')" -o /usr/local/bin/docker-compose

# Set the binary as executable (+x)
sudo chmod +x /usr/local/bin/docker-compose

At this point, you should be able to run docker and docker-compose.

If you run docker-compose version, you should see something like: Docker Compose version 2.17.3

If you run docker --version, you should see something like Docker version 23.0.5

If you get the following error: permission denied while trying to connect to the Docker daemon, it probably means the change we made to add your user to the docker group has not taken effect. Try closing the ssh session (just run exit) and ssh into your compute instance again.

Creating and Attaching a Block Volume

By default, your compute instance is created with a 50GB boot volume.

You might need additional storage capacity if you’re planning on self hosting photos, music, and other media files.

Oracle supports attaching “Block Volumes” to your compute instances, allowing you to add as much storage as you might need.

To add a Block Volume to your instance:

  1. In the top navigation menu, go to Storage > Block Volumes
  2. Click on Create Block Volume
  3. Enter the following:
  • Name: selfhost-media
  • Compartment: root
  • Volume Size: Depends on your needs.
    Note: When this post was written, the Cloud Free Tier included a max of 200GB of block volume storage. Consult the Oracle Storage Pricing page for pricing details on additional storage.
  • Backup Policies: No backup policy
    In the “Backup your OCI data to a different cloud provider” post we’ll be setting our own backup process.

4. Accept the defaults for any other fields, and click on Create Block Volume.

Once the volume is provisioned, go to the Block Volume Details page, then:

5. Click on Attached Instances > Attach to Instance

6. Enter the following:

  • Attachment type: Paravirtualized
  • Access type: Read/Write
  • Instance: select the compute instance we created before
  • Leave other values empty/unselected, and click on Attach

Mounting Your Block Volume

Once the block volume state changes to “Attached”, we need to SSH into our compute instance in order to get our volume ready for usage.

  1. SSH into your compute instance: ssh opc@<ip address>
  2. Run sudo parted -l
    You should see something like:
    Disk /dev/sda: 50.0GB — This is the boot volume that was created with the instance.
    Error: /dev/sdb: unrecognized disk label — This is the block volume we we attached, which has not been formatted yet.
  3. Run sudo parted /dev/sdb
    This will start the parted app, which allows us to create a partition in our block volume.
  4. Run (parted) mklabel msdos to label the disk.
  5. Run the following commands to create the partition.
    We’ll be using the ext4 file system, and we’ll make the partition the full size of the volume:
(parted) mkpart
Partition type? primary/extended? primary
File system type? [ext2]? ext4
Start? 1
End? 100%
(parted) print
(parted) quit

6. Run sudo mkfs.ext4 /dev/sdb1 to format the new partition.

7. Run sudo e2label /dev/sdb1 disk2-part1 to label the partition.

8. Run sudo mkdir /mnt/media && sudo mount /dev/sdb1 /mnt/media to mount the partition into the /mnt/media directory.

9. Run sudo chown -R opc:opc /mnt/media to make sure the opc user is able to read and write from the /mnt/media directory.

At this point, your block volume is usable. You can go into the /mnt/media directory and start creating files there. However, if you restart the compute instance, you would have to mount the disk again.

10. To enable persistent mounting after a reboot, add the following line to the /etc/fstab file:

/dev/sdb1 /mnt/media ext4 defaults 0 0

Note: if you don’t know how to edit files on a terminal, check out How to use nano on Linux. Run sudo nano /etc/fstab , and add the line.

Wrapping Up

In this post you:

  1. Created an OCI account
  2. Created a compute instance that allows traffic over SSH, HTTP, and HTTPS
  3. Installed Docker and Docker Compose
  4. Attached additional storage to your compute instance

This is all we need in order to get started hosting our services. Check out the “Self Hosting Using Docker and a Cloud Provider” series for next steps.

--

--

Alexis Hevia

Full-Stack Web Developer @ X-Team / Digital Nomad. Se habla español.