Once it starts up, let's run a psql session inside it and check that the database, table and data are still all present and correct: docker exec -it postgres2 shĪnd now, let's really clean up. Let's now start up a brand new container called postgres2 but attach the existing postgres-data volume that contains our database: docker run -d -p 5432:5432 -v postgres-data:/var/lib/postgresql/data -name postgres2 postgres docker rm -f postgres1Īttach an existing volume to a new container Because the data is stored in a volume, that is still safe. Let's stop and remove the postgres1 container with a single command ( -f forces it to remove a running container). In a production environment like Azure, you'd most likely mount an Azure file share as a volume. Obviously a Docker volume doesn't need to be stored on local disk on the Docker host. $ ls /var/lib/docker/volumes/postgres-data/_data/ "Mountpoint": "/var/lib/docker/volumes/postgres-data/_data",Īnd if we take a look inside the local folder on our Docker host, we can see all the data that has been stored in that volume. We can find out information about the volume that we've created with docker volume inspect, including where on our local disk the data in that volume is being stored. Now we can quit from psql and exit from our shell, which will remove the client1 container since we specified the -rm flag to auto-delete the container when the command it was running exits. Now from this client1 container we can access data in the database stored in the postgres1 container: mydb=# SELECT * FROM people Launch psql but connect to the other container ( -h) which we've given the name pg in our link configuration: # psql -U postgres -h pg mydb docker run -it -rm -link postgres1:pg -name client1 postgres sh docker stop postgres1Īnd rather than connect again to this container, let's test from another linked container, using the same technique for linking containers we saw in our Redis demo. Let's prove that we don't lose the data if we stop and restart the container. Of course our postgres1 container is still running. Now we can quit from psql with \q and exit from our shell mydb=# \q Then we'll insert a row into the table: mydb=# INSERT INTO people (id,name) VALUES (1, 'Mark') Īnd finally, check it's there mydb=# SELECT * FROM people We'll create a table: mydb=# CREATE TABLE people (id int, name varchar(80)) Now let's do something a bit more interesting. We'll also ask for the database version, and the current date: mydb=# \l Now inside psql, let's run some basic commands.
# createdb -U postgres mydbĪnd then let's launch the psql utility which is a CLI tool for PostgreSQL, connected to our mydb database: # psql -U postgres mydb Inside that shell we can ask it to create a new database with the name mydb. This saves us from needing to have any tools to connect to and manage PostgreSQL databases installed locally. We'll create a database and one easy way to do that is by using docker exec to launch an interactive shell running inside our postgres1 container, which has the PostgreSQL CLI tools installed. Once we've done this we can check it's running with docker psĪnd view the log output with docker logs postgres1 docker run -d -p 5432:5432 -v postgres-data:/var/lib/postgresql/data ` PostgreSQL stores its data in /var/lib/postgresql/data, so we're mounting our volume to that path.
The volume name will be postgres-data, and Docker will automatically create it (just using storage on the Docker host's local disk) if a volume with this name doesn't already exist. We're running detached ( -d) mode (so in the background).īut we're also going to mount a volume (with -v), which will be used to store the database we create. We'll use docker run to start a new container from the official postgres image with the name postgres1 and exposing port 5432 (the PostgreSQL default). But another great option is Play with Docker which lets us run all these commands in the browser.
#Docker remove container keep logs windows
If you're running Docker for Windows put it in Linux mode. You can follow along with the commands in this tutorial if you have Docker installed. Today we're going to look at PostgreSQL which will give us an opportunity to see Docker volumes in action. Last time we looked at Redis, and that gave us the opportunity to see the docker run and docker exec commands in action.
#Docker remove container keep logs series
This is the second part of a series looking at how easy Docker makes it to explore and experiment with open source software.