Skip to content

Commit 467f79e

Browse files
authored
chore: enhance dockerized development environment (#977)
1. Add an option to control which PHP version is to be used in devenv service via PHP env var passed at the time when dev-shell is started (or any dev-* target is build). 2. Simplify `composer` and `go` install. 3. Fix `devenv` service image build for PHPs 7.2 and 7.3. 4. Apply `Dockerfile` best practices
1 parent 727812d commit 467f79e

File tree

5 files changed

+116
-124
lines changed

5 files changed

+116
-124
lines changed

Makefile

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -475,25 +475,28 @@ test-services-stop:
475475
# Docker Development Environment
476476
#
477477

478-
dev-shell:
479-
docker compose --profile dev up --build --remove-orphans -d
480-
docker exec -it agent-devenv bash -c "sh files/set_path.sh ; bash"
478+
devenv-image:
479+
@docker compose --profile dev build devenv
481480

482-
dev-build:
483-
docker compose --profile dev up --build --remove-orphans -d
484-
docker exec -it agent-devenv bash -c "sh files/set_path.sh ; make -j4 all"
481+
dev-shell: devenv-image
482+
docker compose --profile dev up --pull missing --remove-orphans -d
483+
docker compose exec -it devenv bash -c "sh files/set_path.sh ; bash"
485484

486-
dev-unit-tests:
487-
docker compose --profile dev up --build --remove-orphans -d
488-
docker exec -it agent-devenv bash -c "sh files/set_path.sh ; make -j4 valgrind"
485+
dev-build: devenv-image
486+
docker compose --profile dev up --pull missing --remove-orphans -d
487+
docker compose exec -it devenv bash -c "sh files/set_path.sh ; make -j4 all"
489488

490-
dev-integration-tests:
491-
docker compose --profile dev up --build --remove-orphans -d
492-
docker exec -it agent-devenv bash -c "sh files/set_path.sh ; ./bin/integration_runner -agent ./agent/.libs/newrelic.so"
489+
dev-unit-tests: devenv-image
490+
docker compose --profile dev up --pull missing --remove-orphans -d
491+
docker compose exec -it devenv bash -c "sh files/set_path.sh ; make -j4 valgrind"
493492

494-
dev-all:
495-
docker compose --profile dev up --build --remove-orphans -d
496-
docker exec -it agent-devenv bash -c "sh files/set_path.sh ; make -j4 all valgrind; ./bin/integration_runner -agent ./agent/.libs/newrelic.so"
493+
dev-integration-tests: devenv-image
494+
docker compose --profile dev up --pull missing --remove-orphans -d
495+
docker compose exec -it devenv bash -c "sh files/set_path.sh ; ./bin/integration_runner -agent ./agent/.libs/newrelic.so"
496+
497+
dev-all: devenv-image
498+
docker compose --profile dev up --pull missing --remove-orphans -d
499+
docker compose exec -it devenv bash -c "sh files/set_path.sh ; make -j4 all valgrind; ./bin/integration_runner -agent ./agent/.libs/newrelic.so"
497500

498501
dev-stop:
499502
docker compose --profile dev stop

docker-compose.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Copyright 2021 New Relic Corporation. All rights reserved.
33
# SPDX-License-Identifier: Apache-2.0
44
#
5-
version: '3.8'
65
services:
76
# The Database
87
mysqldb:
@@ -79,6 +78,8 @@ services:
7978
build:
8079
context: .
8180
dockerfile: files/Dockerfile
81+
args:
82+
PHP_VER: ${PHP:-8.3}
8283
user: ${UID}:${GID}
8384
environment:
8485
MEMCACHE_HOST: memcached

docs/dev_environment.md

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,38 @@ The dockerized development environment prototype allows contributors to both dev
66

77
docker-compose spins up `mysql` and `redis` and other databases in separate containers.
88

9-
Two environment variables to note:
10-
`NEWRELIC_LICENSE_KEY` is required to run the integration tests and should be set to your NR license key.
11-
If your collector isn’t the default (collector.newrelic.com), set the `NEWRELIC_COLLECTOR_HOST` to the appropriate value.
9+
## Prerequisites
1210

13-
PHP_VER can also be set to vary the PHP version being used.
11+
### 1. Docker Compose
1412

15-
Set all environment variables prior to running the development environment.
13+
Dockerized development environment for the New Relic PHP Agent uses following Docker Compose services as a runtime platform. So you need `docker` with `docker compose` installed.
14+
15+
### 2. Environment variables
16+
17+
Dockerized development environment for the New Relic PHP Agent needs a valid license key available in `NEW_RELIC_LICENSE_KEY` environment variable.
18+
This environment variable must be set prior to starting Dockerized development environment for the New Relic PHP Agent. The easiest way to set
19+
`NEW_RELIC_LICENSE_KEY` environment variable is via `.env` file. Simply create `.env` file in the top level directory, and add definition
20+
of `NEW_RELIC_LICENSE_KEY` environment variable there, e.g.:
21+
```
22+
NEW_RELIC_LICENSE_KEY=...
23+
```
24+
25+
The second, optional environment variable, that controls PHP version in Dockerized development environment for the New Relic PHP Agent is `PHP`. `PHP` defaults to latest PHP supported by the agent.
26+
This environment variable can be provided at the time when Dockerized development environment for the New Relic PHP Agent is started, e.g.:
27+
```
28+
make dev-shell PHP=8.2
29+
```
1630

1731
## Options for using the environment
1832

19-
## With a shell environment
33+
### With a shell environment
34+
35+
To start the dev environment type `make dev-shell`. This will spin up `devenv` service in `agent-devenv` container, with:
36+
- latest PHP supported by the agent (this can be overriden with `PHP` environment variable like this: `make dev-shell PHP=8.2`)
37+
- all the tools needed to build the agent
38+
- all the tools needed to run unit tests
39+
- all the tools and supporting services to run integration tests
2040

21-
To start the dev environment type `make dev-shell`. This will create a set of docker containers.
2241
A prompt will open and you’ll be able to compile and run all `make` commands right away with no additional setup (for example: `make -j4 all` or `make -j4 valgrind` or `make -j4 run_tests`).
2342

2443
After compiling the agent, the integration tests can be run using the `integration_runner`.
@@ -31,27 +50,27 @@ To end the session type `exit`. You can run `make dev-stop` to stop the docker-
3150

3251
In the shell, you can run all `make` commands as you normally would.
3352

34-
## Build only
53+
### Build only
3554

3655
`make dev-build`
3756

38-
## Unit Tests only
57+
### Unit Tests only
3958

4059
`make dev-unit tests`
4160

42-
## Integration Tests only
61+
### Integration Tests only
4362

4463
`make dev-integration-tests`
4564

46-
## Build and test all
65+
### Build and test all
4766

4867
`make dev-all`
4968

50-
## Stop all containers
69+
### Stop all containers
5170

5271
`make dev-stop`
5372

54-
# Next steps and issues
73+
## Next steps and issues
5574

56-
## There is possibly some incompatibility with mysql in the main build container as one of the mysql unit tests fails. Unless this is resolved, It might make sense at a future point to have the integration tests run from a different container than the build container.
75+
### There is possibly some incompatibility with mysql in the main build container as one of the mysql unit tests fails. Unless this is resolved, It might make sense at a future point to have the integration tests run from a different container than the build container.
5776

files/Dockerfile

Lines changed: 63 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -14,95 +14,65 @@ FROM php:${PHP_VER:-8.3}
1414
RUN docker-php-source extract
1515

1616
ARG DEBIAN_FRONTEND=noninteractive
17-
RUN apt-get update
18-
RUN apt-get install -y build-essential
19-
20-
#
21-
# PHP dependencies
22-
#
23-
RUN apt-get update \
24-
&& apt-get -y install gcc git netcat-openbsd \
25-
libpcre3 libpcre3-dev psmisc automake libtool \
26-
insserv procps vim ${PHP_USER_SPECIFIED_PACKAGES} \
27-
zlib1g-dev libmcrypt-dev
17+
RUN apt-get update && apt-get install -y --no-install-recommends \
18+
# Install build entrypoint - make:
19+
make \
20+
# Makefile's default shell:
21+
bash \
22+
# git; techincally not needed to build agent but is required for successfull
23+
# processing of top level Makefile:
24+
# - make/version.mk if GIT_COMMIT is not defined, git is used to compute it:
25+
git \
26+
# The following are required to build PHP extension:
27+
$PHPIZE_DEPS \
28+
# valgrind, awk; required for agent-valgrind and axiom-valgrind targets
29+
valgrind gawk \
30+
# Other useful developer tools:
31+
vim lcov gdb strace ccache procps psmisc curl wget bzip2 zip unzip perl sqlite3 openssl \
32+
# Other build dependencies:
33+
argon2 \
34+
automake \
35+
autotools-dev \
36+
dnsutils \
37+
gyp \
38+
insserv \
39+
libc6 libc6-dev libc6-dbg \
40+
libcurl4-openssl-dev \
41+
libedit-dev \
42+
libghc-argon2-dev \
43+
libgtest-dev \
44+
libmcrypt-dev \
45+
libonig-dev \
46+
libpcre3 libpcre3-dev \
47+
libreadline-dev \
48+
libssl-dev \
49+
libsqlite3-dev \
50+
libtool \
51+
libxml2 libxml2-dev \
52+
locales \
53+
locales-all \
54+
netcat-openbsd \
55+
python3-yaml \
56+
${PHP_USER_SPECIFIED_PACKAGES} \
57+
zlib1g-dev
2858

2959
# pgsql extension
30-
RUN apt-get install -y libpq-dev
60+
RUN apt-get install -y --no-install-recommends libpq-dev
3161
RUN docker-php-ext-install pgsql
3262

33-
#
34-
# Other tools
35-
#
36-
RUN apt-get install -y gdb valgrind libcurl4-openssl-dev pkg-config libpq-dev libedit-dev libreadline-dev git
37-
38-
#
39-
# Install other packages.
40-
#
41-
RUN apt-get update && apt-get install -y \
42-
autoconf \
43-
autotools-dev \
44-
build-essential \
45-
bzip2 \
46-
ccache \
47-
curl \
48-
dnsutils \
49-
git \
50-
gyp \
51-
lcov \
52-
libc6 \
53-
libc6-dbg \
54-
libc6-dev \
55-
libgtest-dev \
56-
libtool \
57-
locales \
58-
locales-all \
59-
make \
60-
perl \
61-
strace \
62-
python-dev-is-python3 \
63-
python3-yaml \
64-
sqlite3 \
65-
libsqlite3-dev \
66-
openssl \
67-
libxml2 \
68-
libxml2-dev \
69-
libonig-dev \
70-
libssl-dev \
71-
unzip \
72-
wget \
73-
zip && apt-get clean
74-
75-
#
76-
# Download and install Go
77-
#
78-
RUN arch=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/) && \
79-
wget https://go.dev/dl/go1.21.1.linux-${arch}.tar.gz -O- | tar -C /usr/local -zxvf -;
80-
81-
RUN ln -s /usr/local/go/bin/go /usr/bin/go
63+
# install latest go to work with the daemon
64+
COPY --from=golang /usr/local/go /usr/local/go
65+
ENV PATH /usr/local/go/bin:$PATH
8266

