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
publish
orp
specifies the port mapping, from the host port outside the container to the expected ports within the container.name
orn
specifies the name to refer to the container asenv
ore
specifies environment variables to be accessible within the container- For MySQL you need to specify the password situation:
- Either
MYSQL_ROOT_PASSWORD=SomePassword
orMYSQL_ALLOW_EMPTY_PASSWORD
- Either
- For MySQL you need to specify the password situation:
detach
ord
specifies 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
p
specifies the port mapping, from the host port outside the container to the expected ports within the container.name
specifies the name to refer to the container asd
specifies 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