Database Containerization
Containers are basic execution environments that are sandboxed and contained within sandboxed portions of an operating system. They are very useful for developing, testing and even deploying databases. See containers for details.
Pros and Cons
Pros
- Containers are lightweight and easy to spin up and down.
- Containers are easy to share and distribute.
- Containers are easy to version and track.
- Containers make it easier to reset the state of a database.
- Easy to segregate databases from each other.
Cons
- Containers are not as performant as native databases
- Especially in I/O performance, a crucial performance metric for databases.
- Container databases are harder to backup and restore without spinning down first.
- Accessing logs is harder with container runtimes.
Running MySQL in Docker
You can run various SQL based databases in containers. In this example the MySQL container is run using Docker.
docker run \
--publish 3306:3306 \
--env MYSQL_ROOT_PASSWORD='root' \
--name my-mysql \
--detach \
mysql
publishorpspecifies the port mapping, from the host port outside the container to the expected ports within the container.nameornspecifies the name to refer to the container asenvorespecifies environment variables to be accessible within the container- For MySQL you need to specify the password situation:
- Either
MYSQL_ROOT_PASSWORD=SomePasswordorMYSQL_ALLOW_EMPTY_PASSWORD
- Either
- For MySQL you need to specify the password situation:
detachordspecifies that the container should run detached, as in not piping into the shell, running in backgroundmysql [name]the final specifier is the name of the container image to download, in this casemysql
Running MongoDB in Docker
Here [MongoDB] is run in a Docker container.
docker run \
--publish 27017:27017
--name some-mongo
--detach \
mongo
pspecifies the port mapping, from the host port outside the container to the expected ports within the container.namespecifies the name to refer to the container asdspecifies that the container should run detached, as in not piping into the shell, running in backgroundmongo [name]the final specifier is the name of the container image to download