Fork me on GitHub

Docker容器——准备Java Web环境(Tomcat MySQL Nginx...)

前言

Docker是一个开源的应用引擎。能够让开发者打包他们的应用及其依赖到一个轻量级的、可移植的容器当中,然后发布到任何的linux及其上,实现虚拟化。

之前发布项目在服务器上,当服务器需要更换或者部署管理多个项目时候,应用所依赖的环境安装配置如何保持一致、无缝切换将成了难题,让人头疼。下面就来说说使用Docker能够让我们做的一些事情。

  • Web应用的自动化打包和发布
  • 自动化测试和持续集成、发布
  • 在服务型环境中部署和调整数据库或者其他的后台应用
  • 搭建自己的Pass环境

接下来我们直接进入主题,安装环境和部署项目。

安装Docker

前提条件

在此演示Centos上安装Docker,由于Docker支持以下版本:

  • Centos 7 (64)
  • Centos 6.5 (64) 或者更高版本

Docker运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。

Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。

在此,可以使用uname -r检查当前内核版本。我使用的是Centos 7

1
2
[root@VM_3_226_centos ~]# uname -r
3.10.0-693.el7.x86_64

安装Docker

Docker软件包和依赖包都已经在源内,我们直接安装就行了。

1
[root@VM_3_226_centos ~]# yum -y install docker-io

启动Docker

上述安装完成后,后台启动Docker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@VM_3_226_centos ~]# systemctl start docker
[root@VM_3_226_centos ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2018-06-10 21:11:18 CST; 5s ago
Docs: http://docs.docker.com
Main PID: 11708 (dockerd-current)
CGroup: /system.slice/docker.service
├─11708 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userlan...
└─11713 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/lib...

Jun 10 21:11:17 VM_3_226_centos dockerd-current[11708]: time="2018-06-10T21:11:17.537192608+08:00" level=warning msg="Docker could not enable SELinux on the host system"
Jun 10 21:11:17 VM_3_226_centos dockerd-current[11708]: time="2018-06-10T21:11:17.744291737+08:00" level=info msg="Graph migration to content-addressability took 0.00 seconds"
Jun 10 21:11:17 VM_3_226_centos dockerd-current[11708]: time="2018-06-10T21:11:17.744778618+08:00" level=info msg="Loading containers: start."
Jun 10 21:11:17 VM_3_226_centos dockerd-current[11708]: time="2018-06-10T21:11:17.802871606+08:00" level=info msg="Firewalld running: false"
Jun 10 21:11:18 VM_3_226_centos dockerd-current[11708]: time="2018-06-10T21:11:18.056871151+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17...IP address"
Jun 10 21:11:18 VM_3_226_centos dockerd-current[11708]: time="2018-06-10T21:11:18.157283981+08:00" level=info msg="Loading containers: done."
Jun 10 21:11:18 VM_3_226_centos dockerd-current[11708]: time="2018-06-10T21:11:18.163032617+08:00" level=info msg="Daemon has completed initialization"
Jun 10 21:11:18 VM_3_226_centos dockerd-current[11708]: time="2018-06-10T21:11:18.163049321+08:00" level=info msg="Docker daemon" commit="94f4240/1.13.1" graphdriver=overlay2 version=1.13.1
Jun 10 21:11:18 VM_3_226_centos systemd[1]: Started Docker Application Container Engine.
Jun 10 21:11:18 VM_3_226_centos dockerd-current[11708]: time="2018-06-10T21:11:18.181073611+08:00" level=info msg="API listen on /var/run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.
[root@VM_3_226_centos ~]#

上述 systemctl start docker 启动docker,然后通过systemctl status docker可以看到docker已经在后台运行。

测试运行hello-world

由于本地没有hello-world这个镜像,所以会到docker默认官方镜像当猴子那个下载一个hello-world的镜像,并且在容器当中运行。如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@VM_3_226_centos ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
9bb5a5d4561a: Pull complete
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for docker.io/hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

同时由于国内网络问题,后续拉取各种docker镜像速度都非常慢,再次我将使用网易的镜像https://c.163.com/hub 网易蜂巢

1
2
3
4
5
6
7
8
[root@VM_3_226_centos ~]# cat /etc/docker/daemon.json 
{}
[root@VM_3_226_centos ~]# vim /etc/docker/daemon.json
[root@VM_3_226_centos ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}
[root@VM_3_226_centos ~]#

安装环境

安装Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@VM_3_226_centos ~]# docker pull hub.c.163.com/library/nginx:latest
Trying to pull repository hub.c.163.com/library/nginx ...
latest: Pulling from hub.c.163.com/library/nginx
5de4b4d551f8: Pull complete
d4b36a5e9443: Pull complete
0af1f0713557: Pull complete
Digest: sha256:f84932f738583e0169f94af9b2d5201be2dbacc1578de73b09a6dfaaa07801d6
Status: Downloaded newer image for hub.c.163.com/library/nginx:latest
[root@VM_3_226_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest e38bc07ac18e 8 weeks ago 1.85 kB
hub.c.163.com/library/nginx latest 46102226f2fd 13 months ago 109 MB
[root@VM_3_226_centos ~]#
1
[root@VM_3_226_centos www]# docker run -p 80:80 --name my-nginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -d hub.c.163.com/library/nginx

联系

聪聪的独立博客 ,一个喜欢技术,喜欢钻研的95后。如果你看到这篇文章,千里之外,我在等你联系。

坚持原创技术分享,您的支持将鼓励我继续创作!