How to automount volumes for Docker containers

3 years ago 402

Looking to adhd persistent retention for your Docker instrumentality deployments? Jack Wallen shows you how, by mode of a WordPress example.

docker-new.jpg

Image: o_m/Shutterstock

There are truthful galore reasons you privation to usage volumes for your container deployments. The superior crushed is to guarantee persistent storage. Say, for example, you're deploying a WordPress lawsuit via a Docker container. Not lone bash you privation to springiness that instrumentality capable retention abstraction to location each of the information it volition necessitate (especially arsenic it scales), you privation to marque definite that information remains successful play, adjacent aft the instrumentality is stopped oregon restarted. For that, you would usage volumes.

SEE: Kubernetes: A cheat expanse (free PDF)  (TechRepublic)

But what happens erstwhile you bash yet restart that Docker container? Will the measurement lodging the information inactive beryllium determination erstwhile the instrumentality comes backmost up? Let's locomotion done the steps for creating and utilizing a measurement specified that it volition ever beryllium determination for your Docker instrumentality deployment.

What you'll need

To marque this work, you'll request a instrumentality with a moving lawsuit of the Docker engine. I'll beryllium demonstrating connected Ubuntu Server 20.04, but it doesn't substance the platform, truthful agelong arsenic Docker is moving properly.

How to make the volume

The archetypal happening we're going to bash is make the directory that volition location the information for a WordPress container. We'll telephone this directory wp-data and make the directory with the command:

sudo mkdir /mnt/wp-data

How to deploy the containers

In bid to bash this right, let's archetypal instal Docker Compose. Do this with the command:

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Make definite to sojourn the Docker Compose download page to guarantee you're downloading the latest mentation of the binary. 

Give the newly-downloaded binary the due permissions with the command:

sudo chmod +x /usr/local/bin/docker-compose

What we'll bash present is make a docker-compose.yml record and make the indispensable manifest for a WordPress instrumentality deployment. Create this record with the command:

nano docker-compose.yml

In that file, paste the following:

version: '3.3' services: db: image: mysql:5.7 volumes: - /mnt/wp-data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: somewordpress MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: wordpress wordpress: depends_on: - db image: wordpress:latest ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_NAME: wordpress volumes: db_data: {}

Notice the lines:

volumes: - /mnt/wp-data:/var/lib/mysql

That is wherever we've defined our volumes for the container.

Save and adjacent the file. 

Deploy the Nextcloud instrumentality with the command:

docker-compose up -d

Give the instrumentality clip to deploy and, erstwhile it has, sojourn http://SERVER:8000 (where SERVER is the IP code of the hosting server) and you should beryllium directed to the WordPress setup (Figure A).

Figure A

wpcontainer.jpg

The WordPress setup is acceptable for you to implicit the task.

Now, if you log backmost into your server and contented the bid ls /mnt/wp-data, you should spot that directory has been populated with files and sub-directories. If you halt that moving instrumentality (using the bid docker halt CONTAINERID - wherever CONTAINERID is the ID of our caller container), you tin past restart it (with the docker-compose up -d command), and everything should beryllium precisely arsenic you near it. Why? Because we've created persistent volumes that automount upon a instrumentality restart. Using volumes this mode not lone ensures your information directories are ever disposable for your containers, but it besides serves arsenic a benignant of automount feature, each clip you re-deploy the container.

Subscribe to TechRepublic's How To Make Tech Work connected YouTube for each the latest tech proposal for concern pros from Jack Wallen.

Data Center Trends Newsletter

DevOps, virtualization, the hybrid cloud, storage, and operational ratio are conscionable immoderate of the information halfway topics we'll highlight. Delivered Mondays and Wednesdays

Sign up today

Also see

Read Entire Article