How to connect to service started using docker-compose/docker in Github Workflow without 'service containers'?

Viewed 589

I'm trying to configure CI with GitHub Actions and I'd like to re-use 'docker-compose'-based logic/scripts in workflow scenario. In first step I start some background services using 'docker-compose'(or docker) and in the following step I'd like to reach those background services using host:port, but it fails. For example:

  - name: Start background service
    run: docker-compose up -d mysql
  - name: Wait and check mysql container is running
    run: |
      sleep 120
      docker ps 
  - name: Check connection
    run: mysql -uroot -ppassword -hlocalhost -p3306
  # or
  - name: Check connection
    run: mysql -uroot -ppassword -hmysql -p3306 
  # here I get 'could not resolve host' error

I'll get connection failure in both cases, despite connection params are correct and according 'docker ps' I have 'mysql' container running and ports are exposed properly.

Is there any way I can reach docker containers from a step? Preferably without need to run step's logic in another container.

I know that for such things GitHub Actions provide 'services', but I have quite a big piece of logic in docker/docker-compose and I'd really like to use it to avoid duplication.

2 Answers

According to GitHub docs:

You can access a service container using the label you configure in the workflow. The hostname of the service container is automatically mapped to the label name. For example, if you create a service container with the label redis, the hostname of the service container is redis.

So I geuss in your case you have to connect with -hmysql instead of -hlocalhost.

Assuming you are running all MySQL commands in another container.

To check communication with the service name of the MySQL container, connect to the same network used in MySQL container deployment or link your current network and the MySQL network. Now, you can able to communicate as expected.

If you can post the docker-compose YAML file, I can give you the correct idea.

Related