Some docker-compose recipes

This article will go through a set of simple step by step instructions to set up Docker containers for various popular services using docker-compose.

Always remember that you can combine or split these recipes as you want so as to create your perfect mix of services; these are mostly laid out according to how I first used them or how I typically use them.

BASIC USAGE

This setup assumes Docker installed on a Mac. Other operating systems will vary a bit but the differences shouldn’t be too significant.

Each service can be run using:
[code]docker-compose up[/code]

Or, perhaps more typically, if you don’t want to bind to the terminal:
[code]docker-compose up -d[/code]

Files provided expose services to equivalent local ports on your workstation, so before you get started on each one, make sure you don’t have same services running locally (e.g. a local Apache server would stop the Nginx setup below from working as it would already have claimed port 80 on your computer).

Also, directories mounted into Docker images in the examples below will typically use my username (replace with yours) and assumes source directories are organised under ~/src — adjust so it matches your setup. Note that ~/src/docker is used as a directory to store data on the host file system in the examples below — for example the MySQL data directory. This allows you more transparent control of files from Docker images and improved persistence, but feel free to adjust as you see fit.

PHP + MySQL + Nginx

docker-compose.yml
[code]
version: ‘3’

services:
db:
image: mariadb:latest
container_name: mysql
restart: always
ports:
– "3306:3306"
volumes:
– /Users/stephan/src/docker/mysql/data:/var/lib/mysql
– /Users/stephan/src:/home/stephan/src
– /Users/stephan/src/docker/mysql/etc:/etc/mysql/conf.d
environment:
MYSQL_ROOT_PASSWORD: root-password
MYSQL_DATABASE: database-name
MYSQL_USER: database-user
MYSQL_PASSWORD: database-user-passsword
php:
build:
context: ./
dockerfile: Dockerfile-PHP71
container_name: php
volumes:
– /Users/stephan/src:/home/stephan/src
– /Users/stephan/src/docker/nginx/sites-enabled:/etc/nginx/sites-enabled
links:
– db
web:
image: nginx:stable
container_name: web
ports:
– "80:80"
– "443:443"
volumes:
– /Users/stephan/src:/home/stephan/src
– /Users/stephan/src/docker/nginx/sites-enabled:/etc/nginx/conf.d
– /Users/stephan/src/docker/nginx/certs:/etc/nginx/certs
links:
– php
[/code]

We need an extra file to explicitly install some PHP extensions for it to work with MySQL, store a file in the same directory called “Dockerfile-PHP71” (this is referred to in the Compose file above):
[code]
FROM php:7.1-fpm
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install mysqli
[/code]

To run, just run:
[code]
docker-compose up -d
[/code]

Once running, if you need to reload Nginx configuration (which is in a directory that’s mounted from your computer’s file structure if you followed the pattern above), simply execute:

[code]docker exec -ti web bash -c ‘kill -HUP 1′[/code]

You will now be able to access the web server by pointing your web browser to your local host (alternatively via entries in the hosts file pointing to 127.0.0.1).

MySQL is available on port 3306 (the standard port) on 127.0.0.1 — note that you cannot connect naming localhost, as MySQL clients will then typically try to use socket connections.

If you need to connect to the MySQL host to add user privileges, just run:
[code]
docker exec -it mysql mysql -p
[/code]

Then
[code]
GRANT ALL PRIVILEGES ON *.* TO myuser@`%` IDENTIFIED BY ‘mypassword’;
FLUSH PRIVILEGES;
[/code]

Kafka

This is a fairly simple “Kafka to go” setup for experimentation with the platform:

docker-compose.yml
[code]
version: ‘3’

services:
zookeeper:
container_name: zookeeper
image: jplock/zookeeper
restart: always
ports:
– "2181:2181"
kafka:
container_name: kafka
image: ches/kafka
restart: always
ports:
– "9092:9092"
environment:
ZOOKEEPER_IP: zookeeper
links:
– zookeeper
[/code]

Replace 192.168.0.10 above with the IP of your local workstation.

After getting the images online, you can connect to the Kafka instance to create a topic:

[code]docker exec -it kafka bash[/code]

Once in run this:
[code]
export JMX_PORT=9998
kafka-topics.sh –create –topic test-topic –replication-factor 1 –partitions 1 –zookeeper zookeeper:2181
[/code]

(Note that the export line is needed before you run any command line tools but will persist throughout your command line session — the CLI tools will otherwise try to bind to the same port as the main Kafka instance which will lead to a conflict; the port used is not important)

Which will create a new Kafka Topic; we can now test adding to the topic and read it back via the CLI as such:

[code]
kafka-console-producer.sh –topic test-topic –broker-list kafka:9092
[/code]

Then enter three sample messages (each line is a message):
… message 1 …
… message 2 …
… message 3 …

End with Ctrl+D

This can now be replayed using a consumer:
[code]
kafka-console-consumer.sh –topic test-topic –from-beginning –zookeeper zookeeper:2181
[/code]

This will play back all messages from the beginning and then listen for further messages; hit Ctrl+C to abort at any time.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *