Docker Tutorial for Beginer(day1)

Docker Tutorial for Beginer(day1)

一、install

Docker 的学习就不能再在windows上进行了,虽然windows上也可以安装,但是bug又多又不好用。还要开通WSL污染环境,所以我选择在华为云上开通一个云服务器。

两种安装方式
1.脚本安装
ps:在测试或开发环境中 Docker 官方为了简化安装流程,提供了一套便捷的安装脚本
# $ curl -fsSL test.docker.com -o get-docker.sh
# $ sudo sh get-docker.sh --mirror AzureChinaCloud
ps:国外的脚本,无梯子加速者不推荐使用

$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun
ps:使用国内ali镜像,安装快,国内网即可
2.apt 安装
ps:由于 apt 源使用 HTTPS 以确保软件下载过程中不被篡改。因此,我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书。
$ sudo apt-get update
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
    
ps:添加软件源的 GPG 密钥
$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 官方源
# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

ps:向 sources.list 中添加 Docker 软件源
$ echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 官方源
# $ echo \
#   "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
#   $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

ps:更新 apt 软件包缓存,并安装 docker-ce:
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
验证安装以及镜像加速
验证
//测试安装成果
$ docker run --rm hello-world
ps:出现以下输出即安装成功
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/
镜像加速

小伙伴看到加速基本上就是懂得都懂了,奈何服务器在国外,一个镜像要拉到猴年马月。。。。,所以呀,就要配置镜像鸭!

ps:我通过阿里云进行镜像加速,登录阿里云(支付宝扫码即可),进入容器镜像服务。根据阿里云的提示即可配置镜像加速。
使用docker 帮助
//docker command --help,如:
docker stats --help

之后的几次尝试参考菜鸟教程,笔者希望通过初步的实践对于docker有一个基本认识。

在day2中开始深入了解docker

二、docker 的初步尝试

//最基本的docker run 命令
//docker run -镜像名 -路径
docker run ubuntu:15.10 /bin/echo "Hello world"
ps:这一段代码的命令的含义是查找ubuntu:15.10镜像,没有则下载,在该镜像的/bin下运行echo "Hello world"命令
//运行交互式的容器
docker run -i -t ubuntu:15.10 /bin/bash
ps:
-t: 在新容器内指定一个伪终端或终端。
-i: 允许你对容器内的标准输入 (STDIN) 进行交互。

运行此命令之后,我们会发现进入里镜像的系统。

运行cat /proc/version命令可以查看系统版本,可以尝试各种Linux 的命令,使用exit可以退出系统。

  • 运行docker 并查看镜像状态
//创建一个容器
docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"

docker images//查看docker 的镜像
root@ecs-3dcc:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    feb5d9fea6a5   7 weeks ago   13.3kB
ubuntu        15.10     9b9cb95443b5   5 years ago   137MB

docker ps //查看容器状态
root@ecs-3dcc:~# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
5448565d319f   ubuntu:15.10   "/bin/sh -c 'while t…"   8 minutes ago   Up 8 minutes             elastic_merkle

ps:其中5448565d319f即为容器的id ,之后对容器的操作可以通过容器id来进行
//查看容器输出
docker logs 5448565d319f
//关闭容器
docker stop 5448565d319f

三、docker 镜像

//首先查看image列表
docker images

root@ecs-3dcc:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    feb5d9fea6a5   7 weeks ago   13.3kB
ubuntu        15.10     9b9cb95443b5   5 years ago   137MB

//查找镜像docker search xxx如:
docker search redis

root@ecs-3dcc:~# docker search redis
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
redis                            Redis is an open source key-value store that…   10171     [OK]
sameersbn/redis                                                                  83                   [OK]
grokzen/redis-cluster            Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2      80
rediscommander/redis-commander   Alpine image for redis-commander - Redis man…   70                   [OK]
....
clearlinux/redis                 Redis key-value data structure server with t…   3
wodby/redis                      Redis container image with orchestration        1                    [OK]
tiredofit/redis                  Redis Server w/ Zabbix monitoring and S6 Ove…   1                    [OK]
xetamus/redis-resource           forked redis-resource                           0                    [OK]


