DEV Community

Cover image for Docker continued - Volumes & Network (part-2)
Shubham Tiwary
Shubham Tiwary

Posted on

Docker continued - Volumes & Network (part-2)

In case you haven't followed up, first go through the Docker Part-1, this is a continued series from that previous iteration..

Volumes in Docker

Previously, we see that docker containers are "stateless", they can be exposed to outside world, but don't have state persisting, or interaction between containers.

volume-preview

With volumes, you have a directory bind between one in docker container to your system.
So you can do things like:

  • make code changes in your machine /dir and it appears on the container where that's bind to. (no need to re-build image+container on code changes)
  • delete container and the data in your system still exists.

Assigning volumes to our container

Back to our express image from previous part, we attach a volume

docker container run -d --name express-app-vol -v /home/dev/work/tests/demo:/app -p 3000:3000 express-dummy:v1
Enter fullscreen mode Exit fullscreen mode

I have my contents of express app in /home/dev/work/tests/demo, which i attach to the /app in container (yours might be different)

Now let's make a simple change in our code (/home/dev/work/tests/demo/index.js in my case) :

const express = require('express')
const app = express();
require('dotenv').config()

const port = 3000;
const num = process.env.num;

app.get('/', (req,res)=>{
  res.send(`Hello ${num}!!!!`);  ## <-- added some "!!!!" at end
})

app.listen(port, ()=>{
  console.log(`Server connected at port: ${port}`)
})
Enter fullscreen mode Exit fullscreen mode

With these code changes, lets stop & restart container:

docker stop express-app-vol
docker start express-app-vol
Enter fullscreen mode Exit fullscreen mode

And response should be something like:

volume-container-response


Container Networks.

Suppose there's two containers:

  • Redis container running on port 6379.
  • Redis-Insight container running on port 5540 (connect to redis & test on a GUI in browser)

Run them as follows:

docker run --name redis-container -p 6379:6379 -d redis
docker run -d --name redisinsight -p 5540:5540 redis/redisinsight:latest
Enter fullscreen mode Exit fullscreen mode

With this setup, let's try testing redis from redis-insight:

redis-insight-fail

So this is redis insight, and it seems to not find any redis DB on port 6379, why is that?
Since the network environment is different for these containers.


Try running the following command:

docker network ls
Enter fullscreen mode Exit fullscreen mode

It lists down all network present, you'll see these by default:

  • bridge: default network driver.
  • host: between container and the docker host.
  • none: completely isolated

Checking the containers inside the bridge network (default):

docker network inspect bridge
Enter fullscreen mode Exit fullscreen mode

Go down to the "containers" in the response:

bridge-containers

you'll notice the redis & redis-insight container, they can talk with your machine but not with each other!


Our Custom Network Group.

Let's create our own custom network (we'll place our containers here afterwards):

#network called "redis-line"
docker network create redis-line 
Enter fullscreen mode Exit fullscreen mode

Then, let's add the two containers here:

#add redis-container & redisinsight (the containers) into redis-line network
docker network connect redis-line redis-container
docker network connect redis-line redisinsight
Enter fullscreen mode Exit fullscreen mode

Now, let's inspect the network with:

docker network inspect redis-line
Enter fullscreen mode Exit fullscreen mode

You'll notice the containers added inside the "Containers" parameter.

Now see the "Name", i.e: "redisinsight" & "redis-container". So now when you have to refer to other container, instead of putting an ip, you use these names, example:

redis-insight-connected

So here in the "host", we put "redis-container" which is like its domain in that network.

And from that, it is connected and works!

redis-workbench


Just reading through these isn't enough, what might work here might not for you, so its best you try these on your own..

Top comments (0)