Ubuntu16.04 64bits安装docker

本文详细介绍Docker在64位Ubuntu系统上的安装步骤、配置方法及常见问题解决技巧,包括设置Docker组、调整内存配置、配置UFW防火墙转发及DNS服务器等。

首先docker需要64bits的机器,32bits的无法安装。

安装最简单的方法是:sudo apt-get update   sudo apt-get install docker
但这样安装,以后升级可能会升级不了,因为ubuntu的下没有docker相关的源。

更新安装包信息, 确保 APT 使用 https 协议, 同时CA 证书已经被安装.

 $ sudo apt-get update
 $ sudo apt-get install apt-transport-https ca-certificates

添加新的GPGkey

$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

找到合适你的Ubuntu操作系统的键,这个键决定APT将搜索哪个包

用编辑器打开 /etc/apt/sources.list.d/docker.list添加下面这句,如果不存在docker.list新建一个再添加。

deb https://apt.dockerproject.org/repo ubuntu-xenial main

注意:docker没有为所有的架构提供包,Binary artifacts are built nightly,你可以从https://master.dockerproject.org. 处下载下来。在一个多架构的系统上安装docker,为键添加一个[arch=]条款。更多细节参考Debian Multiarch维基百科。

更新APT 软件包索引

$ sudo apt-get update

清除旧的repo if it exists

$ sudo apt-get purge lxc-docker

至此,你可以 使用命令apt-get upgrade, APT 从新代码库拉取代码.

这里记得upgrade一下,否则安装时会遇到无法安装,我在第一次安装时由于没有upgrade,提示安装相关包失败.

某些Ubuntu版本需要的前置操作

Ubuntu Xenial 16.04[LTS]  Ubuntu Trusty 14.04[LTS]
   这两个版本记得安装linux-iamge-extra-*的kernel包。这个包允许你使用aufs存储驱动。

$ sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual

以上前期准备做完后,就可以安装docker

1) 更新APT包索引:sudo apt-get update
2) 安装docker:sudo apt-get install docker-engine
3) 开启docker后天进程:sudo service docker start
4) 校验docker是否安装成功:sudo docker run hello-world
这个命令会下载一个测试镜像,并且运行在一个容器中。当容器运行时,他会打印一些信息,并且退出。

下面是安装成功后,启动docker服务,下载hello-world测试镜像的log记录:

jack@jack-VirtualBox:~$ sudo service docker start 
[sudo] password for jack: 
jack@jack-VirtualBox:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9a0669468bf7: Pull complete 
Digest: sha256:cf2f6d004a59f7c18ec89df311cf0f6a1c714ec924eebcbfdd759a669b90e711
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

可选配置

创建一个docker组

docker后台进程是绑定的Unix的socket而不是TCP端口。默认情况下,Unix的socket属于用户root,其它用户要使用要通过sudo命令。由于这个原因,docker daemon通常使用root用户运行。
为了避免使用sudo当你使用docker命令的时候,创建一个Unix组名为docker并且添加用户。当docker daemon启动,它会分配Unix socket读写权限给所属的docker组。
注意:docker组不等价于用户root,如果想要知道的更多关于安全影响,查看docker daemon attack surface

  sudo groupadd docker //在ubuntu16上不需要另外创建docker助,默认在安装docker是就已经建好了,可以用cat /etc/group查看下有无docker组

  sudo usermod -aG docker $USER

退出再重进,确保该用户有正确的权限。
This ensures your user is running with the correct permissions.

确认不使用sudo可以运行docker.

$ docker run hello-world

如果提示失败信息与下面相似:

Cannot connect to the Docker daemon. Is ‘docker daemon’ running on this host?
检查 DOCKER_HOST 环境变量 没有设置shell. 如果这样,重置他.

调整内存和交换区计算
When users run Docker, they may see these messages when working with an image:

WARNING: Your kernel does not support cgroup swap limit. WARNING: Your
kernel does not support swap limit capabilities. Limitation discarded.
To prevent these messages, enable memory and swap accounting on your system. Enabling memory and swap accounting does induce both a memory overhead and a performance degradation even when Docker is not in use. The memory overhead is about 1% of the total available memory. The performance degradation is roughly 10%.

