Docker镜像制作

1.系统环境

[root@docker ~]# uname -a
Linux docker 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@docker ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@docker ~]# hostname -I
10.0.0.71 172.16.1.71 172.17.0.1 

2.制作镜像要求

用centos6.9的基础镜像,添加SSH服务并搭建LAMP架构

3.具体步骤

1)安装vsftpdruanj

[root@docker ~]# yum install -y vsftpd
[root@docker ~]# systemctl enable vsftpd
[root@docker ~]# systemctl start vsftpd

2)将centos6.9基础镜像上传至目录,配置yum仓库

[root@docker tools]# ls /server/tools/
CentOS-6.9-x86_64-bin-DVD1.iso
[root@docker server]# mkdir -p /var/ftp/centos6.9
[root@docker mnt]# mount -o loop /mnt/CentOS-6.9-x86_64-bin-DVD1.iso  /var/ftp/centos6.9/
[root@docker server]# ll /var/ftp/centos6.9/
total 564
-r--r--r-- 2 root root     14 Mar 29  2017 CentOS_BuildTag
dr-xr-xr-x 3 root root   2048 Mar 29  2017 EFI
-r--r--r-- 2 root root    212 Nov 27  2013 EULA
-r--r--r-- 2 root root  18009 Nov 27  2013 GPL
dr-xr-xr-x 3 root root   2048 Mar 29  2017 images
dr-xr-xr-x 2 root root   2048 Mar 29  2017 isolinux
dr-xr-xr-x 2 root root 534528 Mar 29  2017 Packages
-r--r--r-- 2 root root   1359 Mar 28  2017 RELEASE-NOTES-en-US.html
dr-xr-xr-x 2 root root   4096 Mar 29  2017 repodata
-r--r--r-- 2 root root   1706 Nov 27  2013 RPM-GPG-KEY-CentOS-6
-r--r--r-- 2 root root   1730 Nov 27  2013 RPM-GPG-KEY-CentOS-Debug-6
-r--r--r-- 2 root root   1730 Nov 27  2013 RPM-GPG-KEY-CentOS-Security-6
-r--r--r-- 2 root root   1734 Nov 27  2013 RPM-GPG-KEY-CentOS-Testing-6
-r--r--r-- 1 root root   3380 Mar 29  2017 TRANS.TBL

3)拉取一个centos6.9基础镜像,并检查确认

[root@docker server]# docker image pull centos:6.9
6.9: Pulling from library/centos
831490506c47: Pull complete 
Digest: sha256:6fff0a9edc920968351eb357c5b84016000fec6956e6d745f695e5a34f18ecd2
Status: Downloaded newer image for centos:6.9
docker.io/library/centos:6.9
[root@docker server]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       6.9       2199b8eb8390   3 years ago   195MB

4)启动基础镜像容器,并进入容器

[root@docker server]# docker container run -it --name=sshd centos:6.9

5)修改镜像源

[root@1aa0a6183742 /]# mv /etc/yum.repos.d/*.repo /tmp
[root@1aa0a6183742 /]# cat /etc/yum.repos.d/ftp.repo 
[ftp]
name=ftpbase
baseurl=ftp://10.0.0.71/centos6.9
enabled=1
gpgcheck=0

6)安装SSH服务,并设置密码,使得可以从宿主机通过SSH方式连接容器

[root@1aa0a6183742 yum.repos.d]# yum makecache fast && yum install openssh-server -y
[root@1aa0a6183742 yum.repos.d]# /etc/init.d/sshd start
Generating SSH2 RSA host key:                              [  OK  ]
Generating SSH1 RSA host key:                              [  OK  ]
Generating SSH2 DSA host key:                              [  OK  ]
Starting sshd:                                             [  OK  ]
[root@1aa0a6183742 yum.repos.d]# echo "123456"|passwd root --stdin
Changing password for user root.
passwd: all authentication tokens updated successfully.

7)测试能否从宿主机通过SSH方式连接docker容器

[root@1aa0a6183742 yum.repos.d]# ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02  
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1467 errors:0 dropped:0 overruns:0 frame:0
          TX packets:950 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:37019559 (35.3 MiB)  TX bytes:66827 (65.2 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

在docker中通过ifconfig命令可以看到容器的IP是172.17.0.2
在宿主机通过SSH连接方式连接该IP,发现连接成功,具备SSH服务功能。

[root@docker ~]# ssh 172.17.0.2
The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established.
RSA key fingerprint is SHA256:mcqagVe6fFO630kGXH4S8Hv7uDn4HIpuosjZIZG2LUs.
RSA key fingerprint is MD5:bf:3b:27:36:4c:2b:c5:22:ab:ab:ba:6f:04:eb:a1:3a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.2' (RSA) to the list of known hosts.
root@172.17.0.2's password: 
[root@1aa0a6183742 ~]# 

8)完成并提交含有SSH服务的镜像

[root@docker server]# docker commit sshd lamp:v1
sha256:56814c78957e9a78b206815ac1518344fbbc1d20e6ef56b72227be3c272afd48
[root@docker server]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
lamp         v1        56814c78957e   7 seconds ago   331MB
centos       6.9       2199b8eb8390   3 years ago     195MB