//拉取镜像docker 
docker pull redis
root@ecs-3dcc:~# docker search redis
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
redis                            Redis is an open source key-value store that…   10171     [OK]
sameersbn/redis                                                                  83                   [OK]
grokzen/redis-cluster            Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2      80
......
runnable/redis-stunnel           stunnel to redis provided by linking contain…   1                    [OK]
xetamus/redis-resource           forked redis-resource                           0                    [OK]
root@ecs-3dcc:~# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
7d63c13d9b9b: Pull complete
....
0531663a7f55: Pull complete
9bf50efb265c: Pull complete
Digest: sha256:54ee15a0b0d2c661d46b9bfbf55b181f9a4e7ddf8bf693eec5703dac2c0f5546
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

//删除镜像
docker rmi redis
  • 镜像的更新

很多时候,从镜像仓库拉取的镜像并不能满足我们的需求。此时我们就需要自己创建docker 镜像,创建的方式有两种。

  • 1、从已经创建的容器中更新镜像,并且提交这个镜像
  • 2、使用 Dockerfile 指令来创建一个新的镜像
## 1.从已经创建的容器更新镜像,如:我们现在更新之前拉取的Ubuntu15镜像
## 先进入交互界面
docker run -t -i ubuntu:15.10 /bin/bash
apt-get update
//docker commit -m(描述信息) -a(作者) 容器ID 镜像名
docker commit -m="has update" -a="silent" d59efd18dcc5 ubuntu:v2

root@ecs-3dcc:~# docker commit -m="has update" -a="silent" d59efd18dcc5 ubuntu:v2
sha256:bcf01602715bd246e1dc2dde8dbc3c58179f84d69142dd25b4cf3fa7f0d7117d
root@ecs-3dcc:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu        v2        bcf01602715b   5 seconds ago   137MB
hello-world   latest    feb5d9fea6a5   7 weeks ago     13.3kB
ubuntu        15.10     9b9cb95443b5   5 years ago     137MB
ubuntu        13.10     7f020f7bf345   7 years ago     185MB

## 2.使用 Dockerfile 指令来创建一个新的镜像
这部分一时半会学不完,在Docker deeper dive中补齐

四、docker 容器

## 启动镜像,创建容器
docker run -it ubuntu /bin/bash
## 查看所有容器
docker ps -a
CONTAINER ID   IMAGE          COMMAND        CREATED          STATUS              PORTS     NAMES
5edfa686679f   ubuntu:15.10   "/bin/bash"    6 minutes ago    Exited (0) 6 minutes ago      eager_dirac

## 使用start命令启动容器,docker start 容器ID/容器Name
docker start 5edfa686679f/eager_dirac
## stop 命令关闭容器,语法基本与start一致
docker stop 5edfa686679f/eager_dirac
docker stop $(docker ps -q)//快速停止所有容器
## restart 重启容器
decoker restart 5edfa686679f/eager_dirac
## rm 删除容器
docker rm $(docker ps -aq)//快速删除所有容器
docker container prune //删除所有处于终止的容器

## -d后台运行容器
docker run -itd --name ubuntu-test ubuntu:15.10 /bin/bash
ps:使用-d运行容器时,容器自动在后台运行,此时想进入容器
docker attach <name>:进入容器终端
docker exec:进入容器终端,推荐使用,因为此退出容器终端,不会导致容器的停止。
如:docker exec -it ubuntu-test /bin/bash

## 导入与导出容器
### 导出容器
docker export 676bafbcc4bd > ubuntu.tar
### 导入容器
docker import ubuntu.tar test/ubuntu:v1

学到这里,我对于docker的镜像容器的基本操作有了一次实践,但是,学习的还是不够深入。这在我结束了docker for Beginer 之后会继续补齐。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值