Greetings!
When we talked about Docker, we said that containers are isolated. Then how do we communicate with our containers. Say we are using MySQL database. It is not useful if we can't access it.
Docker has a network concept. It has several network drivers to work with. Depending on how do we want out container to behave, we can select our network. This help us to communicate container with a container or container with host.
Lets list available network commands.
Docker prints the id of the created network. Use the inspect command to see properties. You will see that it has used bridge as the driver since we didn't specify a driver to be used. We can specify a driver using -d option.
We can get the exposed port of an image by inspecting it. Issue the inspect command and see the line for ExposedPorts
We can use -p or --publish option to bind this port to the host's port when running an image.
Now we can access this in our browser.
We can get the containers port using port command
Now when we inspect the container, we can see that it has attached to host's port.
When we talked about Docker, we said that containers are isolated. Then how do we communicate with our containers. Say we are using MySQL database. It is not useful if we can't access it.
Docker has a network concept. It has several network drivers to work with. Depending on how do we want out container to behave, we can select our network. This help us to communicate container with a container or container with host.
Network commands summary
- docker network ls - list available networks
- docker network create - create a network
- docker network rm - remove a network
- docker network inspect - inspect a network
- docker network connect - connect container to a network
- docker network disconnect - disconnect container from a network
Docker network drivers
- bridge - This is the default network. When the Docker daemon service starts, it configures a virtual bridge names docker0. When we don't specify the network this is the one docker uses. Docker creates a private network inside the host which allows containers to communicate with each other.
- host - This tells docker to use host computers network directly.
- none - Disable network for the container.
Network commands
Just like other Docker commands, it has the same pattern.docker network
Lets list available network commands.
docker network help
Inspecting a network
Use the inspect command to inspect a Docker network.docker network inspect bridge
Create a network
We can create our own network using create command.docker network create mynetwork
Docker prints the id of the created network. Use the inspect command to see properties. You will see that it has used bridge as the driver since we didn't specify a driver to be used. We can specify a driver using -d option.
Remove a network
We can use rm command to remove a network.docker network rm mynetwork
Connect to a network
By default our containers connect to bridge network. To use another network, we can use --net option when we create the container.docker container run -it --net=mynetwork nginx
Connect with the world
Now we need to use our containers from the host. There is no meaning of isolating a container if we can't access it.We can get the exposed port of an image by inspecting it. Issue the inspect command and see the line for ExposedPorts
$ docker image inspect nginx
"ExposedPorts": {
"80/tcp": {}
}
We can use -p or --publish option to bind this port to the host's port when running an image.
hostport:containerport
$ docker container run -it -p 81:80 nginx
$ docker container run -it --net=mynetwork -p 81:80 nginx
Now we can access this in our browser.
We can get the containers port using port command
docker port <cotainer_name/id>
Now when we inspect the container, we can see that it has attached to host's port.
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "81"
}
]
},
Comments
Post a Comment