9)做好搭建LAMP的准备工作,并制作使用mysql和http服务的数据卷,启动lamp_v1容器,进入容器,优化yum源,安装软件和初始化操作

[root@docker server]# \rm -rf /opt/*
[root@docker server]# mkdir -p /opt/vol/mysql /opt/vol/html
[root@docker server]# docker container run -it --name=lamp_v1 -v /opt/vol/mysql:/var/lib/mysql -v /opt/vol/html:/var/www/html lamp:v1
[root@6f65bf0ab838 /]# mv /etc/yum.repos.d/*.repo /tmp
[root@6f65bf0ab838 /]# cat >/etc/yum.repos.d/ftp.repo <<EOF 
> [ftp]
> name=ftpbase
> baseurl=ftp://10.0.0.71/centos6.9
> enabled=1
> gpgcheck=0
> EOF
[root@6f65bf0ab838 /]# yum makecache fast && yum install openssh-server htppd mysql mysql-server php php-mysql -y
[root@6f65bf0ab838 /]# /etc/init.d/sshd start
Starting sshd:                                             [  OK  ]
[root@6f65bf0ab838 /]# echo "123456" | passwd  root --stdin
Changing password for user root.
passwd: all authentication tokens updated successfully.
[root@c3fd597ec194 mysql]# /etc/init.d/mysqld start
mysql> grant all on *.* to root@'%' identified by '123';
mysql> grant all on *.* to discuz@'%' identified by '123';
mysql> create database discuz charset utf8;
[root@6f65bf0ab838 /]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2 for ServerName                                                          [  OK  ]

10)上传discuz软件包并解压

[root@6f65bf0ab838 tmp]# yum install -y lrzsz unzip
[root@6f65bf0ab838 html]# chmod -R 777 *
[root@6f65bf0ab838 html]# ll
total 68
-rwxrwxrwx  1 root root 2848 May 20 13:20 admin.php
drwxrwxrwx  9 root root  135 May 20 13:20 api
-rwxrwxrwx  1 root root  727 May 20 13:20 api.php
drwxrwxrwx  2 root root   23 May 20 13:20 archiver
drwxrwxrwx  2 root root   90 May 20 13:20 config
-rwxrwxrwx  1 root root 1040 May 20 13:20 connect.php
-rwxrwxrwx  1 root root  106 May 20 13:20 crossdomain.xml
drwxrwxrwx 12 root root  178 May 20 13:20 data
-rwxrwxrwx  1 root root 5558 May 18 15:15 favicon.ico
-rwxrwxrwx  1 root root 2245 May 20 13:20 forum.php
-rwxrwxrwx  1 root root  821 May 20 13:20 group.php
-rwxrwxrwx  1 root root 1280 May 20 13:20 home.php
-rwxrwxrwx  1 root root 6655 May 20 13:20 index.php
drwxrwxrwx  5 root root   64 May 20 13:20 install
drwxrwxrwx  2 root root   23 May 20 13:20 m
-rwxrwxrwx  1 root root  998 May 20 13:20 member.php
-rwxrwxrwx  1 root root 2371 May 20 13:20 misc.php
-rwxrwxrwx  1 root root 1788 May 20 13:20 plugin.php
-rwxrwxrwx  1 root root  977 May 20 13:20 portal.php
-rwxrwxrwx  1 root root  615 May 20 13:20 robots.txt
-rwxrwxrwx  1 root root 1274 May 20 13:20 search.php
drwxrwxrwx 10 root root  168 May 20 13:20 source
drwxrwxrwx  7 root root   86 May 20 13:20 static
drwxrwxrwx  3 root root   38 May 20 13:20 template
drwxrwxrwx  7 root root  106 May 20 13:20 uc_client
drwxrwxrwx 13 root root  241 May 20 13:20 uc_server

11)制作lamp_v2镜像

[root@docker server]# docker commit lamp_v1 lamp:v2
sha256:23876eff12efe55738da8d4721e2ae046be66b6a1c119193ab2a4a913a09b77b

12)创建启动脚本

[root@docker html]# cd /opt/vol/html
[root@docker html]# cat init.sh 
#!/bin/bash
/etc/init.d/mysqld start 
/etc/init.d/httpd start
/usr/sbin/sshd -D
[root@docker html]# chmod 777 init.sh

13)启动容器,映射端口,挂载数据卷,自启动多服务

[root@docker html]# docker container run -d --name=lamp_v2 -v /opt/vol/mysql:/var/lib/mysql -v /opt/vol/html:/var/www/html  -p 22222:22 -p 8080:80 -p 33060:3306 lamp:v2 /var/www/html/init.sh
[root@docker html]# docker container ls -a
CONTAINER ID   IMAGE        COMMAND                  CREATED             STATUS                         PORTS                                                                                                                         NAMES
1848d3f8dc62   lamp:v2      "/var/www/html/init.…"   42 seconds ago      Up 39 seconds                  0.0.0.0:22222->22/tcp, :::22222->22/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp, 0.0.0.0:33060->3306/tcp, :::33060->3306/tcp   lamp_v2

14)测试

ssh新建会话,端口22222连接
http网站10.0.0.71:8080
mysql 的sqlyog33060

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值