8367
#
84-
# If the debian version is jessie, don't install argon2
68+
# If the debian version is buster, don't install python-dev-is-python3
8569
#
86-
RUN if [ -z "$(grep '^8\.' /etc/debian_version)" ]; then \
87-
apt-get install -y argon2 libghc-argon2-dev; \
70+
RUN if [ -z "$(grep '^10\.' /etc/debian_version)" ]; then \
71+
apt-get install -y --no-install-recommends python-dev-is-python3; \
8872
fi
8973

9074
# install composer
91-
WORKDIR /usr/src
92-
93-
# based on https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md
94-
RUN \
95-
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')" \
96-
&& php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
97-
&& ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")" \
98-
&& if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]; \
99-
then \
100-
>&2 echo 'ERROR: Invalid installer checksum'; \
101-
rm composer-setup.php; \
102-
exit 1; \
103-
fi \
104-
&& php composer-setup.php \
105-
&& php -r "unlink('composer-setup.php');"
75+
COPY --from=composer ["/usr/bin/composer", "/usr/bin/composer"]
10676

10777
#
10878
# The explain plan in the sql tests contain partition/filtered properties
@@ -121,8 +91,16 @@ RUN \
12191
# the mysql tests a separate machine running mysql server 5.6 is required.
12292
RUN docker-php-ext-install pdo pdo_mysql
12393

