ubuntu部署-docker的简单使用

ubuntu部署-docker的简单使用

下载与安装

我这里使用的是手动安装

先卸载旧版本的dockers

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

使用dockers仓库安装

首次安装需要先在Ubuntu的源上添加dockers官方的源

设置仓库(dockers源)

更新一下Ubuntu的apt的软件包索引

1
sudo apt-get update

安装apt依赖包,用于HTTPS获取仓库

1
2
3
4
5
6
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

添加 Docker 的官方 GPG 密钥:

1
sudo curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

验证你是否拥有带有指纹的密钥

1
2
3
4
5
6
 sudo apt-key fingerprint 0EBFCD88

pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ unknown] Docker Release (CE deb) <docker@docker.com>
sub rsa4096 2017-02-22 [S]

使用以下指令在源那里加上仓库

1
2
3
4
sudo add-apt-repository \
"deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
$(lsb_release -cs) \
stable"

再更新apt索引

1
sudo apt-get update

直接安装最新版dockers

1
sudo apt-get install docker-ce docker-ce-cli containerd.io

测试是否安装成功

执行以下命令,打印以下信息则是成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
sudo docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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/

其他安装方式

需要其他安装方式则可前往Ubuntu Docker 安装 | 菜鸟教程 (runoob.com)

或者是自行查找

对于docker的使用

本文只做docker的基础操作介绍

镜像操作

我们都知道镜像是用来创建容器的那我们怎么样获取镜像和用镜像来生成容器和一些操作的那么接下来就是介绍一些关于镜像的一些操作

获取镜像

建议使用docker pull 的方式获取

其命令格式

1
sudo docker pull [OPTIONS] NAME[:TAG|@DIGEST]

意思就是在docker源那里拉取相应的镜像

  • name:就是要安装的镜像名字,若如果没有以下两个标签则是以:latestdocker pull ubuntu默认为docker pull ubuntu:latest

  • tag:就是获取这个镜像相应版本的最新版,比如sudo docker pull ubuntu:18.04就是拉取系统版本为18.04的ubuntu镜像最新版

  • digest:表示是拉取指定版本的镜像系统,如 sudo docker pull ubuntu@sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2

docker run

1
sudo docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

其实docker run 命令就是下载一个本地没有的镜像,并且创建一个容器

例如

1
sudo docker run --rm  -it -d --gpus all --name test  ubuntu /bin/bash 
  • 以Ubuntu镜像创建一个名为test的容器
  • –rm 表示退出这个容器后自动移除这个容器
  • -d是指容器启动后以后台分离模式运行,默认会在前台模式运行,
  • -t和-i和/bin/bash,加起来表示使用bash创建一个伪终端,进入容器交互模式
  • gpus all 表示对于该容器,开启gpu支持,并且所有GPU都可用

Docker容器后台运行,就必须有一个前台进程!

你如果执行sudo docker run -d –name test ubuntu ,虽然-d表示容器在后台挂起但事实上你会发现并不会在后台挂起,因为主线程任务已经执行完,而容器也自动关闭了

所以,sudo docker -itd –name test ubuntu 可以解决这个问题

查看镜像和容器

查看镜像

1
2
sudo docker images
#查看本机已有的镜像

查看容器

1
2
3
4
sudo docker ps 
#查看正在运行的容器信息
sudo docker ps -a
#查看全部容器信息

删除镜像

1
sudo docker rmi [OPTIONS] IMAGE [IMAGE...]

支持删除多种镜像

  • 使用image id :sudo docker rmi fd484f19954f –这个image id可以直接sudo docker images查看镜像id
  • 使用tag:docker rmi test:latest
  • 使用DIGEST: docker rmi localhost:5000/test/busybox@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf

容器操作

容器就是以镜像为只读模板创建的

创建容器

前面已经说了可以以docker run来创建容器这里就不多说了

进入容器交互

要进入容器分两种情况

  • 进入已经后台分离模式下运行的容器内
  • 已经关闭的容器内
后台分离模式下运行的容器

对于已经启动的容器有两个命令进入 attachexec

1
2
3
4
5
sudo docker exec -it test bash
#进入一个名为test的容器并打开伪终端
sudo docker attach
#attach不能使用-it bash
#进入一个名为test的容器并打开伪终端

然后你在退出容器

1
exit

这个时候你会发现,当你以exec进入容器后退出容器并不会关闭而attach则相反

关闭的容器

关闭的容器当然是先启动再进入咯

重启容器的方式

1
sudo docker restart 容器名或者容器id

启动容器的方式

1
sudo docker start 容器名或者容器id

不管是重启还是启动,容器都是以后台分离的模式运行

然后再进入容器内即可

删除容器

都是基于docker rm来进行

1
docker rm [OPTIONS] CONTAINER [CONTAINER...]

例如删除ubuntu-test2这个容器

1
sudo docker rm ubuntu-test2

容器的停止和关闭和暂停

停止

1
2
3
sudo docker stop test
#也可以加入参数-t,表示在一定时间内无法关闭则强制关闭,默认时间为10s
#比如sudo docker stop -t=20 test

关闭

1
2
sudo docker kill test
#直接关闭容器

暂停

1
2
3
4
sudo docker pause test
#暂停运行
sudo docker unpause test
#恢复运行
  • stop,kill和pause的差别是,stop,kill让容器停止而pause则是让容器挂起

ubuntu部署-docker的简单使用
http://move-brain.github.io/super_zhu/2022/11/20/ubuntu部署-docker/
作者
super_zhu
发布于
2022年11月20日
更新于
2022年11月22日
许可协议