Run Ubuntu In Docker Container

Need Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on.

As you build your distributed application, the services is composes of will need to be able to communicate with each other. These services, running in the containers, might be on a single host, on multiple hosts or even across data centers - therefore container networking is a critical factor of any Docker-based distributed application.

Networking with docker containers is a very important featured of Docker. The feature allows users to define their own networks and connect containers to them. You can create a network on a single host or a network that spans across multiple hosts using docker network feature.

Oct 02, 2021 Run Ubuntu Docker Container On Mac The -privileged flag gives all capabilities to the container, and it alsolifts all the limitations enforced by the device cgroup controller. In otherwords, the container can then do almost everything that the host can do. Run Ubuntu In Docker On Windows. Run a Docker Container in Ubuntu. In order to create and run a Docker container, first you need to run a command into a downloaded CentOS image, so a basic command would be to check the distribution version file inside the container using cat command, as shown. $ docker run centos cat /etc/issue 14.

In this article, we will learn some basic and advanced networking tools that you can use for managing Docker Containers.

  1. Docker will cache these, so when we run the container we don’t need to download the container image(s) each time. Docker maintains a site called Dockerhub, a public repository of Docker files (including both official and user-submitted images).
  2. Run Ubuntu Docker Container On Mac The -privileged flag gives all capabilities to the container, and it alsolifts all the limitations enforced by the device cgroup controller. In otherwords, the container can then do almost everything that the host can do.
  3. In this video we'll see how docker can be used to run Ubuntu shell on a Mac and roam around inside an ubuntu distribution.Learn Full Stack Development by bui.

Requirements

  • Ubuntu server 14.04 with docker installed on your system
  • A non-root user account with sudo privilege set up on your server

Docker Default Network

Docker creates a pair of virtual Ethernet interfaces on each container, randomly assigning them an IP address and a subnet from a private address range not already used by the host system. When you install docker, it creates three networks automatically.

You can list these networks by simply running the following command:

sudo docker network ls

You should see the following output:

The bridge network represents the docker0 network present in all Docker installations.The none network adds a container to a container-specific network stack. The host network adds a container on the hosts network stack.

Find the Docker Interface

By default docker creates a bridge interface - docker0 on the host system when the docker process starts up. Docker assigns the IP address 172.17.0.2/16 to the bridge interface docker0 which acts as a subnet for all the IP addresses of the running containers.

You can easily find out the docker bridge interface and IP address by running the following command:

sudo ip a

You should see the output like this:

Basic Container Networking

When docker starts a container, it creates a virtual interface on host system with unique name like vethef766ac, and assign IP address within the same subnet.

This new interface will be connected to the eth0 interface on the container and allow networking between containers by adding iptable rules. A NAT rule is used to forward traffic to external hosts and the host machine must be set up to forward IP packets.

You can see the iptable NAT rules by running the following command on host system:

sudo iptables -t nat -L

The output looks something like this:

You should see the POSTROUTING chain in the above output. It has a rule that will masquerade all traffic originating from 172.17.0.0/16 (docker bridge network). This masquerade rule will allow containers to reach the outside world.

Creating Your Own Network Bridge

If you want to assign a custom static IP address to the containers, you will need to create new bridge interface br0 in the host system.

To do this, Run following commands on the host machine running docker.

First stop docker service:

sudo service docker stop

Add br0 interface:

sudo ip link add br0 type bridge

Assign the network range as you wish and up br0 interface:

sudo ip addr add 192.168.1.0/24 dev br0sudo ip link set br0 upRun Ubuntu In Docker Container

After creating the docker bridge, you will need to add the following line in /etc/default/docker file:

sudo nano /etc/default/docker

Add the following line at the end of file:

Save and close the file and start docker service:

sudo service docker start

You should see the new bridge interface br0 by running the following command on host machine:

ifconfig

In the above output, the bridged interface will range the docker containers with the IP address followed by bridge IP subnet.

Retrieve a Docker container's IP address

