在 Ubuntu 中配置 Docker

本文详细介绍Docker的安装步骤,包括环境配置、旧版本移除、软件包安装、GPG密钥添加、稳定源设置及Docker Engine与containerd的安装。同时,指导如何配置中国区官方镜像,实现快速拉取镜像。文章还涵盖了Docker的基本操作,如镜像管理、容器操作、数据卷使用、端口映射和容器互联等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境配置

删除旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc

更新一下 apt
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 -

可以通过uname -a查看自己的架构,我的是x86_64

添加 stable 源

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

更新一下 apt
sudo apt-get update

安装 Docker Engine - Community 和 containerd
sudo apt-get install docker-ce docker-ce-cli containerd.io

配置 Docker 中国区官方镜像
sudo vim /etc/docker/daemon.json
写入

{
“registry-mirrors”: [“https://registry.docker-cn.com“]
}

保存,然后执行下列命令,使配置生效
systemctl daemon-reload
systemctl restart docker

测试是否安装成功
systemctl status docker.service
sudo docker run hello-world

成功会出现以下信息

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.

更新 Docker Engine - Community
首先 sudo apt-get update
然后按照安装步骤选择新版本即可

get.docker.com中有自动化安装脚本

卸载 Docker Engine - Community
sudo apt-get purge docker-ce
删除镜像、容器、卷
sudo rm -rf /var/lib/docker
自定义配置文件需要手动删除

为了避免每次使用 Docker 命令时都要切换到特权身份,可以将当前用户加入 docker 用户组
sudo usermod -aG docker USER_NAME
退出并重新登录即可生效

使用镜像

获取镜像
docker pull ubuntu:18.04
从输出信息可以看出,镜像是由若干层组成的

22e816666fd6: Pull complete
079b6d2a1e53: Pull complete
11048ebae908: Pull complete
c58094023a2e: Pull complete

列出所有镜像
docker images

删除镜像
docker rmi IMAGE
IMAGE 可以为标签或 ID
要先删除依赖该镜像的所有容器,才能删除镜像
清理临时无用的镜像
docker image prune -f
存入与载入镜像
docker save -o ubuntu_18.04.tar ubuntu:18.04
docker load -i ubuntu_18.04.tar

上传镜像
docker tag test:latest user/test:latest
docker push user/test:latest

操作容器

新建容器:
docker create -it ubuntu:18.04
查看
docker ps -a
启动容器
docker start CONTAINER_ID
查看
docker ps
创建和启动可以合并
docker run ubuntu
让 Docker 容器在后台以守护态(Daemonized)形式运行,添加“-d”参数在·
docker run -d ubuntu
查看容器输出
docker [container]logs
例如,docker logs 6838a027473a
暂停容器
docker [container] pause CONTAINER
恢复暂停
docker [container] unpause CONTSINER
终止容器
docker [container] stop [-t|--time[=10]][CONTAINER......]
此时,执行docker container prune,会自动清除掉所有处于停止状态的容器。
当使用-d参数时,容器启动后会进入后台,用户无法看到容器中的信息,也无法进行操作。
如果需要进入容器进行操作,可以使用 exec 命令
docker exec -it 243c32535da7 /bin/bash
当 Docker 容器中指定的应用终结时,容器也会自动终止。

删除处于终止或退出状态的容器
docker [container] rm CONTAINER
查看容器详情
docker container inspect CONTAINER

数据管理

Data Volumes 类似 Linux 中的 mount,支持三种类型:

  • volume,普通数据卷,映射到/var/lib/docker/volumes
  • bind,绑定数据卷,映射到主机指定路径下
  • tmpfs,临时数据卷,只存在于内存中

例如,创建一个数据卷挂载到容器的/opt/webapp目录
docker run -d -P --name web --mount type=bind, source=/webapp, destination=/opt/webapp training/webapp python app.py

挂载本地目录到 docker 中
docker run -it -v /home/username/DockerVolume:/opt/mrs sequenceiq/hadoop-docker:latest /bin/bash
此命令即将本地目录/home/username/DockerVolume挂载到 docker 中的/opt/mrs

如果要往远程服务器传数据,可以使用 scp,命令如下
scp local2remote.txt root@192.168.3.4:<remote_dir>

数据卷容器也是一个容器,它的目的是专门提供数据卷给其他容器挂载。
首先,创建数据卷容器
docker run -it -v /dbdata --name dbdata ubuntu:18.04
其次,创建其他容器,从数据卷容器中挂载数据卷
docker run -it --volumes-from dbdata --name db1 ubuntu:18.04
使用--volume-from参数所挂载数据卷的容器自身并不需要保持在运行状态。

备份
docker run --volumes-from dbdata -v $(pwd):/backup --name worker ubuntu tar cvf /backup/backup.tar /dbdata
恢复
docker run -v /dbdata --name dbdata2 ubuntu /bin/bash
docker run --volumes-from dbdata2 -v $(pwd):/backup busybox tar xvf /backup/backup.tar

端口映射与容器互联

可以通过-P 或 -p 参数来指定端口映射。当使用大写 P 时,Docker 会随机映射一个 49000-49900 的端口到内部容器开放的网络端口。
docker run -d -P training/webapp python app.py
docker ps -l
小写的 p 则可以指定要映射的端口

容器互联(linking)
先创建一个新的数据库容器
docker run -d --name db training/postgres
再创建一个新的 Web 容器,并将它连接到 db 容器
docker run -d -P --name web --link db:db training/webapp python app.py
link 参数的格式为--link name:alias
查看容器的连接
docker ps
使用 env 命令来查看 web 容器的环境变量
docker run --rm --name web2 --link db:db training/webapp env
其中以 DB_开头的环境变量是供 web 容器连接 db 容器使用的

测试连接 db 容器
docker run -t -i --rm --link db:db training/webapp /bin/bash
cat /etc/hosts
ping db

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值