Kali ini saya akan berbagi sedikit tutorial tentang Docker Compose yaitu menulis manifest MySQL dengan Adminer, tujuan dari tutorial ini adalah membantu temen-temen melakukan setup service database MySQL sekaligus menggunakan Adminer sebagai manajemen databasenya menggunakan Docker Compose. Show
Tidak susah untuk temen-temen coba. Apalagi temen-temen yang sering menggunakan Docker tentu masalah ini sangat easy. 😀 Docker ComposeNamun, menggunakan teknologi Docker + Docker Compose untuk menjalankan service-nya. Baca Juga: Tutorial Docker Compose PostgreSQL dengan Adminer Langkah-langkah Setup MySQL + Adminer dengan Docker ComposeIkuti langkah-langkah berikut agar proses 1. Buat Folder untuk Menyimpan Database MySQLSilahkan buat folder dengan nama 2. Buat File Docker ComposeBerikut manifest-nya: version: '3' services: mysql: image: 'mysql' ports: - '3306:3306' environment: MYSQL_ROOT_PASSWORD: root123 MYSQL_DATABASE: dbstart MYSQL_USER: userstart MYSQL_PASSWORD: mysql123 networks: - 'database-net' volumes: - ./data:/var/lib/mysql adminer: image: 'adminer' ports: - '8081:8080' networks: - 'database-net' networks: database-net: 3. Jalankan Docker ComposeSetelah langkah sebelumnya selesai dilakukan maka selanjutnya tinggal jalankan docker compose, yaitu dengan menggunakan syntax berikut: MySQL is one of the most popular relational databases of all time. Using MySQL with Docker and docker-compose makes it very easy and fast to test out any changes in any application using MySQL as the database. In this tutorial, we will detail how to use MySQL with Docker and docker-compose step-by-step keeping things easy to comprehend. Let’s get rolling! Table of contentsWhy use MySQL with Docker for local developmentThere are multiple great reasons to use any database including MySQL with Docker for local development, some of them are as follows:
To know the above reasons in a bit more detail please do read the why use docker post. PrerequisitesBefore we get our hands dirty with some code and CLI commands, below are some good to have prerequisites:
Now as you are aware of the prerequisites, we can dive deeper into the CLI commands and any related code. Run MySQL with DockerFor this beginners’ guide, we will use the official MySQL Docker image from DockerHub. The official MySQL Docker image does not have an Alpine Linux version, still, the Debian version is also 147 MB which is not too big for a docker image. To run the MySQL 8.0 container using the official image, simply run the following command:
Let’s analyze the command we just ran to run MySQL with docker. First, we created a directory called
It will show and output simialr to below:
As the MySQL server is running we can execute the MySQL command inside the container with:
It will look like the below: We can stop the container with 9.Even though that wasn’t too hard I would not consider it easy as well as the command’s parameters were not simple to remember. Another aspect is we ran MySQL in isolation, there is no link between the Quotes API Node.js application and the MySQL container. This is where the declarative nature of docker-compose comes in very handy as we will see in the next section. Running MySQL with docker-composeTo run the same MySQL 8.0 with docker-compose we will create a new docker-compose-mysql-only.yml file, with the following contents:
The above docker-compose file has the following things to comprehend:
We can start the MySQL container this time with docker-compose using:
It will show an output like below:
The output is similar but this time being declarative with docker-compose, running MySQL inside a docker container was a much easier command. Adding MySQL to and existing Node.js app using docker-composeAt this point, we have run MySQL with just docker and then with docker-compose which was much easier than the previous long command. Still, one piece of the puzzle is missing, the piece to link the MySQL container with an application. In our case, it will be the Node.js MySQL API for Quotes. For this beginner’s tutorial we will add a new 9 with the following contents:
This docker-compose.yml file looks like a longer version of the above file. The main differences include: Adding the API service which uses the 0 included in the project. We will build the 1 service with the target production. As per your interest, you can read more about docker multi-stage builds which explains the use of build target.Next up, we indicate to docker-compose that the API depends on the DB container. After that, we map the host port 2 to the container port 2 as the Express.js app with Node.js runs on port 2.Subsequently, we set all the needed environment variables to that the Node.js app connects to to the MySQL database running inside the container without problems. After that, we map the local file in 5 to 6 in the container so that all the file changes are reflected inside the container. Next, we link both containers so that the API container can communicate with the DB container.This 7 file is available as a for your reference.When we run the application with 8 we will see output like the below:
If we hit the browser with 9 we can see something like the following:Great! Our Node.js Express Quotes API is communicating properly with the local MySQL running inside the docker container and connected with docker-compose. As the data was put from the 7 script the quotes are available in the API response.Check out this step-by-step tutorial if you want to run Postgres with Docker. ConclusionIn this guide, we saw how to run MySQL with docker run, then docker-compose, and finally linked up with a Node.js application step-by-step. |