You can use the docker inspect command with 'Container ID' to find the IP address of the running container.

To do this, run following command to find detailed information about your docker container including its internal IP address:

sudo docker inspect 'container ID'

You should see the following output:

If you want to get the container's IP address value only, run:

sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' 'container ID'

You should see the following output:

Depending on the operating system running within your docker container you can also retrieve it's IP address using ifconfig command:

Run ubuntu in docker container on windowssudo docker exec -it 'container ID' /sbin/ifconfig eth0

The output looks something like this:

Docker Single Host Networking

You can create a new network with docker network create command. In this example, we'll create a network called net1 and run an ubuntu container inside it:

sudo docker network create net1

You can list the network by running the following command:

sudo docker network ls

You should see the following output:

Now, you can easily run ubuntu container inside net1 network:

sudo docker run -itd --net=net1 ubuntu

You can disconnect and delete docker network any time by running the following command:

sudo docker network disconnect net1sudo docker network rm net1

Docker Multi Host Networking

In this example, we will create three docker hosts on virtual box using a docker machine. One host runs consul and other two hosts share the network information using the consul service discovery container on the first host.

Before creating a docker machine, you will need to download the docker-machine binary.

To do so, run the following command:

sudo curl -L https://github.com/docker/machine/releases/download/v0.7.0/docker-machine-uname -s-uname -m> /usr/local/bin/docker-machinesudo chmod +x /usr/local/bin/docker-machine

Now, create a docker machine named host1:

sudo docker-machine create -d virtualbox host1

You should see the following output:

You can launch a consul container on the host1 host using the following docker run command.

sudo docker $(docker-machine config host1) run -d -p '8500:8500' -h 'consul' progrium/consul -server -bootstrap

You should see the following output:

You can verify the status of running container using the following command:

sudo docker $(docker-machine config host1) ps

You should see the running container in following output:

Now, launch the second docker machine host with parameters to register it with consul running on the host.

To do so, run the following command:

sudo docker-machine create -d virtualbox --engine-opt='cluster-store=consul://$(docker-machine ip host1):8500' --engine-opt='cluster-advertise=eth1:0' host2

You should see the following output:

Next, launch the third docker machine by running the following command:

sudo docker-machine create -d virtualbox --engine-opt='cluster-store=consul://$(docker-machine ip host1):8500' --engine-opt='cluster-advertise=eth1:0' host3

Now the two hosts have the default networks which can be used only for single host communication.

To have a multi-host network you need to create an overlay network on host2. You can do this by running the following command:

sudo docker $(docker-machine config host2) network create -d overlay myapp

Run Ubuntu In Docker Container On Windows

Now, if you check the networks on host3, you will be able to see the overlay network we created on host2. Because our two hosts are registered with consul and the network information is shared among all the hosts which are registered with it.

To check the network on host2 and host3, run the following command:

sudo docker $(docker-machine config host2) network ls

The output looks like the following:

sudo docker $(docker-machine config host3) network ls

Run Ubuntu In Docker Container List

The output looks like the following:

Ubuntu Start Docker Container Automatically

Now, if you launch containers in a different host, you will be able to connect them using the container name. Let test it by launching a Nginx container on host2 and test the connection by downloading the default Nginx page from host3 using a busybox container.

You can lunch a Nginx container on host2 by specifying the network myapp we have created.

sudo docker $(docker-machine config host2) run -itd --name=webfront --net=myapp nginx

Run Ubuntu In Docker Container Code

You should see the following output:

Run Ubuntu In Docker Container Number

Verify the running container using the following command:

sudo docker $(docker-machine config host2) ps

The output looks like the following:

Now, launch a busybox container on host3 with parameters to download the homepage of nginx running on host2.

sudo docker $(docker-machine config host3) run -it --rm --net=myapp busybox wget -qO- http://webfront

You should see the following output:

Congratulations! The above command returns an HTML output which means the containers are able to connect to other hosts using the overlay network you have created.

Need Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on.