Docker Compose for Development

Robioki Denis
3 min readSep 25, 2021
Empowering App Development for Developers | Docker

At the beginning of the development a project, we are often bothered with the installation of programming languages, dependency libraries, third party services (databases, packages) etc. If there are many team members the process must be documented so that the environment for all developers is as expected. Can we do better?

Yes we can!

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.

With docker compose we can save the configuration in a file, meaning that all changes to service dependencies, such as database versions and other services can be included in the VCS (Version Control System). With VCS we can more easily debug if an error occurs in the software.
Another case example is when one of the developers updates certain services such as databases. The software may not run for the previous version of the database, so it might happen “Why doesn’t x feature work?”, “But it works at my place”.

Lets get to the code

An example of an application that we will create is a web using PHP. First create a directory for our project, then create a composer.json file containing the php library dependency.

  1. Create an index.php file to enpoint our software, the script will only display the message “Hello world”

2. Then we create a Dockerfile file to create a docker image for our software.

3. Docker Compose
Run Docker Compose

docker-compose up -d

Install dependencies by running the composer install command in the docker container

$ docker-compose run --rm web composer install

Wait, earlier we ran composer install in the docker container but in the local directory there is a new `vendor` directory? That’s because the local directory is mounted to docker, so all file changes in the container also occur in the local directory, and vice versa.

To see the output issued run the command

docker-compose logs -f

Add Service
Next we will add the redis service for persistent data. Add the following code in the services block in docker-compose.yml

redis:
image: redis:alpine

Add environment variable for web service in ‘environment’ block

Restart docker-compose

$ docker-compose up -d

Install the redis library, inside the docker container

$ docker-compose run --rm web composer require predis/predis

Edit index.php, add the following line

Predis\Autoloader::register();$app->get('/count', function($request, $response) use ($redis) { return $response;
$redis = new Predis\Client([
$redis->incr('counter');
});
$counter = $redis->get('counter');
$response->getBody()->write($counter);
'host' => getenv('REDIS_HOST')
]); $app-run();

View results

docker-compose ps

That’s it….

--

--

Robioki Denis

DevOps Engineer & Fullstack Developer, someone who has a high curiosity 😁