Container Port Binding:
You might be running multiple containers of the same application like below. Note that redis are using the same port on each container.
On your host machine you need to configure port-binding to have access to multiple containers on the same host machine which are using the same port for example 6379 in our example.
This can be done when running the docker run command. The below command runs redis 6379 in its container and host port 6000 is bound to this container.
docker run -p<host-port>:<container-port> -d <image-name>
Let's run an Nginx container named "mynginxcont" at port 8087 on host machine. If I simply browse 127.0.0.1:8087, I can access to nginx container from my host machine.
You see how easy it is to run many instance of the same application on a host.
Volumes:
Docker volumes allow us to share data between host and container OR between multiple containers.
a) Sharing Volumes Between Host and a Container:
On my host machine I created a folder (web01) that has index.html and styles.css file in the following path
/home/selim/Desktop/web01/
On nginx container the following location is used by default to serve html pages:
/usr/share/nginx/html
On my terminal first I change to web01 directory. Then stopped all the running containers to just make sure no other container is running and confuse us (optional of course). Then run a new container named mywebsite on host port 8088 and defined the two volumes /usr/share/nginx/html and /home/myuser/Desktop/web01 to be bound as read-only.
docker run --name mywebsite -v $(pwd):/usr/share/nginx/html:ro -d -p 8088:80 nginx
If I browse to 127.0.0.1:8088 on my host, I can see that my container is using the path that I defined. So it shows the index.html and its css style. This simply means /usr/share/nginx/html and /home/myuser/Desktop/web01 volumes are bound.
Let's get into our container and see what's going on there. docker exec -it <container-name> bash command lets us to get in a container interactively.
docker exec -it mywebsite bash
If we try to create files or directories in this location (/usr/share/nginx/html) on our container we will get errors like above because it is mounted as read-only. If we make any changes on our host machine (/home/myuser/Desktop/web01), we can see the same folders and files exist on our container as well. I just created a folder named another folder on my host and my container also has it.
We can of course create a writable volume instead. Let me crate another container named mywebsite2 that runs at port 8089 on my host which has write permission on web01 folder. This command almost identical, it just does not use ro flag.
docker run --name <container-name> -v $(pwd):/usr/share/nginx/html -d -p 8089:80 nginx
Let's create a folder named "test4writable" and I created the folder succesfully.
b) Sharing Volumes Between Containers:
Now let me create another container named mywebsite3 from nginx image and mount it to the existing container named mywebsite2.
docker run --name website3 --volumes-from mywebsite -d -p 8090:80 nginx
Now both containers are bound to eachother. Just visit 127.0.0.1:8090 and you will see index.html and its style will be served.