How to Mount Docker Volume into a Container

Introduction

In this article, you’ll understand How to Mount Docker Volume into a Container and also store associated data inside the container. Please, read our previous article before proceeding to this article where we learn how to execute a command inside the container.

Mount data volume in the container

Docker containers are designed to run your applications, but they’re not intended for storing data long term. If you want to upgrade an application, you don’t upgrade the software that’s inside the container. Instead, you dispose of the container and create a new one with an updated version.

But, what if you have data associated with your content that you want to keep? So, that’s where Docker volume comes in. We can mount a volume to a container, which gives it a place to write data that has an independent life of the container itself. And that means that if we delete the container, we can reattach the same docker volume to a new container.

So, let’s see this in the below example with a Postgres database. Here’s the command that we’re going to run to create a Docker container running Postgres.

Postgres container

And we’ve seen some of these flags before. We’re running in detached mode, So in the background, and we’re publishing the default Postgres port of 5432, which means that we can connect to this database from our local machine if we want to. And we’re naming the container postgres1, and we’re using a Postgres image, which, again, will be downloaded from Docker Hub. And to set up a docker volume, we’re using the -v argument.

postgres-data:/var/lib/postgresql/data

The first part of this is the docker volume name, postgres-data, and the second is the location inside the container that these docker volumes will be mapped to. In this case, var/lib/postgresql/data, which is the default location that Postgres will store any databases we create in. And so as far as Postgres is concerned, it’s writing to that location, but actually, anything that’s written there will end up in our postgres-data volume.

Now, we haven’t yet got a docker volume called postgres-data, but Docker will create one for us to use the first time we run this command. So, if we run this, it’s, first of all, going to download the Postgres image if we don’t already have it locally.

postgres image download

So, it’s going to start my container running in the background. So we’ll just wait a moment for this to complete. And now, let’s use the same technique that we used before to execute some commands within the container.

here, we going to do a docker exec on my postgres1 container and open another shell inside it.

docker container shell

After that, when we’re inside that shell, we can create a new database with the CreateDb command specifying the Postgres user and a database name of mydb.

docker exec command

Now, we’re going to use psql, which is the postgres-cli to connect to that database with the following command, again, specifying the Postgres user and what database we want to communicate to.

docker psql user

Actually, these tools are not installed on our local Windows machine. we’re running them inside the container. And so now we’re connected to that mydb database. Let’s create a table called people with a CREATE TABLE command, and let’s just insert a new row into that table.

postgres insert table

here, we going to exit the psql CLI with \q, and we’re also going to exit from this interactive shell session with exit.

exit container shell

Access Docker Volume to Another Container

So, in our container, we’ve created a new database, and we’ve put some data in, but we’re actually going to delete the container. Now, we do docker stop and then docker rm, but we can do both of those in one step if we execute docker rm -f to force it to stop and the name of the container we want to delete.

docker remove command

So, now we’ve deleted our postgres1 container, but our database isn’t lost because that was storing its data into a volume. And so if we run the docker volume ls command, which will show me all the docker volumes we have,

docker volume list

we can see that the postgres-data volume is still present even though the container that was using it has been deleted. And so what that means is we can now start a brand-new container from the Postgres image and mount that same docker volume. And so let’s call this new container postgres2. And we start it up with very much the same command that we saw before. we’re mounting the same volume to the same location, and the only difference is we’re giving this a new name. And this time it’s super quick to start up because we’ve already got the Postgres image locally on our machine.

Postgres clone image

Let’s use the docker exec command again to run an interactive shell on this container, which will use exactly the same docker exec command that we saw before,

Postgres shell

In the postgres2 container. Once we’re in, we’ll run the psql command-line tool again and connect to the mydb database. And we can do that because the volume has already got that database in it. And so once we’re connected to that database, we can run a basic SQL query and see that the data is still there.

display postgres data in container

So, we’ve managed to persist the database that we created in one container, deleted the container, and then reattached it to a completely different container.

Let’s exit the psql with \q and exit from our shell with exit. then we’ll delete this container with docker rm -f for force and postgres2. And if we decided that we don’t want my database anymore, we can delete that with docker volume rm and then the name of the volume, which is postgres-data. And this deletes the volume which contains the database we created.

docker container exit and remove

I hope you have been enjoyed this article. We’ll get into more details about this when we’ll Learn Docker Networking in the next article.

Leave a Reply

Your email address will not be published. Required fields are marked *