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.
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:
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
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):
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install mysqli
To run, just run:
docker-compose up -d
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:
docker exec -it mysql mysql -p
GRANT ALL PRIVILEGES ON *.* TO myuser@`%` IDENTIFIED BY ‘mypassword’;
This is a fairly simple “Kafka to go” setup for experimentation with the platform:
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:
kafka-topics.sh –create –topic test-topic –replication-factor 1 –partitions 1 –zookeeper zookeeper:2181
(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:
kafka-console-producer.sh –topic test-topic –broker-list kafka:9092
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:
kafka-console-consumer.sh –topic test-topic –from-beginning –zookeeper zookeeper:2181
This will play back all messages from the beginning and then listen for further messages; hit Ctrl+C to abort at any time.