Okteto Pipelines vs Deploy Preview Actions for `docker-compose` Files

Hi there! I was hoping to get some clarifications on what the best way of utilizing Okteto for my usecase would be.

I have a repository with the following files:

  • docker-compose.yml - this spins up a java backend + React frontend web app, a MySQL, and a MongoDB instance
  • A bash script - downloads seed data and initializes the MySQL database
  • data directory - contains data files that will be imported into the containers spun up
  • Inside one of the containers spun up by the docker-compose file, I also have a Python script which needs to be run to import the data from the data directory into the MySQL database.

I want to setup the repo such that pull requests automatically trigger a build of the docker compose file, with the data in the data directory imported into the web application+databases running in the container. This build will essentially be a staging instance to preview the new data files and how they look when run in the application.

Thus, my plan was to essentially setup a GitHub Action Workflow that does the following steps on a pull request to the repo:

  • run the bash script
  • Use an Okteto Pipeline action to run the docker compose file to spin up the application and databases (previously a docker compose up command)
  • execute the Python script inside the running container to import some data into the container (previously a docker compose exec command)

I just wanted to clarify what exactly the recommended way of approaching this using Okteto would be. I had previously tried using Preview Environments (and the deploy-preview GitHub action) to do this, but even though the compose file supposedly deploys correctly, I am unable to access the endpoint (from my understanding, the deploy-preview action checks out the repo from scratch, and so the setup from the bash script is not carried forward).

I am able to properly deploy the compose file using local CLI commands (okteto deploy or okteto up) however, which is why I thought to use Okteto Pipelines.

I was also unclear about whether I needed to specifically use an okteto.yml file, or whether I could stick to just using the docker-compose file, as well as whether this was actually the right usecase for Okteto Pipelines.

Any clarifications would be much appreciated!

Hi @justinjao,

I’ll try to go 1 by 1 on all your questions:

deploy-preview vs okteto pipeline

Okteto Previews and Okteto Pipelines are both tools that can be used to deploy your applications to Okteto Cloud. However, they have different purposes and use cases.

  • Okteto Previews are designed to create temporary environments that you can use to preview changes to your application before you deploy them to production. They are typically used for pull requests, so that you can get feedback from your team before making changes to the main branch.
  • Okteto Pipelines is a more general-purpose tool that can be used to automate the deployment of your applications to your Okteto cluster. They can be used to deploy applications into a namespace.

Both of them lets you deploy a compose or k8s files. Reading your comment and since you want to run it for every PR I think okteto previews are more suitable for you since it allows you to have one preview environment for every PR and with okteto pipeline yo will be sharing one namespace.

I was also unclear about whether I needed to specifically use an okteto.yml file, or whether I could stick to just using the docker-compose file

You can stick to docker compose syntax.

In order to do that you’ll need to modify your compose so you end up with the following services:

  • download: You can achieve that this script is run only once without restarting by setting the restart policy to no. This download script job should store the data downloaded in a persistent volume.
  • databases: I’d deploy them the same way as they are being deployed nowadays.
  • data injector: This scripts waits until the download script has finished correctly and the database are up and running by using the depends_on compose field. It gets the data from the persistent volume used by the download service and inject the data into the databases
  • backend: You will probably need to wait until the databases are up and running. This can be achieved by using the depends_on compose field

Using depends_on and restart policies can make you achieve the expected result.

You can also mix okteto syntax with compose by using the extended okteto manifest v2 deploy section. Here is an example of how you can deploy a compose, your own pipeline and still use it for development.

Both ways are recommended by okteto, so use the one you are more comfortable with.

1 Like

Hi @jLopezbarb,

Thank you very much for the detailed reply. Really appreciated the explanation. And I will try those modifications to the compose file.

In the meantime, I was wondering if you could answer another question of mine. I have managed to get the deploy working using both Pipeline and Preview GitHub Actions. Currently however. after the compose file successfully deploys and the GitHub Action step completes, my database takes a few minutes to actually properly spin up and seed data (around 5 minutes). During this time, the deployed URL returns a 502 error, until it finishes.

Thus, I have setup another GitHub Action to poll the URL until it returns a 200 response. Once this occurs, I want to then run an okteto exec command via one of the running containers. however, I am unable to run this using GitHub Actions, as the Okteto CLI is not installed in the image. And I’m not sure if I can somehow modify the Okteto Manifest file to do this, because of the fact that I need to wait for this intermediate polling GitHub Action to complete before I run the okteto exec command.

Essentially, I am stuck at the last stage of what I wanted to do (execute the Python script inside the running container using a docker compose exec like command).

Would you happen to know whether there is any way I can run an okteto exec command for one of the containers in a separate GitHub Action step, after the containers have been spun up?

Let me know if that was unclear in any way!

Hi again @justinjao,

The okteto exec command allows you to execute a command in your development container. The command will fail if there’s no active development container corresponding to the current folder. In your case okteto exec won’t work since you don’t have an active okteto up session where you can run okteto exec on.

You could achieve the behavior just using docker compose in okteto, but, if you need to use it on another GHAction you can use kubectl exec command in order to have a similar experience to the one you were getting with docker compose exec. You could use something like kubectl exec -it deployment/svc -- ${command} for deployments services or kubectl exec -it statefulset/svc -- ${command} for statefulsets. Let us know if this answer your question. Remember to run okteto kubeconfig before running any kubectl commands in order to have your kubernetes context pointing to the correct cluster.

Happy coding!!

Hi @jLopezbarb, apologies for the late reply. It took a lot of fiddling, but I’m happy to confirm that your last reply worked, and I was able to get it working with okteto kubeconfig and kubectl! I had to run the activate-namespace github action beforehand.

Thank you so much for your detailed help!