Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn OpenShift

You're reading from   Learn OpenShift Deploy, build, manage, and migrate applications with OpenShift Origin 3.9

Arrow left icon
Product type Paperback
Published in Jul 2018
Publisher Packt
ISBN-13 9781788992329
Length 504 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (3):
Arrow left icon
Denis Zuev Denis Zuev
Author Profile Icon Denis Zuev
Denis Zuev
Aleksey Usov Aleksey Usov
Author Profile Icon Aleksey Usov
Aleksey Usov
Artemii Kropachev Artemii Kropachev
Author Profile Icon Artemii Kropachev
Artemii Kropachev
Arrow right icon
View More author details
Toc

Table of Contents (24) Chapters Close

Preface 1. Containers and Docker Overview FREE CHAPTER 2. Kubernetes Overview 3. CRI-O Overview 4. OpenShift Overview 5. Building an OpenShift Lab 6. OpenShift Installation 7. Managing Persistent Storage 8. Core OpenShift Concepts 9. Advanced OpenShift Concepts 10. Security in OpenShift 11. Managing OpenShift Networking 12. Deploying Simple Applications in OpenShift 13. Deploying Multi-Tier Applications Using Templates 14. Building Application Images from Dockerfile 15. Building PHP Applications from Source Code 16. Building a Multi-Tier Application from Source Code 17. CI/CD Pipelines in OpenShift 18. OpenShift HA Architecture Overview 19. OpenShift HA Design for Single and Multiple DCs 20. Network Design for OpenShift HA 21. What is New in OpenShift 3.9? 22. Assessments 23. Other Books You May Enjoy

Using persistent storage

In the previous sections, we saw that containers can be created and deleted easily. But when a container is deleted, all the data associated with that container disappears too. That is why a lot of people refer to containers as a stateless architecture. But we can change this behavior and keep all the data by using persistent volumes. In order to enable persistent storage for a Docker container, we need to use the -v option, which binds the container filesystem to the host filesystem that runs that container.

In the next example, we will create a MariaDB container with persistent storage in the /mnt/data folder on the host. Then, we delete the MariaDB container and recreate it again using the same persistent storage.

First, remove all previously created containers:

$ docker rm -f $(docker ps -aq)

We have to prepare persistent storage on the node before we begin. Be aware that we need to give read/write permissions to the persistent storage directory. The MariaDB application works with a MySQL user with UID=999 inside the container. Also, it is important to mention that the special SE Linux security context svirt_sandbox_file_t is required. This can be achieved using the following commands:

# mkdir /mnt/data
# chown 999:999 /mnt/data
# chcon -Rt svirt_sandbox_file_t /mnt/data

The next step is to create the container running the MariaDB service:

$ docker run -d -v /mnt/data:/var/lib/mysql --name mariadb -e MYSQL_ROOT_PASSWORD=password mariadb
41139532924ef461420fbcaaa473d3030d10f853e1c98b6731840b0932973309

Run the docker ps command:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
41139532924e mariadb "docker-entrypoint.sh" 4 seconds ago Up 3 seconds 3306/tcp mariadb

Create a new database and verify the existence of this new DB:

$ docker exec -it mariadb mysql -uroot -ppassword -e "create database persistent;"

$ docker exec -it mariadb mysql -uroot -ppassword -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| persistent |
+--------------------+

Verify that there is new data in the /mnt/data directory created by the mariadb container. This is how we make the data persistent:

$ ls -l /mnt/data/
drwx------. 2 polkitd ssh_keys 4096 Mar 6 16:18 mysql
drwx------. 2 polkitd ssh_keys 20 Mar 6 16:18 performance_schema
drwx------. 2 polkitd ssh_keys 20 Mar 6 16:23 persistent
...
output truncated for brevity
...

Delete the mariadb container and verify that all files will be kept:

$ docker rm -f mariadb
mariadb

$ ls -l /mnt/data/
drwx------. 2 polkitd ssh_keys 4096 Mar 6 16:18 mysql
drwx------. 2 polkitd ssh_keys 20 Mar 6 16:18 performance_schema
drwx------. 2 polkitd ssh_keys 20 Mar 6 16:23 persistent
...
output truncated for brevity
...

We are going to rerun the container and verify whether the previously created database persistent survived container removal and creation:

$ docker run -d -v /mnt/data:/var/lib/mysql --name mariadb mariadb
c12292f089ccbe294cf3b9a80b9eb44e33c1493570415109effa7f397579b235

As you can see, the database with the name persistent is still here.

Remove all the containers before you proceed to the next section:

$ docker rm -f $(docker ps -aq)
You have been reading a chapter from
Learn OpenShift
Published in: Jul 2018
Publisher: Packt
ISBN-13: 9781788992329
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image