Open the Docker engine in Linux
We can install Docker in Linux and run the Hello World application. With these actions, we will learn more about the basic features of Docker. We will learn how to:
- Installing the Docker engine
- Run a software image in a container
- Browse for a mirror in the Docker hub
- Create your image and run it in a container
- Create a Docker hub account and a mirrored library
- Create your own image
- Put your image in the Docker hub for others to use
The start tutorial is designed to reduce the likelihood of a user encountering a problem, and in order to be able to test successfully, be sure to follow the steps described below. The process consumes approximately 45 minutes.
Make sure you understand ...
The start tutorial uses the command line to do the appropriate work. You don't have to be particularly familiar with the command line, but you should know how to open a shell or terminal and run basic commands in that environment. It helps us understand how to navigate a directory tree, manipulate files, enumerate running processes, and so on.
Install Docker (in Ubuntu)
Ubuntu
Docker supports these Ubuntu operating systems as follows:
- Ubuntu xenial 16.04 (LTS)
- Ubuntu Wily 15.10
- Ubuntu trusty 14.04 (LTS)
- Ubuntu precise 12.04 (LTS)
This page guides you through the Docker Management release package and installation mechanism to install Docker. Using these packages will ensure that you get the latest Docker release. If you want to install using the Ubuntu Management Pack, you can check out an Ubuntu profile.
注意:Ubuntu 14.10和15.04存在Docker的APT库,但是并不官方支持。
Requirements
Regardless of your Ubuntu version, Docker requires a 64-bit system installation. In addition, your kernel minimum version requirement is 3.10. The recent minor version of 3.10 or the latest maintenance version is acceptable.
A kernel with a version less than 3.10 lacks some features that run Docker containers. These older versions of the kernel are known to have bugs that can cause data loss and, under certain conditions, cause frequent panic attacks.
You can open a terminal and use it uname -r
to check your current kernel version.
bobo@bobo:~$ uname -r3.19.0-15-generic
注意:如果你之前使用`APT`安装了Docker,一定要确定更新你的`APT`源码到最新的Docker库。
Update your APT source
The Docker APT
Library contains Docker 1.7.1 and later versions. In order to set up APT
the use of packages from the latest library:
- Use
sudo
or root
permission to log into your machine as a user
- Open a terminal window
- Update package information, determine that apt
https
works as a method, and that the CA certificate has been installed.
$ sudo apt-get update $ sudo apt-get install apt-transport-https ca-certificates
- Add a new
GPG
key
$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:8058118E89F3A912897C070ADBF76221572C52609D
- Open the file using your favorite editor
/etc/apt/sources.list.d/docker.list
. If the file does not exist, create it.
- Removes all existing entries.
Add an entry for your Ubuntu operating system.
The possible entries are:
-
In ubuntu precise 12.04 (LTS)
deb https://apt< Span class= "Hljs-preprocessor" >.dockerproject .org /repo Ubuntu-precise main
-
In ubuntu trusty 14.04 (LTS)
deb https://apt.dockerproject .org /repo ubuntu-trusty main
-
In Ubuntu wily 15.10
deb https://apt .org /repo ubuntu-wily main
-
In Ubuntu xenial 16.04 (LTS)
deb https://apt.dockerproject .org /repo ubuntu-xenial Main
Note: Docker does not provide packages for all schemas. You can find the built binary in https://master.dockerproject.org. To install Docker on a multi-schema system, add an element to the entry [arch=...]
. Details refer to Debian Multiarch wiki
Save and close the /etc/apt/sources.list.d/docker.list
file.
- Update the
APT
package index.
sudo apt-get update
- If the old repo exists, clear the
$ sudo apt-get purge lxc-docker
- Verify that
APT
it was pulled from the correct library.
$ apt-cache policy docker-engine
From now on, when you run it again apt-get upgrade
, it APT
will be pulled from the latest library.
Conditions for the Ubuntu version
- Ubuntu xenial 16.04 (LTS)
- Ubuntu Wily 15.10
- Ubuntu trusty 14.04 (LTS)
For Ubuntu trusty,wily and xenial, we recommend that you install linux-image-extra
kernel packs. The linux-image-extra
package allows you to use the aufs
storage driver.
In order to install the package to your kernel version linux-image-extra
:
- Open a terminal in your Ubuntu system.
- Update your package Manager.
sudo apt-get update
- Install the recommended package
$ sudo apt-get install linux-image-extra--r)
- Continue installing Docker
If you are installing in Ubuntu 14.04 or 12.04, apparmor
it is needed. You can use apt-get install apparmor
it to install it.
Ubuntu precise 12.04 (LTS)
For Ubuntu precise, Docker requires a 3.13 kernel version. If your kernel version is required to be 3.13 Low, you must update it. Check this table to see which packages are needed for your environment.
In order to update your kernel and install additional packages, do the following:
- Open a terminal.
- Update Package Manager
sudo apt-get update
- Required and optional packages are to be installed
$ sudo apt-get install linux-image-generic-lts-trusty
Depending on your environment, you may need to install more, as described in the table above.
- Reboot your host.
- After the system restarts, continue to install Docker.
Installation
Make sure you have installed the necessary conditions for your Ubuntu version. Then, install Docker using the following command:
- Use
sudo
privileges as a user to log in to your Ubuntu.
- Update your
APT
package index.
sudo apt-get update
- Install Docker.
$ sudo apt-get install docker-engine
- Open
docker
Domain
sudo service docker start
- Verify that the
docker
installation is correct
sudo docker run hello-world
The command downloads a test image to the container and runs it. When the container runs successfully, a message is printed and then exited.
[Email protected]:~$ sudo docker run hello-worldunable toFind image' Hello-world:latest 'Locallylatest:pulling fromLibrary/hello-worldGenevaF4658f8b78:pull Complete A3ed95caeb02:pull Complete digest:sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7status:downloaded Newer image forHello-world:latesthello fromDocker.this message shows that your installation appears tobe working correctly. To generate the this message, Docker took theFollowing steps:1.The Docker client contacted theDocker Daemon.2.The Docker daemon pulled the "Hello-world"Image from theDocker Hub.3.The Docker daemon createda NewContainer fromThat image which runs theExecutable that produces theOutput you is currently reading.4.The Docker daemon streamed that output to theDocker client, which sentit toYour terminal. ToTrySomething more ambitious, you can run anUbuntu container with: $ docker Run-itUbuntu Bashshare images, automate workflows, andMore with aFree Docker Hub Account:HTTPS://hub.docker.comfor More examples andIdeas, visit:HTTPS://docs.docker.com/userguide/
Optional configuration
This section contains optional procedures for configuring your Ubuntu to better run Docker.
- Create a Docker group
- Adapting to memory and switching
- Enable UFW to Forward
- Configure a DNS server to use
- Configuring Docker Boot
Create a Docker group
docker
A domain is bound to a socket on UNIX, not a TCP port. By default, the UNIX socket belongs to root
the user, and other users can sudo
access it. For this reason, the docker
domain will always root
run as a user.
To avoid having to docker
use commands sudo
, we need to create a UNIX group called docker
, and add users to the group. When docker
a domain is started, the read and write ownership of UNIX sockets is owned by the docker
group.
警告:`docker`组和`root`用户是平等的。
In order to create docker
a group and add your users:
- Use
sudo
permissions to access Ubuntu.
- Create a
docker
Group
sudo groupadd docker
docker
add your users to a group
sudo-aG docker ubuntu
- Log out and log in
This ensures that your users are running with the correct permissions.
- Do not use
sudo
run docker
to verify your work.
$ docker run hello-world
If the failure message resembles the following:
tothe‘docker daemon‘on this host?
Check DOCKER_HOST
that the environment variable is set and reset if it is set.
Adjust memory and swap partitions
When users run Docker, they may see the following information when they run a mirror:
WARNING: Your kernel does not support cgroup swap limit. WARNING: Yourkernel does not support swap limit capabilities. Limitation discarded.
In order to block this information, you need to enable the memory and swap partitions (memories and Swap accounting) on your system. enabling memory and swap partitions can cause memory overhead and degradation performance, even if Docker is not in use. The memory overhead is approximately 1% of all available memory. The performance is approximately degraded by 10%.
In order to enable memory and swap partitions on systems that use GNU GRUB, follow these steps:
- Use privileges as a user
sudo
to log on to Ubuntu.
- Edit the
/etc/default/grub
file.
- Set the value as follows
GRUB_CMDLINE_LINUX
.
GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
- Save and close a file
- Update Grub
sudo update-grub
- Rebooting the system
Enable UFW to Forward
If you run Docker and use UFW (uncomplicated Firewall) on the same host, you will need to do additional configuration. Docker uses a bridge to manage the container network. By default, UFW discards all forwarded messages. As a result, when UFW is enabled and Docker wants to run, you must set the UFW forwarding policy.
The default setting rule for UFW is to reject all incoming messages. If you want another host to reach your container, you need to allow incoming connections on the Docker port. If LTS is enabled, then the default port for Docker is 2376
, if not, the port is 2375
. If TLS is not enabled, the communication is unencrypted. By default, Docker runs when TLS is not enabled.
In order to configure the UFW and allow incoming connections on the Docker port:
- As a user to
sudo
login.
- Verify that the UFW is installed and enabled.
sudo ufw status
- Open
/etc/default/ufw
and edit.
$ sudo nano /etc/default/ufw
- Set
DEFAULT_FORWARD_POLICY
the policy to:
DEFAULT_FORWARD_POLICY="ACCEPT"
- Save and close a file
- Reload UFW to use the new settings.
sudo ufw reload
- Allow incoming connections on the Docker port.
sudo2375/tcp
Configure a DNS server for Docker to use
Running Ubuntu system on the desktop or Ubuntu derivative system, in the /etc/resolv.conf
file, is typically used 127.0.0.1
as the default nameserver
. The network manager is also set up dnsmasq
to use the real DNS server and is /etc/resolv.conf
set in nameserver 127.0.0.1
.
When you use these configurations to open a container on a desktop machine, the Docker user will see this warning:
WARNING: Local (127.0.0.1) DNS resolver found in resolv.conf and containerscan‘t use it. Using default external servers : [8.8.8.8 8.8.4.4]
This warning occurs because the Docker container cannot use a local DNS domain name server. Docker uses an external domain name server by default.
To avoid this warning, you can specify a DNS server for the Docker container to use. Or, you can disable it in Network manager dnsmasq
. However, disabling dnsmasq
may cause DNS to slow down in some networks.
The following instructions describe how to configure a Docker domain that runs on a Ubuntu14.10 or earlier version. Ubuntu 15.04 and later versions are used systemd
as startup and service Manager. systemd
You can refer to this article for a configuration with the control and configure Docker with SYSTEMD.
In order to specify a DNS server for Docker to use:
- Log into the system as a user identity
sudo
.
- Open
/etc/default/docker
file editing.
- Add a setting.
DOCKER_OPTS="--dns 8.8.8.8"
Use a local DNS server, such as 192.168.1.1
replace 8.8.8.8
. You can also specify multiple DNS servers. Separate them with a space. For example:
--dns8.8.8.8--dns192.168.1.1
警告:如果你正在使用连接多个网络的电脑做这个事情,确定要选择一个公共DNS服务器。
- Save and close a file
- Restart the Docker domain.
sudo restart docker
Or, as an optional option for the previous operation, it is disabled in the network manager dnsmasq
.
- Open the
/etc/NetworkManager/NetworkManager.conf
file.
- Comment out
dns=dnsmasq
this line.
- Save and close the file.
- Restart Network manager and Docker.
sudosudo restart docker
Configuring Docker Boot-up
Ubuntu 15.04 and above systems are used systemd
as its startup and service Manager, Ubuntu 14.10 and its lower version upstart
.
For 15.04
and above versions, to configure docker
domain boot, run:
sudo systemctl enable docker
For 14.10
its lower version, the above installation method is automatically configured to upstart
start automatically when the Docker domain is turned on.
Update Docker
Install the latest version of Docker using apt-get
:
$ sudo apt-get upgrade docker-engine
Unloading
To uninstall a Docker package:
$ sudo apt-get purge docker-engine
Uninstalling the Docker package is no longer necessary to use the following command:
$ sudo apt-get--purge docker-engine
The above command does not remove the mirror, container, volume, or user-created profile. If you want to delete all the mirrors, containers and volumes, run the following command:
-rf /var/lib/docker
You must manually delete the user-created configuration file.
Docker installation, configuration, updates, and uninstallation