To enable memory and swap on system using GNU GRUB (GNU GRand Unified Bootloader), do the following:

Log into Ubuntu as a user with sudo privileges.

Edit the /etc/default/grub file.

Set the GRUB_CMDLINE_LINUX value as follows:

GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
Save and close the file.

Update GRUB.

$ sudo update-grub
Reboot your system.

当用户运行docker时,他们可能在使用一个镜像时看见下面的信息:

  WARNING: Your kernel does not support cgroup swap limit. WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.

  为了阻止这些信息,在你的系统中启用内存和交换区计算。这个操作会导致即便docker没有使用也有内存开销以及性能下降。内存开销大概是总内存的1%。性能降低了大约10%。

  修改/etc/default/grub文件。vi或者vim命令都行,设置GRUB_CMDLINE_LINUX的值,如下:GRUB_CMDLINE_LINUX=”cgroup_enable=memory swapaccount=1”。保存文件并关闭。sudo update-grub更新启动项。reboot重启你的系统。

启动UFW转发
If you use UFW (Uncomplicated Firewall) on the same host as you run Docker, you’ll need to do additional configuration. Docker uses a bridge to manage container networking. By default, UFW drops all forwarding traffic. As a result, for Docker to run when UFW is enabled, you must set UFW’s forwarding policy appropriately.

Also, UFW’s default set of rules denies all incoming traffic. If you want to reach your containers from another host allow incoming connections on the Docker port. The Docker port defaults to 2376 if TLS is enabled or 2375 when it is not. If TLS is not enabled, communication is unencrypted. By default, Docker runs without TLS enabled.

To configure UFW and allow incoming connections on the Docker port:

Log into Ubuntu as a user with sudo privileges.

Verify that UFW is installed and enabled.

$ sudo ufw status
Open the /etc/default/ufw file for editing.

$ sudo nano /etc/default/ufw
Set the DEFAULT_FORWARD_POLICY policy to:

DEFAULT_FORWARD_POLICY="ACCEPT"
Save and close the file.

Reload UFW to use the new setting.

$ sudo ufw reload
Allow incoming connections on the Docker port.

$ sudo ufw allow 2375/tcp

  当你运行docker时,在同一台主机上使用UFW(Uncomplicated Firewall) ,你需要额外的配置。docker使用桥接方式来管理容器的网络。默认情况下,UFW废弃所有的转发流量。因此,docker运行时UFW可以使用,你必须设置合适UFW的转发规则。

  UFW默认配置规则拒绝了所有传入流量。如果你想要从另一个主机到达你的容器需要允许连接docker的端口。docker的默认端口是2376如果TLS启用,如果没有启动则是2375,会话是不加密的。默认情况,docker运行在没有TLS启动的情况下。

  为了配置UFW并且允许进入的连接docker端口:

  检查UFW是否安装并启用:sudo ufw status

  打开/etc/default/ufw文件并编辑:sudo nano /etc/default/ufw

  设置DEFAULT_FORWARD_POLICY:DEFAULT_FORWARD_POLICY=”ACCEPT”

  保存退出并重启使用新的设置:sudo ufw reload

  允许所有的连接到docker端口:sudo ufw allow 2375/tcp

为使用docker配置DNS服务器
Systems that run Ubuntu or an Ubuntu derivative on the desktop typically use127.0.0.1 as the default nameserver in /etc/resolv.conf file. The NetworkManager also sets up dnsmasq to use the real DNS servers of the connection and sets up nameserver 127.0.0.1 in /etc/resolv.conf.

When starting containers on desktop machines with these configurations, Docker users see this warning:

WARNING: Local (127.0.0.1) DNS resolver found in resolv.conf and containers
can't use it. Using default external servers : [8.8.8.8 8.8.4.4]
The warning occurs because Docker containers can’t use the local DNS nameserver. Instead, Docker defaults to using an external nameserver.

To avoid this warning, you can specify a DNS server for use by Docker containers. Or, you can disable dnsmasq in NetworkManager. Though, disablingdnsmasq might make DNS resolution slower on some networks.

The instructions below describe how to configure the Docker daemon running on Ubuntu 14.10 or below. Ubuntu 15.04 and above use systemd as the boot and service manager. Refer to control and configure Docker with systemd to configure a daemon controlled by systemd.

