Create a Docker container with MySQL/MariaDB database - mysql

I'm planning to migrate my application stack to Docker. Let me describe the services I'm currently using:
HAProxy, which is used for SSL termination on all service's connections (HTTP and raw TCP connections), and forwards traffic to the services below.
Nginx, which serves static files, like updates and some information pages.
Node.js, which runs the main applications.
MySQL (MariaDB), the database used and shared by all the applications.
My question is about the database.
What's the proper way of running MariaDB in this case?
Install and run it inside my container, along with the other services?
Run the official image in a separate container, and link my container to it with the --link option of Docker's run command?
Does the first option have any disadvantage?
TeamSpeak docker container uses the second option and that's what made me question myself about the correct way of running the database in my case, but I particularly feel more inclined to package all the services inside my own image.

Docker Philosophy: Split your application into microservices and use a container for each microservice.
In your case, I recommend a MariaDB container, Using official (Library) Image gives you easier update management, but feel free to use your custom image.
An HAProxy Container, A nginx container and a nodejs container.
This way you divided your application into microservices, and you can upgrade, manage and troubleshoot them easier in an isolated environment.
If you are thinking about delivering your application to end users via docker, a simple docker-compose file will do the trick for easy launching the required containers.

Related

What is the best way to use mysql server with docker?

I have a golang web application associated with MySQL database. I need to deploy that web application in number of servers provided by different vendors. So I am going to used docker images to deploy this web app. The thing I need to know is, it is okay to keep Mysql server on same docker image or should I make a separate docker image to deploy MySQL on those servers.
A rule of thumb with Docker which you should follow is "One application, one container" It's always the best practice to have separate containers for different parts of your application. The main reason is that down the line if you want to replace MySQL with some NoSQL database, you could simply kill the container and spin up a new one and not worry about it affecting your golang application

Docker image and DB Server integration

We are using Docker Images for Spring Boot Rest Services. the current setup is working fine in Production. We want to use the similar setup in Development Environment. The spring boot image needs to connect to the database. At this point we have couple of options:
Have a centralized database server and have all the docker images from each development machine to connect to it.
Create a separate database image and have the developers run it along with the Spring boot image in the same Dev Machine.
Option #1 is easier to implement but if there is a change in the database, it may impact the whole development community in the organization, Option #2 mitigates that risk but it creates the problem of DataSync i.e when someone starts both these images, how to make sure it has all the required data.
I am wondering if there is any other option I need to consider or given these two options, which makes sense?
I went with option #2, it helps to provide isolated work environment.

