在矩池云上练习Docker基础操作

1 租用机器

首先你在矩池云机器租用界面,租用一台支持Docker的服务器,目前只有NVIDIA Tesla P100-16GB 公测机器支持docker。

机器租用成功后,租用页面会显示服务器ssh连接和自定义端口对应的访问链接(如上图我自定义了一个8888端口)。

2 选中一个工具连接服务器

你可以选择任意你熟悉的ssh连接工具远程连接服务器,在矩池云支持中心有很多相关教程,如:Pycharm、VScode、Xshell、PuTTY等。

3 Docker基本操作

3.1 基本

  • 查看docker版本 docker --version
root@localhost:~# docker --version
Docker version 20.10.7, build 20.10.7-0ubuntu5~18.04.3
  • 查看docker系统相关信息 docker info
root@localhost:~# docker info
Client:
 Context:    default
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.7
...
  • 查看docker使用镜像源
root@localhost:~# cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}

3.2 Docker的Hello world

root@localhost:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest

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

docker run hello-world实际上是在通过镜像hello-world创建一个容器,并运行,首先会从本地镜像里找是否存在hello-world镜像,发现没有后,会从远端查找是否有同名镜像,如果有就会拉取到本地,然后创建容器并运行,上面的运行结果就是Hello from Docker!

3.3 镜像相关

  • 创建镜像 docker create
docker create 镜像名字
  • 查看当前系统有哪些镜像 docker images
root@localhost:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   7 months ago   13.3kB

有一个我们刚刚从远程拉取的镜像。

  • 查找镜像 docker search python
root@localhost:/# docker search python
NAME                                     DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
python                                   Python is an interpreted, interactive, objec…   7348      [OK]       
continuumio/anaconda3                    Powerful and flexible python distribution       602                  [OK]
pypy                                     PyPy is a fast, compliant alternative implem…   318       [OK]       
continuumio/anaconda                     Powerful and flexible python distribution       219                  [OK]
circleci/python                          Python is an interpreted, interactive, objec…   48                   
hylang                                   Hy is a Lisp dialect that translates express…   44        [OK]       
amazon/aws-lambda-python                 AWS Lambda base images for Python               42            
  • 从远程拉取镜像 docker pull

如果是拉取公共镜像,我们可以先官网看一眼镜像可用的版本有哪些,如:拉取一个python3.9镜像。

# https://registry.hub.docker.com/_/python
root@localhost:~# docker pull  python:3.9-slim
3.9-slim: Pulling from library/python
a2abf6c4d29d: Pull complete 
27003db43ed4: Pull complete 
eba080ca5a70: Pull complete 
00ceeca3d803: Pull complete 
8980b900ecd2: Pull complete 
Digest: sha256:f4efbe5d1eb52c221fded79ddf18e4baa0606e7766afe2f07b0b330a9e79564a
Status: Downloaded newer image for python:3.9-slim
docker.io/library/python:3.9-slim
  • 删除镜像 docker rmi 镜像id
root@localhost:/# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
python       3.9-slim   8ace3a02b842   4 months ago   122MB
hello-world   latest    feb5d9fea6a5   7 months ago   13.3kB
root@localhost:/# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED       STATUS                   PORTS     NAMES
228f30f90c32   hello-world   "/hello"   2 hours ago   Exited (0) 2 hours ago             serene_montalcini
root@localhost:/# docker rm 228f30f90c32
228f30f90c32
root@localhost:/# docker rmi feb5d9fea6a5
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
root@localhost:/# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
python       latest    a5d7930b60cc   4 months ago   917MB

在删除镜像前,首先要删除引用过该镜像的容器,不然会报错:

root@localhost:/# docker rmi feb5d9fea6a5
Error response from daemon: conflict: unable to delete feb5d9fea6a5 (must be forced) - image is being used by stopped container 228f30f90c32

3.4 容器相关

  • 创建容器 docker run
root@localhost:~# docker run -it --name=pythonme 8ace3a02b842 bash
root@bf24be357fd6:/# python --version
Python 3.9.9
root@bf24be357fd6:/# exit
exit
root@localhost:~# 

注意在进入容器后,hostname从localhost变成了bf24be357fd6

docker run参数:

--name 指定容器名字
-d 后台运行
-it 使用交互方式运行,进入容器
  • 启动容器 docker start

上面我们使用exit指令退出了容器,由于运行docker run的时候没有指定-d指令 后台运行,所以我们下次使用还得启动下容器。

root@localhost:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND     CREATED          STATUS                       PORTS     NAMES
bf24be357fd6   8ace3a02b842   "bash"      5 minutes ago    Exited (0) 4 minutes ago               pythonme
root@localhost:~# docker start pythonme
pythonme
root@localhost:~# docker ps
CONTAINER ID   IMAGE          COMMAND   CREATED         STATUS         PORTS     NAMES
bf24be357fd6   8ace3a02b842   "bash"    5 minutes ago   Up 4 seconds             pythonme

启动容器使用docker start命令,后面可以接容器名称或者容器id。

  • 交互式方式进入正在运行的容器 docker exec

docker exec -it 容器id/容器名称 bash

root@localhost:~# docker exec -it pythonme bash
root@bf24be357fd6:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@bf24be357fd6:/# python --version
Python 3.9.9
  • 将修改后的容器提交保存为镜像

docker commit -m=“描述信息” 容器id/容器名称 新镜像仓库名:新镜像tag

root@localhost:~# docker exec -it pythonme bash
root@bf24be357fd6:/# cd home/
root@bf24be357fd6:/home# touch 123.txt && echo "hello docker" > 123.txt
root@bf24be357fd6:/home# cat 123.txt 
hello docker
root@bf24be357fd6:/home# exit
exit
root@localhost:~# docker ps
CONTAINER ID   IMAGE          COMMAND   CREATED          STATUS          PORTS     NAMES
bf24be357fd6   8ace3a02b842   "bash"    22 minutes ago   Up 16 minutes             pythonme
root@localhost:~# docker commit -m="new /home/123.txt" pythonme python3.9:1.0
sha256:1d13116221b5467befc6cf733c10a78b22e3499b4aaa18173509c574808bd773
root@localhost:~# docker images
REPOSITORY   TAG        IMAGE ID       CREATED              SIZE
python3.9    1.0        1d13116221b5   11 seconds ago       122MB
python       3.9-slim   8ace3a02b842   4 months ago         122MB
  • 停止运行中的容器

docker stop 容器id/容器名称

  • 重启容器

docker restart 容器id/容器名称

  • 删除容器

docker rm 容器id/容器名称

文中如有错误,请评论指出,更多docker相关操作我们也会继续更新。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值