PHPnews.io

Docker Cheat Sheet for Common Database Engine

Written by JoliCode / Original link on Feb. 18, 2022

I use docker almost everyday, and usually everything is already set-up in the project.

But from time to time I need one or a few tools to do something. For example, when I contribute to a project that require a Database, I need a PostgreSQL container. Or sometime I need to explore some data and I need Elasticsearch or Grafana.

In this short blog post, I'll share with you some containers I use.

Network

Some containers need a network to talk to each other (like Elasticsearch & Kibana or InfluxDB & Grafana). To keep it simple, I put all containers in the same tools network.

docker network create tools

If you forget to select a network when starting a container, you can connect it later with:

docker network connect tools grafana

Container

Almost all tools write data on disk. And I don't want to lose theses data if I drop a container. So I create a named volume for each one.

If you don't remember where to mount the volume, you can use the following command to find it:

$ docker inspect mysql:8 --format '{{ .Config.Volumes }}'
map[/var/lib/mysql:{}]

In this case, the volume must be mounted to /var/lib/mysql.

I also bind the default port of the container to the host, it eases the interaction with other tools on my host.

How to start a PostgreSQL 14.x docker container

docker run -d --name=postgres14 -v postgres14:/var/lib/postgresql/data --network tools -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:14

Then from your host:

PGPASSWORD=password psql -h 127.0.0.1 -U postgres

Or you can enter into the container directly:

docker exec -it postgres14 psql -U postgres

How to start a MySQL 8.x docker container

docker run -d --name mysql8 -v mysql8:/var/lib/mysql --network tools -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql:8

Then from your host:

mysql -h127.0.0.1 -uroot -ppassword

Or you can enter into the container directly:

docker exec -it mysql8 mysql -uroot -ppassword

How to start a Redis 6.x docker container

docker run -d --name redis6 -v redis6:/data --network tools -p 6379:6379 redis:6

Then from your host:

redis-cli

Or you can enter into the container directly:

docker exec -it redis6 redis-cli

How to start a RabbitMQ 3.x docker container

docker run -d --name rabbitmq3 -v rabbitmq3:/var/lib/rabbitmq --network tools -p 5672:5672 -p 15672:15672 rabbitmq:3.9-management

Then you can open http://127.0.0.1:15672/

How to start a InfluxDB 1.7.x docker container

docker run -d --name influxdb1 -v influxdb1:/var/lib/influxdb --network tools -p 8086:8086 influxdb:1.7

The API is available at http://127.0.0.1:8086/

You can create your first database with:

docker exec influxdb1 influx  -execute "create database stat"

Then

docker exec -it influxdb1 influx --database stat

How to start a Grafana (OSS) docker container

docker run -d --name grafana -v grafana:/var/lib/grafana/ --network tools -p 3000:3000 grafana/grafana-oss

Then you can open http://127.0.0.1:3000/

How to start a Elasticsearch (7.x) docker container

docker run -d --name elasticsearch7 -v elasticsearch7:/usr/share/elasticsearch/data --network tools -p 9200:9200 -e ES_JAVA_OPTS="-Xms1g -Xmx1g" -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.13.4

The API is available at http://127.0.0.1:9200/

How to start a Kibana (7.x) docker container

docker run -d --name kibana7 --network tools -p 5601:5601 -e 'ELASTICSEARCH_HOSTS=["http://elasticsearch7:9200"]' docker.elastic.co/kibana/kibana:7.13.4

Then you can open http://127.0.0.1:5601/

Stop / Start

When you don’t need the container anymore, you can stop it with:

docker stop postgres14

And it you need it back:

docker postgres14

Conclusion

Docker is a very powerful tool, and can help to isolate your development environment. It can also help you to test some product before really integrate them in your favorite stack.

I hope you like this cheat sheet. I'll keep it updated, so don't hesitate to bookmark it.

jolicode frankdejonge gabrielaio kibana gabrielaio bram calevans 92 rabbitmq jolicode

« All the pre-conference workshops at SymfonyLive Paris 2022 are announced - Automatic Route Discovery in Laravel »