Running Spring boot application in a container? or a VM? or a container inside a VM? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have a 96G RAM server, I would like to run a few spring boot applications on it. They all need MySql DB.
I have a hard time to decide what is the best way to utilize the server to obtain the best isolation, and performance.
I am thinking of the following:
Create a VM just for the MySql server
A VM for each spring boot application
Now should I run the mysql/spring boot directly within the VM, or run them in docker? I can see no immediate benefit by doing this. But if later on if I need to create a cluster for my apps, then to have a docker images would be better?
Or, if you were me, what would you do?
Thanks
You want best isolation and performance?
Trust the isolation provided by your Docker container. It's a primary design objective.
Don't add unnecessary layers (i.e. a VM inside which to host your Docker container) — adding a VM layer would incur performance impact, and it sounds like you don't have to.
Containerising MySQL requires thought, since it's inherently stateful.
If you wanted to do this: I'd at least store the state (data and maybe config) outside of the container.
You could get away with not containerising MySQL. I don't feel that databases are a good fit for the containerisation use-case, because:
they're stateful
scaling is not as trivial as "spin up another instance" (because you have to establish slaving, and synchronise and store a lot of state)
they don't undergo updates often
updates are not as trivial as "swap to the newer version of the container"
there's less requirement to "use the same version in all environments" (i.e. devs using MariaDB 5.7 locally, despite production's using MySQL 5.6… this is moreorless fine)
You should also consider using a managed database such as Amazon RDS. I recognise that you've a high-performance computer that you want to make use of, but it's worth weighing that up against the operational costs of maintaining and scaling the infrastructure yourself.
And yes: I'd make a container per Spring Boot application, and run those containers directly. As I said: trust Docker's isolation — or at least look up whether it's been breached, and whether that's an acceptable risk according to your threat model (and whether a VM would've saved you in any reported cases of vulnerability).
As for where to deploy those Docker containers (i.e. locally on your Fast Computer, versus deploying to the cloud): depends whether you want to optimize for operational costs (i.e. it's easier to manage everything on the cloud and not have to interact with any physical machinery) or try to make the most of your Fast Computer (and deploy everything directly to that computer).
Presumably there's some way to remotely manage the orchestration of the Docker containers on your Fast Computer. That could give you a lot of the benefits of deploying to cloud.
What you are looking for is called Docker Swarm. It allows you to deploy dockers (efficient virtual containers) and scale them with any effort.
To "dockerize" your spring boot applications, you only have to build an image with a Dockerfile, like this:
FROM java:8
VOLUME /tmp
ADD spring-boot-0.0.1-SNAPSHOT.jar springboot-appname.jar
RUN bash -c 'touch /springboot-appname.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/springboot-appname.jar"]
To build this image, execute:
docker build -t name-application-img .
To deploy the image as a service inside the Docker Swarm, use:
docker service create -p {exposed-port}:{private-port} --name {service-name} --replicas 1 name-application-img
You can create Docker images of Spring Boot applications, they are so easy to build and scale up and down. Why not even move MySQL too as a Docker image and map the volume to your disk. If you have all applications inside docker they will be easy to manage ( through a docker-compose).
However, the downside is that if you have more than one container of MySQL-DB then you have to worry about data replication and maintaining the same state of DB across multiple DB containers
If I was you I would just dockerize the Spring Boot apps!

Understanding Docker for providing services like web, mysql or similar

I have several questions regarding Docker.
First my project:
I have a blog on a shared host and want to move it to the cloud to have all the server sides in my hands and to have the possibility to scale my server on my needs.
My first intend was to setup a nice ubuntu 14 lts as a server with nginx, php 7 and mysql. But I think it's not that easy to transfer such a server to another cloud i.e. from gce to aws. I then thought about using docker, as a friend told me how easy it is to setup containers and how easy it is to move them from one server to another.
I then read a lot about docker but stumbled upon a few things I wondered about.
In my understanding docker runs just services like php, mysql or similar, but doesn't hold data, right?
Where would I store all the data like database, nginx.conf, php.ini and all the Files I want to serve with nginx (ie. /var/www/)?
Are they stored on the host system? If yes, it would not be easier to move a docker setup then move a whole server, no?
Do I really have an advantage of using Docker to serve a Wordpress Blog or another Website using MySQL and so on?
Thanks in advance
Your data is either stored on the host machine or you data is attached to the docker containers remotely (using a network-attached block device).
When you store your data on the host machine, you have a number of options.
The data can be 'inside' one of your containers (e.g. your mysql databases live inside your mysql container).
You can mount one or more directories from your host machine inside your containers. So then the data lives on your host.
You can create Docker volumes or Docker volume containers that are used to store your data. These volumes or volume containers are mounted inside the container with your application. The data then lives in directories managed by Docker.
For details of these options, see dockervolumes
The last option is that you mount remote storage to your docker containers. Flocker is one of the options you have for this.
At my work I've set up a host (i.e. server) that runs a number of services in docker containers. The data for each of these services 'lives' in a Docker data volume container.
This way, the data and the services are completely separated. That allows me to start, stop, upgrade and delete the containers that are running my services without affecting the data.
I have also made separate Docker containers that are started by cron and these back up the data from the data volume containers.
For mysql, the backup container connects to the mysql container and executes mysqldump remotely.
I can also run the (same) containers that are running my services on my development machine, using the data that I backed up from the production server.
This is useful, for instance, to test upgrading mysql from 5.6 to 5.7.

How to connect Wordpress and MySql running on independant containers

Wordpress is running inside a Docker container on hostA and MySQL is running inside a Docker container on hostB. Is it possible to link these two containers to communicate to each other? Is this even possible to do something like this?
Any help on this is much appreciated as am pretty new to Docker
I can not answer you question but there is a part in the documentation about this:
https://docs.docker.com/engine/userguide/networking/default_network/container-communication/
You will find a section called: Communication between containers
Yes this is possible with docker overlay network.
The setup is not as easy as setting up a link or private network on the same host.
You will have to configure a key value store to get this working.
Here is the relevant docker documentation.
An overlay network:
https://docs.docker.com/engine/userguide/networking/dockernetworks/#an-overlay-network
Here are the steps for setup
https://docs.docker.com/engine/userguide/networking/get-started-overlay/
In my opinion, its not bad to isolate the app and database containers and connect outside the docker network. If you end up adding the key/value store like consul, you can always leverage the service discovery that comes along with it to dynamically discover the services.
I would go for https://github.com/weaveworks/weave.
Weave Net creates a virtual network that connects Docker containers across multiple hosts and enables their automatic discovery.
It might be overkill for your usecase. But it would be very helpful if you want to move the containers around in the future.