Current location - Health Preservation Learning Network - Slimming men and women - How to manage Linux containers with Docker on Ubuntu
How to manage Linux containers with Docker on Ubuntu
Although standard hardware virtualization technologies (such as KVM, Xen or Hyper-V) are good at running completely isolated instances of multiple operating systems on physical hosts, this virtualization technology has various expenses in terms of performance, resources and resource allocation time. Standard machine virtualization may actually be unnecessary, depending on your actual usage.

Another lightweight virtualization method is the so-called Linux Container (LXC), which provides virtualization at the operating system level. LXC allows users to run multiple instances of the standard Linux operating system in a lightweight container sandbox because there is no overhead of running virtual machines. Containers come in handy if you build a replicable development/test environment or deploy applications in a security sandbox.

Docker is such an open source tool, which was developed to facilitate the deployment of Linux containers. Docker is rapidly becoming the de facto standard in container technology and adopted by mainstream Linux distributions such as Ubuntu and Red Hat.

In this tutorial, I will demonstrate how to use Docker to manage Linux containers on Ubuntu 14.04. Please note: For earlier versions of Ubuntu, the operation steps may be slightly different.

Currently, the Docker package available on Ubuntu only supports 64-bit systems. To run it on a 32-bit machine, you need to build a 32-bit version of Docker with source code (see here for details).

Install Docker

With the help of apt-get command, it is easy to install Docker.

$ sudo apt-get install docker.io

To allow non-root users to run docker, please add yourself to the Docker group. The following command will allow the current user to run Docker without root privileges.

$ sudo usermod -a -G docker $USER

Log out and then log back in to activate the change of group membership.

Next, edit the Docker configuration file to update the location of the Docker binary code.

$ sudo vi /etc/default/docker.io

DOCKER="/usr/bin/docker.io "

Restart the Docker service.

$ sudo service docker.io restarted

Manage terminal containers

If you want to start a new Docker container of Ubuntu operating system, you need to obtain the Ubuntu Docker image file first. The following command will download the Docker image file over the network.

$ docker pull ubuntu

You can open Ubuntu Docker in interactive mode, as shown below. The last parameter "/bin/bash" is a command that will be executed in the container once it is started. The following is a simple bash shell command.

$ docker run -i -t ubuntu /bin/bash

The above command will immediately start an Ubuntu container (this is the charm of the container! ) and provide you with a shell prompt inside the container. At this point, you should be able to access the standard Ubuntu operating system in a sandbox environment.

To exit the Docker container, type "exit" at the prompt inside the container.

You can start different types of containers. For example, to start the Fedora container, execute the following command:

$ docker . io run-I-t fedora/bin/bash

If there is no Fedora Docker image file locally, this command will automatically download the image file first, and then start Docker.

You can also do this if you want to start a container that uses a distribution version. For example, to start Ubuntu 13.04 Docker, execute the following command:

$ docker . io run-I-t Ubuntu: 13.04/bin/bash

Container network

Docker uses Linux bridges to interconnect containers and connect them to external networks. After installing Docker, you should see the default automatic setting of docker0 Linux bridge. Every container you create will be connected to the docker0 bridge interface.

Custom Linux bridge

If you like, you can also use a custom Linux bridge to interconnect containers. To do this, you can build a custom bridge and configure it as follows. You can assign a separate subnet to the bridge and assign an IP address to Docker from the subnet. I will use 10.0.0.0/24 as the Docker subnet.

$ sudo apt-get installs the bridging utility

$ sudo brctl addbr br0

$ sudo ifconfig br010.0.0.1netmask 255.255.255.0

If you want DOCKER to use a custom bridge, add "-b=br0" to the DOCKER_OPTS variable in /etc/default/Docker.io, and then restart the Docker service.

$ sudo service docker.io restarted

At this point, any new container will be connected to br0, and its IP address will be automatically assigned from 10.0.0/24.

Other customization

There are several other ways to customize the default network settings of Docker, mainly by changing the DOCKER_OPTS variable in/etc//etc /etc/default/docker.io

"-DNS 8.8.8-DNS 8.8.4.4": Specify the DNS server used by the container.

"-icc=false": isolate containers from each other.

Settle a dispute

1. When you run the docker.io command, you will encounter the following error.

Dialunix/var/run/docker.sock: There is no such file or directory.

This error may occur because the Docker daemon is not running. Check the status of the Docker daemon and make sure to start it first.

$ sudo service docker.io status

$ sudo service docker.io started