Ubuntu下Docker安装,设置镜像加速,基础用法

官网教程

在Ubuntu上安装Docker Engine

操作系统要求

  • Ubuntu Focal 20.04 (LTS)
  • Ubuntu Eoan 19.10
  • Ubuntu Bionic 18.04 (LTS)
  • Ubuntu Xenial 16.04 (LTS)

如果已安装老版本 需要先卸载

sudo apt-get remove docker docker-engine docker.io containerd runc 

更新apt软件包索引并安装软件包以允许apt通过HTTPS使用存储库:

$ sudo apt-get update
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

添加Docker的官方GPG密钥:

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

设置存储库

$ sudo add-apt-repository \
  "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) \
  stable"

到这一步,如果系统内置python版本有过改动,可能会报错

	File "/usr/bin/add-apt-repository", line 11, in <module>
    from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
  	File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 28, in <module>
    import apt_pkg
	ModuleNotFoundError: No module named 'apt_pkg'

这是由于Python版本问题引起的, 运行cd /usr/bin检查一下目录里的python版本(3.x)大概都有哪些,我的服务器里是python3.6和python3.7,
然后运行sudo vim /usr/bin/apt-add-repository查看apt-add-repository配置文件其中一部分如下,

#!/usr/bin/python3

from __future__ import print_function

import io
import os
import sys
import gettext
import locale

from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
from softwareproperties.shortcuts import ShortcutException
import aptsources
from aptsources.sourceslist import SourceEntry
from optparse import OptionParser
from gettext import gettext as _

修改第一行 #!/usr/bin/python3为当前服务器版本,我用的是3.6,修改后为#!/usr/bin/python3.6
然后继续接着报错那一步继续安装docker

  $ sudo add-apt-repository \
  "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) \
  stable"

更新apt程序包索引,并安装最新版本的Docker Engine和容器

 $ sudo apt-get update
 $ sudo apt-get install docker-ce docker-ce-cli containerd.io

测试是否正确安装

 $ sudo docker run hello-world

输出以下信息表示安装成功

0e03bdcc26d7: Pull complete 
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
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.
    (amd64)
 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://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Docker 镜像加速

  • 网易:https://hub-mirror.c.163.com/
  • 七牛云加速器:https://reg-mirror.qiniu.com

我的服务器是Ubuntu18.04 64位,新建或修改

vim /etc/docker/daemon.json

内容为

{"registry-mirrors":["https://reg-mirror.qiniu.com/"]}
# 或者
{"registry-mirrors":["https://hub-mirror.c.163.com/"]}

常用命令

#查看到 Docker 客户端的所有命令选项
$ docker 
#或者
$ docker command --help 
# 查看运行中的容器
$ docker ps
# 查看全部容器
$ docker ps -a
# 停止容器
$ docker stop 容器ID
# 删除容器
$ docker rm 容器ID
# 启动/重启一个已经停止的容器
$ docker start 容器ID 
$ docker restart 容器ID
# 第一次启动docker 
#-d 代表后台运行 -p 指的是映射容器端口与服务器端口 --name 指对镜像所自定义的名称 ,
#-v 指的是自定义配置目录,最后的参数是本地的镜像
# 注意-p有大小写之分 如果用-P话则将容器内部使用的网络端口随机映射到我们使用的主机上。
$ docker run -d -p 49002:8080 --name 自定义容器名 -v /var/dir_name:/var/dir_name 镜像名
# 清理掉所有处于终止状态的容器。
$ docker container prune
# 进入容器
docker exec -it web_jenkins bash

学习资料

Docker从入门到实践
docker文件存放路径, 获取容器启动命令

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值