1. 安装CentOS 7.x

  1. 略。

2. 安装docker

详见: How to Install Docker on CentOS 6.x

3. 基础docker命令

  1. 获取centos6 docker镜像
  2. [root@docker ~]# docker pull centos6
  3. Using default tag: latest
  4. latest: Pulling from library/centos:centos6
  5. 43db9dbdcb30: Downloading 41.14 MB/49.33 MB
  6. 43db9dbdcb30: Pull complete
  7. 2dc64e8f8d4f: Pull complete
  8. 670a583e1b50: Pull complete
  9. 183b0bfcd10e: Pull complete
  10. Digest: sha256:c6674c44c6439673bf56536c1a15916639c47ea04c3d6296c5df938add67b54b
  11. Status: Downloaded newer image for centso:centos6
  12. 查看当前服务器上的docker镜像
  13. [root@docker ~]# docker images
  14. REPOSITORY TAG IMAGE ID CREATED SIZE
  15. centos6-visionet latest aeed37612ecb 6 hours ago 1.916 GB
  16. centos centos6 a3c09d36ab4a 2 days ago 194.6 MB
  17. registry latest c6c14b3960bd 3 days ago 33.28 MB
  18. 查找docker镜像
  19. [root@docker ~]# docker search centos:centos6
  20. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  21. imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 10 [OK]
  22. lemonbar/centos6-ssh Installed openssh-server on centos6, defau... 7 [OK]
  23. sergeyzh/centos6-nginx 2 [OK]
  24. guyton/centos6 From official centos6 container with full ... 2 [OK]
  25. paijp/centos6-apache-php-sqlite2 php5.3.3 with sqlite2 and apache on CentOS... 1 [OK]
  26. edrans/centos6 1 [OK]
  27. mohri1219/centos6.7-ruby2.2.2-mysql5.6 centos6.7-ruby2.2.2-mysql5.6 1 [OK]
  28. ...
  29. ...
  30. 删除 centos6 docker镜像
  31. [root@docker ~]# docker rmi IMAGES_NAME
  32. [root@docker ~]# docker rmi centos:centos6
  33. # 报错无法删除
  34. Error response from daemon: conflict: unable to remove repository reference "centos" (must force) - container 705a30cbc002 is using its referenced image 42118e3df429
  35. # 这是因为之间的运行的进程没有清楚导致该images正在使用中,使用一下命令查看占有的进程
  36. [root@docker ~]# docker ps -a 查看所有的docker进程
  37. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  38. 705a30cbc002 centos:centos6 "/bin/bash" 6 minutes ago Exited (0) 6 minutes ago centos6
  39. [root@docker ~]# docker rm 705a30cbc002
  40. 705a30cbc002
  41. [root@docker ~]# docker rmi centos:centos6
  42. Untagged: centos:centos6
  43. Deleted: sha256:42118e3df429f09ca581a9deb3df274601930e428e452f7e4e9f1833c56a100a
  44. Deleted: sha256:d72d0199021776e77ac14a764a3decd156e9f6f18b37e25b867b0f1ca42fb3d9
  45. Deleted: sha256:fb178c6cea1453d089ce033b96eba0efdb42274138e042517ea2d5ea7bb51665
  46. Deleted: sha256:8945af30572845a904adce2aeaf73402c842d86e99e8f10688b25cb27834110b
  47. Deleted: sha256:ea9f151abb7e06353e73172dad421235611d4f6d0560ec95db26e0dc240642c1

4. 运行一个docker镜像

进入交互式程序

  1. [root@docker ~]# docker run -it centos:centos6 /bin/bash
  2. [root@c1d190f95562 /]# cat /etc/redhat-release
  3. CentOS release 6.8 (Final)
  4. # Install openssh-server and httpd
  5. root@d8c122dbe09d:/# yum install -y openssh-sever httpd
  6. root@d8c122dbe09d:/# exit
  7. # 注意当你执行exit,从容器之中退出时,容器也会随着关闭,这时如果还继续执行上条命令
  8. # 你会发现之前所有的操作都被还原,因为相对于docker,又是新开了一个进程
  9. # 在exit退出之后,使用一下命令查看上次提交,然后commit修改,便可保存之前的修改
  10. [root@docker ~]# docker ps -l
  11. [root@docker ~]# docker commit c1d190f95562 centos:centos6-httpd
  12. sha256:77eb12e36c7bbe9ef874555b1873019022b5258740aa731627e2cd6fa749c325
  13. [root@docker ~]# docker images
  14. REPOSITORY TAG IMAGE ID CREATED SIZE
  15. centos centos6-httpd 77eb12e36c7b 3 seconds ago 290.3 MB
  16. centos6-visionet latest aeed37612ecb 6 hours ago 1.916 GB
  17. centos centos6 a3c09d36ab4a 2 days ago 194.6 MB
  18. registry latest c6c14b3960bd 3 days ago 33.28 MB