124-
# redis
125-
RUN pecl install redis && docker-php-ext-enable redis
94+
# install redis extension required by test_redis:
95+
RUN \
96+
php_cmp=$(php -r "echo version_compare(PHP_VERSION, '7.4.0', '>=');"); \
97+
if [ "$php_cmp" = 1 ]; then \
98+
# install latest redis for PHPs >= 7.4
99+
echo 'no' | pecl install redis; \
100+
else \
101+
# install redis-4.3.0 - last one with support for php 7.2
102+
echo 'no' | pecl install redis-4.3.0; \
103+
fi && docker-php-ext-enable redis
126104

127105
# memcache
128106
# Pre 8.0 requires 4.0.5.2
@@ -135,7 +113,7 @@ RUN \
135113
fi
136114

137115
# memcached
138-
RUN apt-get install -y libmemcached-dev
116+
RUN apt-get install -y --no-install-recommends libmemcached-dev
139117
RUN pecl install memcached && docker-php-ext-enable memcached
140118

141119
# uopz
@@ -152,13 +130,9 @@ RUN \
152130
# install predis
153131
# installation will be in /usr/src/vendor/predis/predis
154132
# which is value which should be used for PREDIS_HOME
155-
RUN php composer.phar require "predis/predis"
156-
RUN php composer.phar update
157-
158-
#
159-
# install composer and make executable so it can be used in dev env
160-
#
161-
RUN cp composer.phar /usr/local/bin/composer && chmod +x /usr/local/bin/composer
133+
WORKDIR /usr/src
134+
RUN composer require "predis/predis"
135+
RUN composer update
162136

163137
#
164138
# These args need to be repeated so we can propagate the VARS within this build context.

files/set_path.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ case ":$PATH:" in
1717
*) PATH=/usr/local/bin:$PATH
1818
esac
1919

20-
case ":$PATH:" in
21-
*:/usr/local/go/bin:*) ;;
22-
*) PATH=/usr/local/go/bin:$PATH
23-
esac
24-
2520
export PATH
2621

2722

0 commit comments

Comments
 (0)