实训35天(容器的基础命令使用)

1.创建一个容器并同时执行echo命令
# 快速启动一个容器执行特定的一次性命令并查看输出结果,输出结果后容器直接退出
[root@docker ~]# docker run -it --name=a0 centos:latest echo "abc"
abc
[root@docker ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS   PORTS     NAMES
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND       CREATED         STATUS           
          PORTS     NAMES
a1c0c9551a19   centos:latest   "echo abc"    8 seconds ago   Exited (0) 7 
seconds ago             a0
41c9a7796fde   centos:latest   "/bin/bash"   19 hours ago   Exited (0) 13 
hours ago             c0

2.删除容器
# 删除名为a0的容器
[root@docker ~]# docker rm a0
a0
# 开启容器
[root@docker ~]# docker start c0
c0
[root@docker ~]# docker ps
CONTAINER ID   IMAGE           COMMAND       CREATED       STATUS         
PORTS     NAMES
41c9a7796fde   centos:latest   "/bin/bash"   19 hours ago   Up 3 seconds     
        c0
# 由于该容器正在运行不能直接删除
[root@docker ~]# docker rm c0
Error response from daemon: cannot remove container "/c0": container is 
running: stop the container before removing or force remove
# 使用-f选项强制移除正在运行中的容器
[root@docker ~]# docker rm -f c0
c0
 

3.设置远程管理
# 自己规定套接字文件的位置并通过TCP协议在所有网络接口(0.0.0.0代表监听所有IP地址)的2375
端口上接受连接
[root@docker ~]# vim /etc/docker/daemon.json 
        "hosts" : [
                "tcp://0.0.0.0:2375",
                "unix:///var/run/docker.sock"
       ]
# 启动Docker服务时执行的命令为/usr/bin/dockerd,即启动Docker守护进程
[root@docker ~]# vim /usr/lib/systemd/system/docker.service 
ExecStart=/usr/bin/dockerd
# 重新加载系统的服务管理器配置
[root@docker ~]# systenctl daemon-reload
# 重新启动Docker服务,使对Docker服务配置文件的修改生效
[root@docker ~]# systemctl restart docker
# version用于查看指定IP地址(10.0.0.100)上的Docker版本信息;images用于查看指定IP地址
上的Docker镜像列表。命令中的参数-H用于指定连接到的 Docker守护进程的地址
[root@docker ~]# docker -H10.0.0.100 varsion|images
 

4.docker Image中pull的使用,从镜像仓库中下载镜像
[root@docker ~]# docker image --help
 pull       Download an image from a registry
[root@docker ~]# docker image pull --help
Usage: docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
Download an image from a registry
Aliases:
 docker image pull, docker pull
Options:
  -a, --all-tags               Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
      --platform string         Set platform if server is multi-platform 
capable
  -q, --quiet                   Suppress verbose output
# 从仓库中拉取centos的镜像
[root@docker ~]# docker image pull -q centos
# 查看本地镜像
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest   5d0da3dc9764   2 years ago   231MB
 

5.使用save将镜像打包
# 查看帮助文件,找到save,可以将镜像保存为一个tar包
[root@docker ~]# docker --help
 save       Save one or more images to a tar archive (streamed to STDOUT by 
default)
# 查看save使用方式
[root@docker ~]# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Aliases:
 docker image save, docker save
Options:
  -o, --output string   Write to a file, instead of STDOUT
# 查看现有镜像
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest   5d0da3dc9764   2 years ago   231MB
# 打包镜像
[root@docker ~]# docker save -o centos.tar centos:latest
# 可以将tar发给其他用户,也可以作为备份
[root@docker ~]# ls
anaconda-ks.cfg centos.tar v

6.使用rmi删除镜像
[root@docker ~]# docker --help
  rm         Remove one or more containers
 rmi         Remove one or more images
[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE           COMMAND       CREATED         STATUS           
          PORTS     NAMES
6e2dbeb436db   centos:latest   "/bin/bash"   9 seconds ago   Exited (0) 4 
seconds ago             c0
# 由于有容器在使用该镜像,所以不能直接删除该镜像
[root@docker ~]# docker rmi centos:latest 
Error response from daemon: conflict: unable to remove repository reference 
"centos:latest" (must force) - container 6e2dbeb436db is using its referenced 
image 5d0da3dc9764
# 删除容器,退出容器up状态
[root@docker ~]# docker rm c0
c0
[root@docker ~]# docker rmi centos:latest 
Untagged: centos:latest
Untagged: 
centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b17
7
Deleted: 
sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: 
sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
 

7.使用oad从tar文件中引入镜像
[root@docker ~]# docker --help
load       Load an image from a tar archive or STDIN
[root@docker ~]# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Aliases:
 docker image load, docker load
Options:
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet         Suppress the load output
[root@docker ~]# docker load -i centos.tar 
74ddd0ec08fa: Loading layer  238.6MB/238.6MB
Loaded image: centos:latest
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest   5d0da3dc9764   2 years ago   231MB
 

8.使用export封装镜像,使用import导入镜像
[root@docker ~]# docker run -it --name c0 centos:latest /bin/bash
[root@5906fffd3383 /]# cd /etc/yum.repos.d    

[root@5906fffd3383 yum.repos.d]# rm -rf *
[root@5906fffd3383 yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo 
https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
 % Total   % Received % Xferd Average Speed   Time   Time     Time 
Current
                                 Dload Upload   Total   Spent   Left Speed
100  2495  100  2495    0     0   6316      0 --:--:-- --:--:-- --:--:-- 
 6316
[root@5906fffd3383 yum.repos.d]# ls
CentOS-Base.repo
[root@5906fffd3383 yum.repos.d]# yum -y install epel
[root@5906fffd3383 yum.repos.d]# yum clean all && yum makecache
# 保持容器是运行状态
[root@41c9a7796fde /]# read escape sequence
# 查看帮助文档
[root@docker ~]# docker --help
[root@docker ~]# docker export --help
Usage: docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive
Aliases:
 docker container export, docker export
Options:
  -o, --output string   Write to a file, instead of STDOUT
# 使用指令,将容器导出为tar包
[root@docker ~]# docker export -o centos_yum.tar c0
# 查看新生成的tar包
[root@docker ~]# ls
anaconda-ks.cfg centos.tar centos_yum.tar v
# 查看帮助文档
[root@docker ~]# docker --help
[root@docker ~]# docker import --help
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Aliases:
 docker image import, docker import
Options:
  -c, --change list       Apply Dockerfile instruction to the created image
  -m, --message string   Set commit message for imported image
      --platform string   Set platform if server is multi-platform capable
# 使用指令,
[root@docker ~]# docker import -m yum centos_yum.tar centos:yum
sha256:f3ca4715914e87e2d1527365e66775d9ab47b2a0208128e3ec52c0d45c364146
# 查看镜像
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED             SIZE
centos       yum       f3ca4715914e   About a minute ago   326MB
centos       latest   5d0da3dc9764   2 years ago         231MB
 

练习:制作httpd镜像

[root@docker ~]# docker run -it --name c0 centos:yum1 /bin/bash
[root@865090f46fbd /]# yum -y install httpd
[root@865090f46fbd /]# echo "aaa" > /var/www/html/index.html
[root@865090f46fbd /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified 
domain name, using 172.17.0.3. Set the 'ServerName' directive globally to 
suppress this message
[root@c717adc1969a /]# [root@docker ~]#
[root@docker ~]# docker ps
CONTAINER ID   IMAGE         COMMAND       CREATED         STATUS         
PORTS     NAMES
865090f46fbd   centos:yum1   "/bin/bash"   44 seconds ago   Up 44 seconds     
        c0
[root@docker ~]# docker export -o centos_httpd.tar c0
[root@docker ~]# docker import -m httpd centos_httpd.tar centos:httpd
sha256:195d7f041bf5c9e17f5cfe172c85c6d2e7d11241e530e839dfe7770186296bb1
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       httpd     195d7f041bf5   13 seconds ago   268MB
centos       yum1     a2a0a138806b   3 hours ago     260MB
centos       latest   5d0da3dc9764   2 years ago     231MB
[root@docker ~]# docker run -it --name c0 centos:httpd /bin/bash
[root@5f6e6f840b2b /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified 
domain name, using 172.17.0.4. Set the 'ServerName' directive globally to 
suppress this message
[root@5f6e6f840b2b /]# curl localhost  
hh
 

9.查看容器ip地址的方法
# 直接进入到容器中查看IP地址
[root@docker ~]# docker attach c1
[root@452771f307cc /]# ip addr
inet 172.17.0.3/16 brd 172.17.255.255 scope 
# 查看容器信息,可以查看ip地址
[root@docker ~]# docker inspect c0
# 在外部调用指令,查看容器ip地址
[root@docker ~]# docker exec c1 ip a
inet 172.17.0.3/16 brd 172.17.255.255 scope 


练习测试:使用exec touch一个文件

[root@docker ~]# docker exec c1 touch 1.txt
[root@docker ~]# docker attach c1
[root@452771f307cc /]# ls
1.txt dev home lib64       media opt   root sbin sys usr
bin   etc lib   lost+found mnt   proc run srv   tmp var

10.容器中虚拟端口映射
# 将容器中的80端口映射到宿主机(docker主机)的80端口   -p宿主机端口:容器端口
[root@docker ~]# docker run -it --name c2 -p80:80/tcp centos:httpd1 /bin/bash
[root@5f6e6f840b2b /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified 
domain name, using 172.17.0.4. Set the 'ServerName' directive globally to 
suppress this message
[root@5f6e6f840b2b /]# curl localhost  
hh
[root@5f6e6f840b2b /]# [root@docker ~]# 
[root@docker ~]# docker inspect c2
"IPAddress": "172.17.0.4",
[root@docker ~]# curl 172.17.0.4
hh
# 浏览器访问宿主机的80端口

# 将容器中的80端口映射到宿主机(docker主机)的12345端口
[root@docker ~]# docker run -it --name c3 -p12345:80 centos:httpd1 /bin/bash
# 需要启动服务
[root@fa074e53e1d6 /]# curl localhost  
curl: (7) Failed to connect to localhost port 80: Connection refused
[root@fa074e53e1d6 /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified 
domain name, using 172.17.0.5. Set the 'ServerName' directive globally to 
suppress this message
[root@fa074e53e1d6 /]# curl localhost
hh
[root@fa074e53e1d6 /]# [root@docker ~]# 
[root@docker ~]# docker ps
CONTAINER ID   IMAGE           COMMAND       CREATED         STATUS         
PORTS                                     NAMES
fa074e53e1d6   centos:httpd1   "/bin/bash"   5 minutes ago   Up 5 minutes   
 0.0.0.0:12345->80/tcp, :::12345->80/tcp   c3
5f6e6f840b2b   centos:httpd1   "/bin/bash"   13 minutes ago   Up 13 minutes   
0.0.0.0:80->80/tcp, :::80->80/tcp         c2
# 浏览器访问宿主机的12345端口

# 宿主机上所有端口的应用情况,防止映射端口时产生端口冲突

[root@docker ~]# less /etc/services
# 停止容器
[root@docker ~]# docker stop c0 c1 c2 c3
c0
c1
c2
c3
# 删除容器
[root@docker ~]# docker rm c0 c1 c2 c3
c0
c1
c2
c3
# 随机为容器指定映射端口(映射端口大于等于32767端口)
[root@docker ~]# docker run -it --name c0 -p80 centos:httpd1 /bin/bash
[root@46580643be3c /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified 
domain name, using 172.17.0.2. Set the 'ServerName' directive globally to 
suppress this message
[root@46580643be3c /]# curl localhost  
hh
[root@46580643be3c /]# [root@docker ~]# 
[root@docker ~]# docker ps 
CONTAINER ID   IMAGE           COMMAND       CREATED         STATUS         
PORTS                                     NAMES
46580643be3c   centos:httpd1   "/bin/bash"   35 seconds ago   Up 34 seconds   
0.0.0.0:32768->80/tcp, :::32768->80/tcp   c0
# 浏览器访问宿主机的32768端口

[root@docker ~]# docker run -it --name c1 -p80 centos:httpd1 /bin/bash
[root@fe69a949d832 /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified 
domain name, using 172.17.0.3. Set the 'ServerName' directive globally to 
suppress this message
[root@fe69a949d832 /]# [root@docker ~]# 
[root@docker ~]# docker ps
CONTAINER ID   IMAGE           COMMAND       CREATED         STATUS         
PORTS                                     NAMES
fe69a949d832   centos:httpd1   "/bin/bash"   20 seconds ago   Up 19 seconds   
0.0.0.0:32769->80/tcp, :::32769->80/tcp   c1
46580643be3c   centos:httpd1   "/bin/bash"   4 minutes ago   Up 4 minutes   
 0.0.0.0:32768->80/tcp, :::32768->80/tcp   c0
 

11.通过宿主机中其它的ip地址的端口映射容器的端口
# 添加一块虚拟网卡
[root@docker ~]# ifconfig ens33:0 10.0.0.101 broadcast 10.0.0.101 netmask 
255.255.255.255 up
# 强制删除正在运行中的容器
[root@docker ~]# docker rm -f c0 c1
c0
c1
# 创建一个容器将容器的80端口映射到宿主机的10.0.0.101地址下的随机端口上
[root@docker ~]# docker run -it --name c0 -p10.0.0.101::80 centos:httpd1 
/bin/bash
[root@c9ea4034d9e2 /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified 
domain name, using 172.17.0.2. Set the 'ServerName' directive globally to 
suppress this message
[root@c9ea4034d9e2 /]# curl localhost  
hh
[root@c9ea4034d9e2 /]# [root@docker ~]# 
[root@docker ~]# docker ps
CONTAINER ID   IMAGE           COMMAND       CREATED         STATUS         
PORTS                     NAMES
c9ea4034d9e2   centos:httpd1   "/bin/bash"   43 seconds ago   Up 43 seconds   
10.0.0.101:32768->80/tcp   c0
# 浏览器访问宿主机10.0.0.101的32768端口

12.容器数据持久化保存(卷映射)
        因为dockers容器只是一个工具,不需要保存数据,不需要在持久化,如果要做持久化,那么就需要 保存到宿主机上,需要宿主机和容器之间有一个共享卷。

[root@docker ~]# docker ps
CONTAINER ID   IMAGE           COMMAND       CREATED         STATUS         
PORTS                     NAMES
c9ea4034d9e2   centos:httpd1   "/bin/bash"   23 minutes ago   Up 23 minutes   
10.0.0.101:32768->80/tcp   c0
# 强制删除正在运行中的容器
[root@docker ~]# docker rm -f c0
c0
# 创建映射目录
[root@docker ~]# mkdir /source
# 创建一个容器并将容器的/data目录映射到宿主机的/source目录上
[root@docker ~]# docker run -it --name c0 -v /source:/data centos:httpd1 
/bin/bash
[root@13f4660eef3e /]# ls
bin   dev home lib64     media opt   root sbin sys usr
data etc lib lost+found mnt   proc run srv   tmp var
[root@13f4660eef3e /]# [root@docker ~]# 
[root@docker ~]# touch /source/hh.txt
[root@docker ~]# docker exec c0 ls /data
hh.txt
[root@docker ~]# docker exec c0 touch /data/hehe.txt
[root@docker ~]# ls /source/
hehe.txt hh.txt
 

13.总结
(1)镜像的迁移
1)打包
docker save -o centos.tar centos:lstest

2)加载
docker load -i centos.tar 
docker images
# 停用关闭容器
docker rm c0
# 删除镜像
docker rmi centos:latest
 

(2)创建镜像
1)创建镜像
docker run -it --name c0 centos:latest /bin/bash
# 下载阿里云仓库
# clean,,makecache
ctrl p q


2)容器打包
docker export -o centos_yum.tar c0

3)引入镜像
docker import -m "updata yum repo" centos_yum.tar centos:yum

docker images

(3)ip网络
1)交互式
docker start c0
docker attach c0
yum -y install iproute
ip a
 

2)inspect
docker inspect c0

3)exec
docker exec c0 yum -y install net-tools

docker exec c0 ifconfig

(4)端口
1) 指定端口映射
docker run -it --name c0 -p70:80 centos:httpd /bin/bash

2)随机端口映射
docker run -it --name c0 -p80 centos:httpd /bin/bash

3)指定其他ip的端口映射
ifconfig ens33:0 10.0.0.101 broadcast 10.0.0.101 netmask 255.255.255.255 up
docker run -it --name c0 -p10.0.0.101::80 centos:httpd /bin/bash
 

(5)持久化
1)挂载
mkdir /source
docker run -it --name c0 -v /source:/data sentos:httpd /bin/bash

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值