You are on page 1of 4

4.

Networking
By default when installing docker you’re presented with 3 networks

bridge → Default network ( All containers on that network can


communicate with each other using their assigned IP addresses )

$ docker run -it --name c1 alpine


$ docker run -it --name c2 alpine

In that example we have 2 containers c1 & c2, if you inspect both of


them using the

$ docker inspect c1
$ docker inspect c2

You will find both of them are on the same network, probably with IP
addresses like (172.18.0.2) and (172.18.0.3)

If you try to get into one container ( $ docker attach c1 ) and then ping
the other one ( $ ping 172.18.0.3) you’d get a normal response
indicating that both containers can communicate with each other.

You can even create a new network with the type Bridge like that

$ docker network create first_network

Now if you try to create new containers with that new network

$ docker run -it --name c3 --network first_network alpine


$ docker run -it --name c4 --network first_network alpine

Like before if we inspect the 2 containers for their IP addresses and


then try to ping one of them from the other we find that they are
connected.

By: Ahmad ElBatanouni


What’s different here is that if you try to ping c3 or c4 from another
container like c1 or c2 you will get no response at all.
The reason for this is that
c3 and c4 use the network first_network
c1 and c2 use the network bridge

Indeed both networks are of type Bridge, but still .. they are 2 different
networks.

Moreover, if you create a new bridge network it provides some more


features like
1. DNS resolution
so instead of pinging each other using the IP address you
can ping using the container name like this:

$ ping 172.19.0.3
$ ping c4
Both of them are the same .. c4 will be automatically
resolved by Docker to the corresponding container IP.

Note that we can’t do the same thing with the default


network “bridge”.

2. Containers can be attached and detached from user-defined


networks on the fly.

During a container’s lifetime, you can connect or disconnect


it from user-defined networks on the fly.
To remove a container from the default bridge network, you
need to stop the container and recreate it with different
network options.

$ docker network create n1


$ docker run -it --name c5 alpine

By: Ahmad ElBatanouni


$ docker network connect n1 c5
$ docker network inspect n1

Here,You’ll find C5 listed as a container using this network

$ docker network disconnect n1 c5


$ docker network inspect n1
Here,You’ll not find C5 any more in the containers list using the network

none → Self explanatory … none is used when you don’t want the
container to have any type of networking

$ docker run --network none wordpress

To make sure this is running you can either inspect the container itself
or the none network

$ docker network inspect none

$ docker inspect <container-id>

Note that other types also exist like the host, overlay, macvlan,
but those are out of the scope of this course

If you want to fine more about them please refer to the network guide.

5. Creating images using Dockerfile


Let’s imagine you have a simple application written in Dart, and you
want to send it to your friend … That app uses
➔ Flutter version 1.5
So you ask your friend to install those first before trying to run the
application.

By: Ahmad ElBatanouni


He sounds a little bit hesitated … he had a bad experience before with
installing an old version of Flutter.

Now what if we created a docker Image that holds all that … the app,
Dart, and the needed version of flutter.. so that friend can just get the
app up and running with a single command .. something like

docker run -it {image_name}

In order to do that we need to follow a few steps


1. Create a file called Dockerfile
2. Add Dockerfile instructions.
3. Build the image using a command like
docker build -t myimage:version1.0 /home/ahmad/test

Replace {/home/ahmad/test} with the path to the folder containing


Dockerfile

OR
If you’re on the same location as the Dockerfile, you can run
docker build -t myimage:version1.0 .
4. Push the image to docker hub ( https://docs.docker.com/get-
started/04_sharing_app/ )
5. Your friend can simply then pull that image from Docker hub like
we did before

Assignment
Create a docker image that
✔ includes a program written in Java which prints your name
✔ the program should be run once a container is created using your
docker image
then push the image to your docker hub account and share the image
link to the class room (Class code: zpyntoe)
You can use any base image of your choice, doesn’t have to be
openjdk.

Sample docker image: https://hub.docker.com/r/os2021/test

By: Ahmad ElBatanouni

You might also like