To specify a DNS server for use by Docker:

Log into Ubuntu as a user with sudo privileges.

Open the /etc/default/docker file for editing.

$ sudo nano /etc/default/docker
Add a setting for Docker.

DOCKER_OPTS="--dns 8.8.8.8"
Replace 8.8.8.8 with a local DNS server such as 192.168.1.1. You can also specify multiple DNS servers. Separated them with spaces, for example:

--dns 8.8.8.8 --dns 192.168.1.1
Warning: If you’re doing this on a laptop which connects to various networks, make sure to choose a public DNS server.

Save and close the file.

Restart the Docker daemon.

$ sudo service docker restart


Or, as an alternative to the previous procedure, disable dnsmasq in NetworkManager (this might slow your network).

Open the /etc/NetworkManager/NetworkManager.conf file for editing.

$ sudo nano /etc/NetworkManager/NetworkManager.conf
Comment out the dns=dnsmasq line:

dns=dnsmasq
保存并关闭.

重启NetworkManager 和 Docker.

$ sudo restart network-manager
$ sudo restart docker

 系统运行桌面的Ubuntu或者Ubuntu衍生产品通常使用127.0.0.1作为默认的nameserver文件/etc/resolv.conf文件中。NetworkManager也通常设置dnsmasq nameserver 127.0.0.1在/etc/resolv.conf。

  当在桌面机器运行容器,使用这些配置时,docker的使用者会看见这些警告:

  WARNING: Local (127.0.0.1) DNS resolver found in resolv.conf and containers can’t use it. Using default external servers : [8.8.8.8 8.8.4.4]

  这个警告发生是因为docker容器不能使用本地DNS命名服务器。此外docker默认使用一个额外的nameserver。

  为了避免这个警告,你可以在使用docker容器的时候指定一个DNS服务器。或者你可以禁用dnsmasq在NetworkManager中。但是,禁用会导致DNS协议在某些网络中变慢。

  下面的说明描述了如何在Ubuntu14.0或以下版本配置docker守护进程。Ubuntu15.04及之上的使用systemd用于启动项和服务管理。指导通过使用systemd来配置和控制一个守护进程。

  设置指定的DNS服务:

  打开/etc/default/docker文件并编辑:sudo nano /etc/default/docker,添加配置项:DOCKER_OPTS=”–dns 8.8.8.8”。将8.8.8.8用一个本地的DNS服务例如192.168.1.1替换。你也可以配置多个DNS服务器。用空格隔开它们,如:–dns 8.8.8.8 –dns 192.168.1.1。警告:当你在笔记本连接了不同网络的情况时做这些操作,确保选择一个公用的DNS服务器。保存文件并退出,重启docker守护进程:sudo service docker restart。

  或者另一个选择,禁用dnsmasq在网络管理器中,这可能导致你的网速变慢:

  打开/etc/NetworkManager/NetworkManager.conf文件,编辑它:sudo nano /etc/NetworkManager/NetworkManager.conf。找到行dns=dnsmasq,注释掉。保存关闭文件,重启网络管理器和docker.sudo restart network-manager sudo restart docker。
  

配置docker引导启动
Ubuntu uses systemd as its boot and service manager 15.04 onwards andupstart for versions 14.10 and below.

For 15.04 and up, to configure the docker daemon to start on boot, run

$ sudo systemctl enable docker
For 14.10 and below the above installation method automatically configuresupstart to start the docker daemon on boot

Ubuntu15.04之后使用systemd作为引导启动和服务管理,14.10及以下版本是upstart。15.04以上,需要配置docker守护进程boot启动,运行命令:sudo systemctl enable docker

  14.10及以下版本安装方法会自动配置upstart来启动docke daemon在boot。

升级卸载docker

升级:sudo apt-get upgrade docker-engine

  卸载:sudo apt-get purge docker-engine

  卸载及依赖:sudo apt-get autoremove –purge docker-engine

上述命令不会卸载images,containers,volumes或者用户自己创建的配置文件。你如果想删除这些东西,执行下面的命令:

  rm -rf /var/lib/docker

