Docker搭建一个web环境
环境Centos(虚机)CentOS Linux release 7.2.1511 (Core)
安装docker
yum install docker
启动docker,并设置为开机启动
方式一(老式)
service docker start chkconfig docker on
cent7支持新的systemd
systemctl start docker.service systemctl enable docker.service
安装完成后查看
[root@localhost ~]# docker -v Docker version 1.10.3, build 9419b24-unsupported
搜索相关镜像
[root@localhost ~]# docker search centos INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/centos The official build of CentOS. 2410 [OK] docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 79 [OK] docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8... 26 [OK] docker.io docker.io/nimmis/java-centos This is docker images of CentOS 7 with dif... 13 [OK] docker.io docker.io/million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 12 [OK] docker.io docker.io/consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 10 [OK] docker.io docker.io/torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK] docker.io docker.io/nickistre/centos-lamp LAMP on centos setup 4 [OK] docker.io docker.io/centos/mariadb55-centos7 3 [OK] docker.io docker.io/nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 3 [OK] docker.io docker.io/consol/sakuli-centos-xfce Sakuli end-2-end testing and monitoring co... 2 [OK] docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK] docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 1 [OK] docker.io docker.io/timhughes/centos Centos with systemd installed and running 1 [OK] docker.io docker.io/dmglab/centos CentOS with some extras - This is for the ... 0 [OK] docker.io docker.io/ericuni/centos centos dev 0 [OK] docker.io docker.io/grayzone/centos auto build for centos. 0 [OK] docker.io docker.io/grossws/centos CentOS 6 and 7 base images with gosu and l... 0 [OK] docker.io docker.io/harisekhon/centos-scala Scala + CentOS (OpenJDK tags 2.10-jre7 - 2... 0 [OK] docker.io docker.io/januswel/centos yum update-ed CentOS image 0 [OK] docker.io docker.io/jsmigel/centos-epel Docker base image of CentOS w/ EPEL installed 0 [OK] docker.io docker.io/kz8s/centos Official CentOS plus epel-release 0 [OK] docker.io docker.io/repositoryjp/centos Docker Image for CentOS. 0 [OK] docker.io docker.io/sgfinans/docker-centos CentOS with a running sshd and Docker 0 [OK] docker.io docker.io/ustclug/centos USTC centos 0 [OK]
拉取一个镜像
[root@localhost ~]# docker pull nickistre/centos-lamp Using default tag: latest Trying to pull repository docker.io/nickistre/centos-lamp ... Pulling repository docker.io/nickistre/centos-lamp d6d9ec2e67f1: Pull complete 3690474eb5b4: Pull complete d65a92bab695: Pull complete 4a98883d437e: Pull complete b10e99758b95: Pull complete 5f5b0aafb036: Pull complete 205a003e4718: Pull complete 4fd77ea1aba2: Pull complete 5c0d565c6fa2: Pull complete 306cfb3b53df: Pull complete 724c96ad712b: Pull complete 4121ea2cc401: Pull complete 341fee58aa0f: Pull complete 3f97351ce7a9: Pull complete b03bf1629977: Pull complete ee207207752f: Pull complete 1aa4911f40c7: Pull complete afa600610768: Pull complete f818b5019926: Pull complete Status: Downloaded newer image for docker.io/nickistre/centos-lamp:latest docker.io/nickistre/centos-lamp: this image was pulled from a legacy registry. Important: This registry version will not be supported in future versions of docker. [root@localhost ~]#
查看镜像
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/centos latest 05188b417f30 6 days ago 196.8 MB docker.io/ubuntu latest 0f192147631d 8 days ago 132.7 MB docker.io/nickistre/centos-lamp latest 71686fd39d40 7 weeks ago 538.4 MB [root@localhost ~]#
运行容器,进入该容器的shell界面
[root@localhost ~]# docker run -i -t docker.io/centos /bin/bash [root@6054cbac7144 /]# ls anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
因为是最小化安装的所以ifconfig command not found
[root@6054cbac7144 /]# ifconfig bash: ifconfig: command not found
下面安装net-tools
[root@6054cbac7144 /]# ifconfig bash: ifconfig: command not found [root@6054cbac7144 /]# yum install net-tools Loaded plugins: fastestmirror, ovl base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/4): extras/7/x86_64/primary_db | 149 kB 00:00:00 (2/4): base/7/x86_64/group_gz | 155 kB 00:00:06 (3/4): base/7/x86_64/primary_db | 5.3 MB 00:00:10 (4/4): updates/7/x86_64/primary_db | 5.7 MB 00:00:11 Determining fastest mirrors * base: ftp.sjtu.edu.cn * extras: ftp.sjtu.edu.cn * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package net-tools.x86_64 0:2.0-0.17.20131004git.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================================================================ Package Arch Version Repository Size ================================================================================================================================================================================ Installing: net-tools x86_64 2.0-0.17.20131004git.el7 base 304 k Transaction Summary ================================================================================================================================================================================ Install 1 Package Total download size: 304 k Installed size: 917 k Is this ok [y/d/N]: y Downloading packages: warning: /var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.17.20131004git.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Public key for net-tools-2.0-0.17.20131004git.el7.x86_64.rpm is not installed net-tools-2.0-0.17.20131004git.el7.x86_64.rpm | 304 kB 00:00:00 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-2.1511.el7.centos.2.10.x86_64 (@CentOS) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Is this ok [y/N]: ^[[B^[[B^[[A^[[Ay Is this ok [y/N]: y Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : net-tools-2.0-0.17.20131004git.el7.x86_64 1/1 Verifying : net-tools-2.0-0.17.20131004git.el7.x86_64 1/1 Installed: net-tools.x86_64 0:2.0-0.17.20131004git.el7 Complete! [root@6054cbac7144 /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0 inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20<link> ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 3830 bytes 12364154 (11.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3315 bytes 182664 (178.3 KiB) 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 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (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@6054cbac7144 /]#
下面保存这个镜像为新的镜像
先查看下刚刚运行的容器(安装net-tools的)的ID,在使用该ID生成新的镜像。
[root@localhost ~]# docker ps -la CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6054cbac7144 docker.io/centos "/bin/bash" 4 minutes ago Exited (0) 26 seconds ago loving_keller
我们保存为一个新的镜像可以看到保存后的id
[root@localhost ~]# docker commit 6054cbac7144 pooky/centos_withnettools:v1 sha256:5d50dc715c0af87b01dd9e50bf8088b96af2883d434578084211f6c8a1163860 [root@localhost ~]#
查看镜像
[root@localhost ~]# docker commit 6054cbac7144 pooky/centos_withnettools:v1 sha256:5d50dc715c0af87b01dd9e50bf8088b96af2883d434578084211f6c8a1163860 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE pooky/centos_withnettools v1 5d50dc715c0a 53 seconds ago 284.6 MB pooky/docker v1 6252aab1056e 11 minutes ago 284.6 MB docker.io/centos latest 05188b417f30 6 days ago 196.8 MB docker.io/ubuntu latest 0f192147631d 8 days ago 132.7 MB docker.io/nickistre/centos-lamp latest 71686fd39d40 7 weeks ago 538.4 MB [root@localhost ~]#
我们启动新的镜像 新的镜像有已经变动的,配置。
[root@localhost ~]# docker run -i -t pooky/centos_withnettools:v1 /bin/bash [root@81fc2eb3849d /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0 inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20<link> ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 6 bytes 508 (508.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6 bytes 508 (508.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 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (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@81fc2eb3849d /]#
下面制作一个安装了lnmp的镜像,具体的可以网上搜索对应的安装教程
[root@localhost ~]# docker run -d -p 80:80 pooky/centos_httpd:v1 /sbin/init
4efd73d0a2ad834b4511ed6a9237d94913e497bdc8dc40572962b20fd73e8d7d
[root@localhost ~]#
然后我从本机访问虚拟机
ok 关闭docker kill
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4efd73d0a2ad pooky/centos_httpd:v1 "/sbin/init" 5 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp reverent_knuth [root@localhost ~]# docker kill 4efd73d0a2ad 4efd73d0a2ad [root@localhost ~]# docker -ps flag needs an argument: -p See '/usr/bin/docker-current --help'. flag provided but not defined: -ps See '/usr/bin/docker-current --help'. [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]#
安装软件的以此类推,我这里直接用线上的image(cent7安装httpd 有bug 不细说了 都是泪)我们用下面这个镜像,按照上面的拉下来
docker.io/nickistre/centos-lamp
[root@localhost ~]# docker run -i -t -p 80:80 -v /var/www/html:/var/www/html docker.io/nickistre/centos-lamp:latest /bin/bash [root@38eecd594b74 /]# service httpd start Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2 for ServerName [ OK ] [root@38eecd594b74 /]#
映射到本地的磁盘
我们来把这个镜像改动下,保存成我们自己的(具体参考前面的步骤)
主要是加入开机启动httpd,编辑一下httpd.conf 文件的ServerName
然后保存镜像
[root@localhost ~]# docker run -d -p 80:80 -v /var/www/html:/var/www/html pooky/centos_lamp:v1 /sbin/init 16a8cc5ac86af508661bd3eed1c6b874a364d0fd79b0566b04d545503c956555 [root@localhost ~]#
这样这个docker在后台就可以安静的跑起来了
暂时告一段落,回头再弄,
ps 一万个零还是零,每次学习都要阶段性的,过目就忘没有什么用