How can I connect to an apache service in Bitbucket pipelines?
Asked by Chris Runo:

I have a pipeline where I need to run a handful of different tests. One of those tests is to build a NextJS site with a Drupal CMS as the backend. This repository serves as a starting point for future projects so there is no other environment that this gets tested in (IE no production).

To build the NextJS site, the Drupal site needs to be running and available at port 443.

I was getting errors when building around connecting to the Drupal site. To test the connection to the apache server I have my pipeline set up like this right now:

```
pipelines:
  pull-requests:
    '**':
      - step:
        name: Test municipal profile
        image: {custom image}
        caches:
          - composer
          - node
        script:
          - curl https://127.0.0.1
```

That results in this error:

    curl: (7) Failed to connect to 127.0.0.1 port 443 after 0 ms: Connection refused

Here is the custom image:

```
FROM ubuntu:22.04

ENV LC_ALL=C.UTF-8

CMD /bin/bash

# Ensure bash over sh.
RUN rm /bin/sh && \
ln -s /bin/bash /bin/sh;

ARG DEBIAN_FRONTEND=noninteractive

# Replace shell with bash so we can source files and install packages.
RUN apt-get update && \
apt-get -yq upgrade; \
apt-get install -yq software-properties-common && \
add-apt-repository -y ppa:ondrej/php && \
apt-get dist-upgrade -yq && \
apt-get update --fix-missing;

# Install basic packages
RUN apt-get -yq install --fix-missing \
apache2 \
apache2-utils \
apt-transport-https \
autoconf \
automake \
build-essential \
bzip2 \
ca-certificates \
curl \
file \
gcc \
g++ \
git \
imagemagick \
libfontconfig \
libfreetype6 \
libssl-dev \
jq \
make \
mysql-client \
patch \
python3-pip \
python3-venv \
unzip;

# Install PHP 8.2
RUN apt-get -yq install \
libapache2-mod-php8.2 \
php8.2 \
php8.2-bcmath \
php8.2-cli \
php8.2-curl \
php8.2-dom \
php8.2-gd \
php8.2-intl \
php8.2-imagick \
php8.2-mbstring \
php8.2-memcache \
php8.2-mysql \
php8.2-simplexml \
php8.2-soap \
php8.2-xml \
php8.2-xmlwriter \
php8.2-zip \
php-imagick \
php-json \
php-memcache \
php-pear;

RUN update-alternatives --set php /usr/bin/php8.2;

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --filename=composer --install-dir=/usr/bin;
RUN composer --verbose self-update --stable;

# Install NodeJS
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash - && \
apt-get install -y nodejs && \
npm install -g npm && \
npm install -g yarn

# Install AWS cli
RUN ln -s /usr/bin/python3 /usr/bin/python
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
unzip awscliv2.zip && \
./aws/install -i /usr/local/aws-cli -b /usr/local/bin;

# Clean up installs
RUN apt-get autoremove -yq && \
apt-get clean -yq && \
rm -rf /root/.composer /tmp/* ./awscli-bundle;

EXPOSE 80 443 3000

ENTRYPOINT ["apache2ctl"]
CMD ["-D", "FOREGROUND"]
```


Answers:
author: Patrik S
body: Hello [@Chris Runo](/t5/user/viewprofilepage/user-id/5015783) ,

Thank you for reaching out to Atlassian Community!

Just to bring some context, when a pipeline is triggered in bitbucket, a build container is created based on the docker image you have defined in the step. When starting this build container, pipelines will force the entry point to be a terminal instance of **/bin/sh**, where the commands you defined in the step's script will be executed. This means that any entry point or command you have defined in the docker image will not be taken into account.

If you are looking to have a separate container to run your Apache server or any other type of service, then you could use the **[service containers](https://support.atlassian.com/bitbucket-cloud/docs/databases-and-service-containers/)** feature. Service containers are separate containers that are spun up on the step with a docker image you define, and they share a network adapter with your build container, so they can be accessed from your step's script.

Following is an example of defining service containers for redis and MySQL databases:

```
pipelines:
  branches:
    main:
      - step:
          image: redis
          script:
            - redis-cli -h localhost ping
          services:
            - redis
            - mysql
definitions:
  services:
    redis:
      image: redis:3.2
    mysql:
      image: mysql:5.7
      variables:
        MYSQL_DATABASE: my-db
        MYSQL_ROOT_PASSWORD: $password
```

For your particular case, you could define a service using your {custom-image} and add this service to the step. You should be able to access the service containers on the IP address **127.0.0.1** and the port your service is exposing (for example, MySQL uses port 3306 by default).

You're also welcome to take a look at our documentation on [Databases and service containers](https://support.atlassian.com/bitbucket-cloud/docs/databases-and-service-containers/) for further examples of usage.

Hope that helps! Let me know in case you have any questions.

Thank you, [@Chris Runo](/t5/user/viewprofilepage/user-id/5015783) !

Patrik S


Tags: bitbucket-cloud

cloud