0


Docker

目录

前言:

  • Docker 是一个开源的应用容器引擎
  • 诞生于2013 年初,基于 Go 语言实现,dotCloud 公司出品
  • Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,实现了“Build,Ship and Run Any App,Anywhere”的目标。
  • Docker从17.03 版本之后分为 CE(Community Edition: 社区版)和EE(Enterprise Edition: 企业版)。

一、Docke环境的搭建

1、1查看系统内核版本,docke环境要求Linux内核3.8以上

[root@100-215 ~]# uname -a

1、2如果安装过Docker服务,使用卸载命令

[root@100-215 ~]yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine \
                  docker-ce

1、3安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的

[root@100-215 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

安装完成页面

1、4设置yum源为阿里云

[root@100-215 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

1、5查看仓库中可用的docker版本

[root@100-215 ~]# yum list docker-ce --showduplicates | sort -r

1、6安装Docker

[root@100-215 ~]# yum install docker-ce
或
[root@100-215 ~]# yum install docker-ce-18.06.3.ce-3.el7

安装时出现是否继续下载,输入:y

继续安装下载

第二次出现,继续输入y确认

1、7安装完成后,查看docker版本

[root@100-215 ~]# docker -v
Docker version 18.06.3-ce, build d7080c1

二、启动和关闭

2、1 启动docker

[root@100-215 ~]# systemctl start docker
    或
[root@100-215 ~]# service docker start 

2、2 查看docker状态

[root@100-215 ~]# systemctl status docker
    或
[root@100-215 ~]# service docker status

2、3 关闭docker

[root@100-215 ~]# service docker stop

2、4 重启docker

[root@100-215 ~]# service docker restart

2、5 设置docker开机自启

[root@100-215 ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

2、6通过run hello-world运行

[root@100-215 ~]# docker run hello-world

三、Docker常用命令

3、1 镜像命令

3、1、1 搜索镜像

[root@100-215 ~]# docker search IMAGE

3、1、2 拉取镜像

[root@100-215 ~]docker pull IMAGE:TAG
[root@100-215 ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
Status: Downloaded newer image for redis:latest
    或
[root@100-215 ~]# docker pull redis:3.0
3.0: Pulling from library/redis
f5cc0ee7a6f6: Pull complete 
5fc25ed18e87: Pull complete 
e025bc8872f6: Pull complete 
77c68b51b836: Pull complete 
7c403ece3755: Pull complete 
0a653bd338f4: Pull complete 
31531fd948c6: Pull complete 
Digest: sha256:730b765df9fe96af414da64a2b67f3a5f70b8fd13a31e5096fee4807ed802e20
Status: Downloaded newer image for redis:3.0

3、1、3 列出本地镜像

[root@100-215 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
redis               latest              2f66aad5324a        20 hours ago        117MB
hello-world         latest              feb5d9fea6a5        16 months ago       13.3kB
redis               3.0                 c44fa74ead88        5 years ago         91.6MB

3、1、4 删除镜像

docker rmi IMAGE[:tag]
[root@100-215 ~]# docker rmi redis
Untagged: redis:latest
Untagged: redis@sha256:6a59f1cbb8d28ac484176d52c473494859a512ddba3ea62a547258cf16c9b3ae
Deleted: sha256:2f66aad5324aa9c60ecde39b98e85c5342212d290b50399d4ab01173b349fd42
Deleted: sha256:98ff323507c487cc05088a85a662a0e661cb1300e3acdccd702e16c597ef0816
Deleted: sha256:1e6f664c56497ac1cbe483d3957e25aaa10b7cb8b443c6abbf61c2951a02bddf
Deleted: sha256:2fcc15ff4a45cbab2c0cbb5d76ed1315959bb2cbae3707f67801a8c23ecca910
Deleted: sha256:5c8fb40c05b8edda1e3b30473768d50cff63f48e47845c587bf3f0a5920dccbc
Deleted: sha256:6b8d73641e09bcb724f5d6f947be2e1f3f629b2067c85b7bb28406e562eeb950
Deleted: sha256:4695cdfb426a05673a100e69d2fe9810d9ab2b3dd88ead97c6a3627246d83815

3、2 容器命令

3、2、1 新建并启动容器

docker run [options] IMAGE[:TAG]
[root@100-215 ~]# docker run redis:3.0
1:C 10 Feb 02:28:19.571 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1:M 10 Feb 02:28:19.574 # Server started, Redis version 3.0.7
1:M 10 Feb 02:28:19.575 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 10 Feb 02:28:19.575 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 10 Feb 02:28:19.575 * The server is now ready to accept connections on port 6379
^C1:signal-handler (1675996105) Received SIGINT scheduling shutdown...
1:M 10 Feb 02:28:25.789 # User requested shutdown...
1:M 10 Feb 02:28:25.789 * Saving the final RDB snapshot before exiting.
1:M 10 Feb 02:28:25.791 * DB saved on disk
1:M 10 Feb 02:28:25.791 # Redis is now ready to exit, bye bye...
[root@100-215 ~]# docker run --name redis_1 redis:3.0        #容器名为redis_1
[root@100-215 ~]# docker run --name redis_2  -d redis:3.0        #后台以redis_2名字启动
1e106b7e8d52f85d0b0f3e75dc12c56260ba94a1f260d21a13f80b0b368b991d

3、2、2 查看容器列表

 docker ps [options]
[root@100-215 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1e106b7e8d52        redis:3.0           "docker-entrypoint.s…"   45 seconds ago      Up 44 seconds       6379/tcp            redis_2
[root@100-215 ~]# docker ps -a        #启动容器历史列表
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
1e106b7e8d52        redis:3.0           "docker-entrypoint.s…"   2 minutes ago       Up About a minute           6379/tcp            redis_2
e45181f6f1f7        redis:3.0           "docker-entrypoint.s…"   3 minutes ago       Exited (0) 3 minutes ago                        redis_1
73020eefc0e2        redis:3.0           "docker-entrypoint.s…"   5 minutes ago       Exited (0) 5 minutes ago                        cranky_keller
fb8ab43bbe3b        hello-world         "/hello"                 23 minutes ago      Exited (0) 23 minutes ago                       determined_bassi

3、2、3 停止容器

docker stop CONTAINER NAME
[root@100-215 ~]# docker stop redis_2
redis_2
[root@100-215 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

3、2、4 启动容器

docker start CONTAINER
[root@100-215 ~]# docker start redis_2
redis_2
[root@100-215 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
1e106b7e8d52        redis:3.0           "docker-entrypoint.s…"   7 minutes ago       Up 3 seconds        6379/tcp            redis_2

3、2、5 删除容器

docker rm CONTAINER NAME
[root@100-215 ~]# docker rm redis_2
Error response from daemon: You cannot remove a running container 1e106b7e8d52f85d0b0f3e75dc12c56260ba94a1f260d21a13f80b0b368b991d. Stop the container before attempting removal or force remove
[root@100-215 ~]# docker stop redis_2
redis_2
[root@100-215 ~]# docker rm redis_2
redis_2

3、2、6 与运行中容器交互

docker exec [options] CONTAINER NAME [command]

OPTIONS说明:

  • -i:以交互模式运行容器,通常与-t 连用
  • -t:为容器重新分配一个伪终端,通常与-i 连用
[root@100-215 ~]# docker exec -it redis_2 /bin/bash
root@d998f59787ea:/data# pwd 
/data
root@d998f59787ea:/data# cd /
root@d998f59787ea:/# ll
bash: ll: command not found
root@d998f59787ea:/# ls -l
total 8
drwxr-xr-x   2 root  root  4096 Jun 20  2017 bin
drwxr-xr-x   2 root  root     6 Apr 20  2017 boot
drwxr-xr-x   2 redis redis    6 Jun 23  2017 data
drwxr-xr-x   5 root  root   340 Feb 10 02:45 dev
lrwxrwxrwx   1 root  root    34 Jun 23  2017 entrypoint.sh -> usr/local/bin/docker-entrypoint.sh
drwxr-xr-x   1 root  root    66 Feb 10 02:45 etc
drwxr-xr-x   2 root  root     6 Apr 20  2017 home
drwxr-xr-x   1 root  root     6 Jun 23  2017 lib
drwxr-xr-x   2 root  root    34 Jun 20  2017 lib64
drwxr-xr-x   2 root  root     6 Jun 20  2017 media
drwxr-xr-x   2 root  root     6 Jun 20  2017 mnt
drwxr-xr-x   2 root  root     6 Jun 20  2017 opt
dr-xr-xr-x 225 root  root     0 Feb 10 02:45 proc
drwx------   2 root  root    37 Jun 20  2017 root
drwxr-xr-x   3 root  root    30 Jun 20  2017 run
drwxr-xr-x   2 root  root  4096 Jun 20  2017 sbin
drwxr-xr-x   2 root  root     6 Jun 20  2017 srv
dr-xr-xr-x  13 root  root     0 Feb 10 02:45 sys
drwxrwxrwt   1 root  root     6 Jun 23  2017 tmp
drwxr-xr-x   1 root  root    19 Jun 23  2017 usr
drwxr-xr-x   1 root  root    41 Jun 23  2017 var
root@d998f59787ea:/# exit
exit
[root@100-215 ~]# 

3、2、7 查看容器日志

docker logs CONTAINER NAME
[root@100-215 ~]# docker logs redis_2
1:C 10 Feb 02:45:23.705 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1:M 10 Feb 02:45:23.706 # Server started, Redis version 3.0.7
1:M 10 Feb 02:45:23.707 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 10 Feb 02:45:23.707 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 10 Feb 02:45:23.707 * The server is now ready to accept connections on port 6379

四、Docker数据卷

4、1 容器和宿主机数据共享

docker run ... –v 宿主机目录:容器内目录 ......

语法:创建容器时,使用 –v 参数 设置数据卷。如果目录不存在,会自动创建。

1、建立一个名为c1的容器,并挂载宿主机目录/root/data_host,对应容器目录/root/data_container

[root@100-215 ~]# docker run --name c1 -id -v /root/data_host:/root/data_container centos:7
fe563e6c85ec4f74a9c4ff3f1957587eac1976101cde4f39afdd337d9ee0ac5b
[root@100-215 ~]# ll
总用量 76136
-rw-------. 1 root root     1339 2月  22 2022 anaconda-ks.cfg
drwxr-xr-x  2 root root        6 2月  10 10:57 data_host
-rw-r--r--  1 root root 62903272 2月   6 21:13 kernel-ml-6.1.10-1.el7.elrepo.x86_64.rpm
-rw-r--r--  1 root root 15052156 2月   6 21:13 kernel-ml-devel-6.1.10-1.el7.elrepo.x86_64.rpm

2、在宿主机data_host目录里面创建一个文件,检查容器的data_container目录是否同步

[root@100-215 data_host]# docker exec -it c1 /bin/bash            #容器交互
[root@fe563e6c85ec /]# ll 
total 16
-rw-r--r--   1 root root 12114 Nov 13  2020 anaconda-post.log
lrwxrwxrwx   1 root root     7 Nov 13  2020 bin -> usr/bin
drwxr-xr-x   5 root root   340 Feb 10 02:57 dev
drwxr-xr-x   1 root root    66 Feb 10 02:57 etc
drwxr-xr-x   2 root root     6 Apr 11  2018 home
lrwxrwxrwx   1 root root     7 Nov 13  2020 lib -> usr/lib
lrwxrwxrwx   1 root root     9 Nov 13  2020 lib64 -> usr/lib64
drwxr-xr-x   2 root root     6 Apr 11  2018 media
drwxr-xr-x   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x 227 root root     0 Feb 10 02:57 proc
dr-xr-x---   1 root root    28 Feb 10 02:57 root
drwxr-xr-x  11 root root   148 Nov 13  2020 run
lrwxrwxrwx   1 root root     8 Nov 13  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x  13 root root     0 Feb 10 02:57 sys
drwxrwxrwt   7 root root   132 Nov 13  2020 tmp
drwxr-xr-x  13 root root   155 Nov 13  2020 usr
drwxr-xr-x  18 root root  4096 Nov 13  2020 var
[root@fe563e6c85ec /]# cd root
[root@fe563e6c85ec ~]# ll
total 4
-rw------- 1 root root 3416 Nov 13  2020 anaconda-ks.cfg
drwxr-xr-x 2 root root   19 Feb 10 02:59 data_container        #自动创建data_host
[root@fe563e6c85ec ~]# cd data_container/
[root@fe563e6c85ec data_container]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 10 02:59 a.txt            #a文件已经同步过来了

4、2 容器与容器之间挂载同步数据

1、创建容器c2,挂载data_host数据卷。

[root@localhost ~]# docker run --name c2 -id -v /root/data_host:/root/data_container centos:7 #新建并启动容器

2、在c2容器里面创建一个文件,检查c1容器是否同步

[root@100-215 data_host]# docker run --name c2 -id -v /root/data_host:/root/data_container centos:7
41c6a1fe5a1133c8208ec90e1da74a775509231d6de935815bfd0a218f5d2bab
[root@100-215 data_host]# docker exec -it c2 /bin/bash
[root@41c6a1fe5a11 /]# cd /root/data_container/
[root@41c6a1fe5a11 data_container]# touch b.txt
[root@41c6a1fe5a11 data_container]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 10 02:59 a.txt
-rw-r--r-- 1 root root 0 Feb 10 03:11 b.txt
[root@41c6a1fe5a11 data_container]# exit 
exit
[root@100-215 data_host]# ll
总用量 0
-rw-r--r-- 1 root root 0 2月  10 10:59 a.txt
-rw-r--r-- 1 root root 0 2月  10 11:11 b.txt        #宿主机问价同步
[root@100-215 data_host]# docker exec -it c1 /bin/bash
[root@fe563e6c85ec /]# cd /root/data_container/
[root@fe563e6c85ec data_container]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 10 02:59 a.txt
-rw-r--r-- 1 root root 0 Feb 10 03:11 b.txt        #c1容器文件同步

五、Docker应用安装

5、1 Docker安装MySQL

需求:

  • 在Docker容器中部署MySQL,并通过外部mysql客户端操作MySQL Server。

问题:

  • 容器内的网络服务和外部机器不能直接通信

解决方案:

  • 当容器中的网络服务需要被外部的机器访问时,可以将容器中提供服务的端口映射到宿主机的端口上,外部机器访问宿主机上的端口,从而间接访问容器的服务。(这种操作称为容器映射)。

1、拉取MySQL镜像

[root@100-215 data_host]# docker pull mysql:5.7
5.7: Pulling from library/mysql
e048d0a38742: Pull complete 
c7847c8a41cb: Pull complete 
351a550f260d: Pull complete 
8ce196d9d34f: Pull complete 
17febb6f2030: Pull complete 
d4e426841fb4: Pull complete 
fda41038b9f8: Pull complete 
f47aac56b41b: Pull complete 
a4a90c369737: Pull complete 
97091252395b: Pull complete 
84fac29d61e9: Pull complete 
Digest: sha256:8cf035b14977b26f4a47d98e85949a7dd35e641f88fc24aa4b466b36beecf9d6
Status: Downloaded newer image for mysql:5.7

2、创建容器 端口映射

[root@100-215 data_host]# docker run -p 3306:3306 --name mysql \
> -v /usr/local/docker/mysql/conf:/etc/mysql/conf.d \
> -v /usr/local/docker/mysql/logs:/var/log/mysql \
> -v /usr/local/docker/mysql/data:/var/lib/mysql \
> -e MYSQL_ROOT_PASSWORD=1111 \
> -d mysql:5.7
b2bfabd5ca951f1f6e4630aad08675bfffeb85bda885ccc1071a9fc014121bbc

参数说明:

-p:指定端口映射,容器port:服务port

-e:配置信息,此处配置mysql的root用户的登陆密码

3、修改容器中日志的权限

[root@100-215 data_host]# docker exec -it mysql /bin/bash
bash-4.2# ll        
bash: ll: command not found
bash-4.2# pwd 
/
bash-4.2# cd /var/log/mysql
bash-4.2# pwd
/var/log/mysql
bash-4.2#  chown -R mysql:mysql /var/log/mysql

4、在宿主机中创建mysql.cnf文件

[root@100-215 data_host]# cd /usr/local/docker/mysql/conf/
[root@100-215 conf]# vim mysql.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-bin=/var/log/mysql/mysql-bin
server-id=1

5、重启容器并测试

5、2 Docker安装tomcat

需求:

  • 在Docker容器中部署Tomcat,并通过外部机器访问Tomcat部署的项目。

1、拉取tomcat镜像

[root@100-215 ~]# docker pull tomcat

2、创建容器

[root@100-215 ~]# docker run -p 8080:8080 --name tomcat \
> -v /usr/local/docker/tomcat/webapps:/usr/local/tomcat/webapps \
> -d tomcat
ceff60cc92e43866e785c901c845c2324bd5b2222f448e67e52a7c1d4bfa6f75

3、在宿主机上部署工程

[root@100-215 ~]# cd /usr/local/docker/tomcat/webapps/
[root@100-215 webapps]# mkdir test
[root@100-215 webapps]# cd test/
[root@100-215 test]# vim index.html
<h1>hello,docker!!!</h1>

4、测试

访问:ip:8080/test

5、3 Docker安装Redis

需求:

  • 在Docker容器中部署Redis,并通过外部Redis客户端操作Redis Server。

1、下载安装包并解压

[root@100-215 upload]# wget http://download.redis.io/releases/redis-3.0.0.tar.gz
[root@100-215 upload]# tar -zxf redis-3.0.0.tar.gz -C /usr/local

2、拉取镜像

[root@100-215 local]# docker pull redis:3.0

3、拷贝redis.conf

[[root@100-215 local]# cd /usr/local/docker/
[root@100-215 docker]# mkdir -p redis/data
[root@100-215 docker]# mkdir -p redis/conf
[root@100-215 docker]# cp /usr/local/redis-3.0.0/redis.conf /usr/local/docker/redis/conf/

4、创建容器

docker run -p 6379:6379 --name redis \
-v /usr/local/docker/redis/conf:/etc/redis/redis.conf \
-v /usr/local/docker/redis/data:/data \
-d redis:3.0 redis-server /etc/redis/redis.conf

5、测试

六、Docker File

1、什么是Docker File?

Docker File是用来构建 Docker 镜像的构建文件,是由一系列命令和参数构成的脚本。

从应用软件的角度来看,Docker File、Docker镜像与Docker容器分别代表软件的三个不同阶段,Docker File面向开发,Docker镜像成为交付标准,Docker容器则涉及部署与运维,三者缺一不可,合力充当Docker体系的基石。

2、Docker File常用指令

  • FROM:指定构建使用的基础镜像,FROM命令必须写在其他的指令前
  • EXPOSE:声明容器对外暴露的端口号
  • ENV:设置环境变量,可以在容器内获取使用
  • WORKDIR:终端默认的工作目录
  • VOLUME :使容器内的一个目录具有持久化存储的功能
  • CMD:指定容器启动时执行的一条命令,如果指定了多条CMD命令,则只有最后一条会被执行;CMD会被 docker run之后的参数替换
  • ENTRYPOINT:同CMD,但docker run之后的参数会传递给ENTRYPOINT形成新的命令组合。
  • RUN:不同于CMD指令,RUN可以在Dockerfile中出现和执行多次
  • ADD:从src复制文件到容器中的dest
  • COPY:类似于ADD,不支持压缩包
  • MAINTAINER:用于为Dockerfile署名
  • ONBUILD:是一个特殊的指令,只有当以当前镜像为基础镜像,去构建下一级镜像的时候才会被执行

图解Docker File

3、入门Docker File

①需求

  1. 登陆后的默认路径
  2. vim编辑器
  3. 查看网络配置ifconfig支持

②编写Docker File

FROM centos:7 #继承centos:7
MAINTAINER <[email protected]>

ENV MYPATH /usr/local
WORKDIR $MYPATH
 
RUN yum -y install vim
RUN yum -y install net-tools

③构建

docker build [OPTIONS] PATH

OPTIONS说明:

  • -f:DockerFile路径
  • -t :自定义镜像名称:TAG
[root@iZ2vc772r7963jijk5rxjlZ docker]# docker build -f Dockerfile -t mycentos:1.0 .

测试

[root@iZ2vc772r7963jijk5rxjlZ docker]# docker run -it --name mycentos mycentos:1.0                                                                                                              
[root@16ab83fa21bb local]# vim a.txt                                                                                                                                                            
[root@16ab83fa21bb local]# pwd 
/usr/local
[root@16ab83fa21bb local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 16  bytes 1379 (1.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

④压缩镜像

如果我们要把镜像给别人使用,需要压缩镜像:

[root@iZ2vc772r7963jijk5rxjlZ docker]# docker save -o mycentos:1.0.rar mycentos:1.0
[root@iZ2vc772r7963jijk5rxjlZ docker]# ls
Dockerfile  mycentos:1.0.rar  redis

⑤镜像解压

如果被人要用这个镜像,只需要把这个压缩文件解压即可:

[root@iZ2vc772r7963jijk5rxjlZ docker]# docker rm mycentos
mycentos
[root@iZ2vc772r7963jijk5rxjlZ docker]# docker rmi mycentos:1.0
Untagged: mycentos:1.0
Deleted: sha256:b6d4816abbd9ee0e8d5decfe044bc0771abd5d4f3ffa7136bbc132abf9be7649
[root@iZ2vc772r7963jijk5rxjlZ docker]# docker load -i mycentos.1.0.rar

⑥查看镜像历史变更

[root@localhost docker]# docker history mycentos:1.0
标签: docker 运维 容器

本文转载自: https://blog.csdn.net/m0_71239320/article/details/128970608
版权归原作者 Java王小怪 所有, 如有侵权,请联系我们删除。

“Docker”的评论:

还没有评论