5. 编辑Dockerfile

  1. # dockfile的第一行一定是FROM,指定了父镜像
  2. FROM centos:centos6
  3. # RUN 指的是运行的命令
  4. RUN yum install -y epel-release
  5. RUN yum install -y bash-completion
  6. RUN yum install -y openssh-server openssh-client sudo
  7. RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
  8. RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  9. # 增加一个用户,并赋予sudo权限
  10. RUN useradd visionet
  11. RUN echo "visionet:visionet" | chpasswd
  12. RUN echo "visionet ALL=(ALL) ALL" >> /etc/sudoers
  13. 注意,如果没有执行一下两个命令,sshd是无法远程登陆的
  14. RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
  15. RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
  16. EXPOSE 22 22端口暴露出来,方便ssh远程连接
  17. RUN mkdir /var/run/sshd
  18. EXPOSE 22
  19. CMD 为系统内部执行命令
  20. CMD ["/usr/sbin/sshd", "-D"]

6. 生成自定义docker镜像

  1. # build 编译镜像
  2. [root@docker ~]# docker build -f dockerfile3 -t centos:centos6-ssh .
  3. Sending build context to Docker daemon 44.54 kB
  4. Step 1 : FROM centos:centos6
  5. ---> a3c09d36ab4a
  6. Step 2 : RUN yum install -y epel-release
  7. ---> Using cache
  8. ---> 04d81f280a63
  9. Step 3 : RUN yum install -y bash-completion
  10. ---> Using cache
  11. ---> 5ca26894dc9d
  12. Step 4 : RUN yum install -y openssh-server openssh-client sudo
  13. ---> Using cache
  14. ---> 42278450311e
  15. Step 5 : RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
  16. ---> Using cache
  17. ---> db35bef562a2
  18. Step 6 : RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  19. ---> Using cache
  20. ---> dc9f2b82e198
  21. Step 7 : RUN useradd visionet
  22. ---> Using cache
  23. ---> 06c574a51755
  24. Step 8 : RUN echo "visionet:visionet" | chpasswd
  25. ---> Using cache
  26. ---> 03412f053d04
  27. Step 9 : RUN echo "visionet ALL=(ALL) ALL" >> /etc/sudoers
  28. ---> Using cache
  29. ---> bf8476812a62
  30. Step 10 : RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
  31. ---> Using cache
  32. ---> 2314cce75171
  33. Step 11 : RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
  34. ---> Using cache
  35. ---> 99304cd9ac15
  36. Step 12 : RUN mkdir /var/run/sshd
  37. ---> Using cache
  38. ---> f87283751553
  39. Step 13 : EXPOSE 22
  40. ---> Using cache
  41. ---> 56c41202dcb9
  42. Step 14 : CMD /usr/sbin/sshd -D
  43. ---> Using cache
  44. ---> d6dccb483fa6
  45. Successfully built d6dccb483fa6
  46. [root@docker ~]# docker images
  47. REPOSITORY TAG IMAGE ID CREATED SIZE
  48. centos centos6-ssh d6dccb483fa6 2 minutes ago 363 MB
  49. centos centos6-httpd 77eb12e36c7b 6 minutes ago 290.3 MB
  50. centos6-visionet latest aeed37612ecb 6 hours ago 1.916 GB
  51. centos centos6 a3c09d36ab4a 2 days ago 194.6 MB
  52. registry latest c6c14b3960bd 3 days ago 33.28 MB
  53. # 后台运行docker容器,并指定nat端口转发
  54. [root@docker ~]# docker run -d -p 2222:22 -P --name=sshd centos:centos6-ssh
  55. b4211f7a304d9e34b72b510230be2c7a76b276886b488f08f3e12896a4d3c172

7. ssh登陆到docker容器内,部署项目环境

  1. # inspect 查看docker 容器IP地址
  2. [root@docker ~]# docker inspect sshd | grep IPAddress
  3. [root@docker ~]# ssh visionet@172.17.0.2
  4. visionet@172.17.0.2's password:
  5. [visionet@b4211f7a304d ~]$
  6. # Install mysql
  7. # Install tomcat server
  8. [visionet@b4211f7a304d ~]$ exit
  9. [root@docker ~]# docker stop sshd
  10. sshd
  11. [root@docker ~]# docker ps -l
  12. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  13. b4211f7a304d centos:centos6-ssh "/usr/sbin/sshd -D" 6 minutes ago Exited (255) 3 seconds ago sshd
  14. [root@docker ~]# docker commit b4211f7a304d centos:centos6-tomcat
  15. sha256:b4f114d5484822d9a3ddf1100b76b460985ba5e5d31e936570e8d7dbd8df282c
  16. [root@docker ~]# docker images
  17. REPOSITORY TAG IMAGE ID CREATED SIZE
  18. centos centos6-tomcat b4f114d54848 4 seconds ago 363.1 MB
  19. centos centos6-httpd 77eb12e36c7b 15 minutes ago 290.3 MB
  20. centos6-visionet latest aeed37612ecb 7 hours ago 1.916 GB
  21. centos centos6-ssh d6dccb483fa6 9 hours ago 363 MB
  22. centos centos6 a3c09d36ab4a 2 days ago 194.6 MB
  23. registry latest c6c14b3960bd 3 days ago 33.28 MB

8. 推送centos:centos6-tomcat到私有docker仓库