<think>首先,用户在Ubuntu 16.04系统中询问离线安装Dockerdocker-compose的方法。用户提供了几个引用,但我需要专注于他们的查询:离线安装意味着没有互联网连接。 从引用中: - 引用[1]:讨论安装Dockerdocker-compose,但似乎是在线的,因为它提到了官方文档。 - 引用[2]和[3]:显示在Ubuntu 16.04安装docker-compose的命令,但它们使用curl下载文件,这是在线的。 用户明确表示“离线安装”,所以我应该忽略在线方法并专注于离线方法。 关键点: - **离线安装Docker**:这意味着在没有Internet连接的情况下安装Docker。这通常涉及下载必要的.deb文件并手动安装。 - **离线安装docker-compose**:类似地,下载二进制文件并手动安装。 系统级指令: - 所有行内数学表达式使用$...$。 - 独立公式使用$$...$$并单独成段。 - 使用中文回答。 - 生成相关问题。 - 在引用的段落末尾添加引用标识,如[^1]。 在这个上下文中,可能没有数学表达式,但我需要确保格式正确。用户的问题是关于安装软件的,所以可能没有数学内容。 回答结构: 1. 既然用户要求离线方法,我应该提供一个逐步指南。 2. 确保回答清晰、结构良好。 3. 最后,生成相关问题。 4. 使用中文。 来自引用的一些相关细节: - 引用[1]提到升级和安装后步骤,但重点是离线。 - 引用[2]和[3]显示docker-compose的命令,但需要离线版本。 离线安装的一般方法: 1. **为Docker**: - 从另一台有互联网连接的机器上下载.deb软件包。 - 将软件包传输到Ubuntu 16.04机器。 - 使用dpkg安装。 2. **为docker-compose**: - 下载二进制文件(例如,从GitHub发布页面)。 - 复制到系统路径。 - 设置执行权限。 具体步骤: - Docker:对于UbuntuDocker提供deb软件包。对于离线安装,可以从 https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/ 下载deb文件(确认版本)。 - docker-compose:从 https://github.com/docker/compose/releases 下载二进制文件。 步骤概述: 1. 在有互联网连接的机器上: - 下载Docker .deb文件。 - 下载docker-compose二进制文件。 2. 传输到离线机器。 3. 在离线机器上: - 安装Docker:`sudo dpkg -i /path/to/docker.deb` - 安装docker-compose: - 复制二进制文件到 /usr/local/bin/docker-compose - `sudo chmod +x /usr/local/bin/docker-compose` 确保依赖关系: - Docker可能有依赖关系,所以离线安装时,需要预先下载所有依赖的deb文件或确保它们可用。 对于Ubuntu 16.04Docker文档推荐使用存储库,但离线时,手动下载deb文件是最好的。 类似地,对于docker-compose,它是一个单一二进制文件。 实现回答: - 用中文。 - 结构清晰:介绍、步骤、验证。 - 最后,生成相关问题。 - 如果需要,添加引用。 引用:用户提供了引用[1]、[2]、[3],但我应该参考相关内容。既然离线安装,引用可能不直接相关,但可以提到一般方法。 可能的引用添加:在解释方法时,参考在线文档以获得更多细节。 结束语:自然地添加相关问题。 相关问题示例: 1. 离线安装Docker的常见问题? 2. 如何在没有sudo的情况下使用Docker? 3. 验证Docker安装的方法? 4. Docker的其他用途? 确保回答真实可靠。 </think>### Ubuntu 16.04离线安装Dockerdocker-compose的方法 在Ubuntu 16.04系统上进行离线安装Dockerdocker-compose,意味着在没有互联网连接的环境下操作。这需要提前下载必要的软件包(如.deb文件)或二进制文件,然后通过本地方式安装。下面我将提供清晰的分步指南。整个过程分为三个阶段:准备阶段(在有互联网连接的设备上操作)、传输文件和安装阶段(在Ubuntu 16.04离线机器上操作)。安装完成后,建议验证安装以确保成功。 #### 1. **准备阶段:在有互联网连接的设备上下载必要文件** 在这一阶段,您需要从另一台联网设备(如笔记本电脑或其他服务器)下载Dockerdocker-compose的安装文件。确保选择兼容Ubuntu 16.04(代号Xenial)的版本。以下是关键步骤: - **下载Docker CE .deb文件**: - Ubuntu 16.04官方支持Docker CE(社区版)。访问Docker官方下载页面(例如:[https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/](https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/)),选择最新的稳定版.deb文件。例如: - 下载文件:`docker-ce_<version>~<build>_amd64.deb` - 同时下载依赖包:Docker可能依赖`containerd.io`和`docker-ce-cli`等包,请一并下载所有相关.deb文件以避免依赖错误[^1]。 - 示例命令(使用wget): ```bash wget https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/docker-ce_24.0.7~3-0~ubuntu-xenial_amd64.deb wget https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/containerd.io_<version>_amd64.deb # 下载其他必要依赖包 ``` - **下载docker-compose二进制文件**: - docker-compose是一个独立的二进制文件。从GitHub发布页面下载对应版本(确保选择Linux x86_64架构)。访问:[https://github.com/docker/compose/releases](https://github.com/docker/compose/releases)。 - 推荐下载版本:v2.23.3(稳定且兼容Ubuntu 16.04)。 - 示例命令: ```bash wget https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-linux-x86_64 ``` - **整理文件**: - 将所有下载文件(.deb和二进制文件)放入一个目录,例如`~/docker-offline`。 #### 2. **传输文件到Ubuntu 16.04离线机器** 将下载的文件从联网设备传输到Ubuntu 16.04目标机器: - 使用U盘、内部网络共享或SCP命令复制文件。例如: ```bash scp -r ~/docker-offline user@ubuntu-machine:/home/user/docker-offline ``` - 确保目标机器上有足够的权限访问这些文件。 #### 3. **在Ubuntu 16.04离线机器上安装** 登录Ubuntu 16.04机器,依次安装Dockerdocker-compose: - **步骤1:安装Docker依赖和主包** - 更新本地包索引(虽然离线,但缓存可能有帮助): ```bash sudo apt-get update ``` - 安装所有下载的.deb文件: ```bash cd /home/user/docker-offline sudo dpkg -i *.deb # 安装所有.deb文件 ``` - 解决依赖问题(如果提示缺少依赖): ```bash sudo apt-get -f install # 尝试修复(需要预设依赖包) ``` - 提示:如果依赖包未提前下载,这一步可能失败。确保准备阶段包含所有必要文件[^2]。 - **步骤2:安装docker-compose** - 将二进制文件复制到系统路径: ```bash sudo cp docker-compose-linux-x86_64 /usr/local/bin/docker-compose ``` - 设置可执行权限: ```bash sudo chmod +x /usr/local/bin/docker-compose ``` - **步骤3:配置和启动Docker服务** - 启用Docker服务: ```bash sudo systemctl start docker sudo systemctl enable docker # 设置开机自启 ``` - (可选)添加用户到docker组,避免sudo: ```bash sudo usermod -aG docker $USER newgrp docker # 刷新组权限 ``` 参考:[如何在无sudo权限下使用Docker](http://www.tinylab.org/use-docker-without-sudo/)[^3]。 - **步骤4:验证安装** - 检查Docker版本: ```bash docker --version # 应输出类似 Docker version 24.0.7 ``` - 检查docker-compose版本: ```bash docker-compose --version # 应输出 Docker Compose version v2.23.3 ``` - 运行测试容器: ```bash sudo docker run hello-world # 确保Docker正常工作 ``` #### 注意事项 - **依赖管理**:Ubuntu 16.04的依赖包可能包括`libc6`、`libltdl7`等。如果安装失败,建议提前下载这些包(通过`apt-get download <package-name>`在联网设备上)。 - **版本兼容性**:Ubuntu 16.04较旧,确保下载的Docker版本支持Xenial(例如,Docker CE 17.x或更高)。 - **离线资源**:如果下载困难,可从Docker镜像站点(如阿里云镜像)获取包,但需确保安全性[^1]。 - 安装完成后,参考Docker文档管理服务和权限:[https://docs.docker.com/engine/installation/linux/linux-postinstall/](https://docs.docker.com/engine/installation/linux/linux-postinstall/)[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JXES智能生态系统

如文章对你有用,请作者喝个咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值