您的位置:首页 > 运维架构 > Docker

Docker 在Windows2016 /Ubuntu18.04中初体验基本命令,tomcat

2018-05-01 12:14 826 查看

前言

Docker出生时就知道,开始感觉和虚拟机技术差不多,也参加过一、二次线下的研讨会,一直想在WINDOWS中使用,安装过1,2次都没有成功。

最近2周才在WINDOWS2016中看了一下,也费尽九牛二虎之力才整成功地从阿里云上PULL了nanoserver.

最近2天遇到Ubuntu18.04发布,成功在docker中运行了tomcat 。

Docker 在Windows 2016中

安装docker



PULL 镜像

PS C:\Users\Administrator> docker pull microsoft/nanoserver
Using default tag: latest
latest: Pulling from microsoft/nanoserver
bce2fbc256ea: Pulling fs layer
83eec61707e8: Downloading
latest: Pulling from microsoft/nanoserver
bce2fbc256ea: Pull complete
83eec61707e8: Pull complete
Digest: sha256:59558bd57c0d14a4df5b827a676fb061abacefebfa2089038f018cf9eea17ecb
Status: Downloaded newer image for microsoft/nanoserver:latest

PS C:\Users\Administrator> docker image ls
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
microsoft/nanoserver   latest              353592ac9faa        2 weeks ago         1.11GB

C:\>docker ps

CONTAINER ID        IMAGE                  COMMAND                    CREATED             STATUS              PORTS               NAMES

56aba97e04d7        microsoft/nanoserver   "c:\\windows\\system..."   12 minutes ago      Up 12 minutes                           quirky_archimedes

eb660699e41a        microsoft/nanoserver   "c:\\windows\\system..."   29 minutes ago      Up 29 minutes                           nervous_lamarr


交互方式运行一个容器



Docker 在Ubuntu 18.04中

安装docker

➜  test sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

➜  test sudo apt-get install docker
Reading package lists... Done
Building dependency tree
Reading state information... Done
docker is already the newest version (1.5-1build1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

➜  test sudo apt-get install docker.io
Reading package lists... Done
Building dependency tree
Reading state information... Done
docker.io is already the newest version (17.12.1-0ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.


docker 版本

➜  test sudo docker version
Client:
Version:       17.12.1-ce
API version:   1.35
Go version:    go1.10.1
Git commit:    7390fc6
Built: Wed Apr 18 01:23:11 2018
OS/Arch:       linux/amd64

Server:
Engine:
Version:      17.12.1-ce
API version:  1.35 (minimum version 1.12)
Go version:   go1.10.1
Git commit:   7390fc6
Built:        Wed Feb 28 17:46:05 2018
OS/Arch:      linux/amd64
Experimental: false


配置阿里云docker镜像加速器

使用阿里云加速,下载速度快,有1MB多:

https://dev.aliyun.com/search.html

使用加速器将会提升您在国内获取Docker官方镜像的速度!

您的专属加速器地址 https://?????.mirror.aliyuncs.com (?????换为各人的专属加速器地址,后同)

如何配置镜像加速器

针对Docker客户端版本大于1.10.0的用户
您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://????????.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

pull两个镜像:hello-world, tomcat

latest: Pulling from library/hello-world
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Image is up to date for hello-world:latest
➜  test
➜  test
➜  test
➜  test sudo docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
Digest: sha256:a5cc095285efc4becda11829fbf63663a4216d9344adcf42e0d3d9c95ed6079f
Status: Image is up to date for tomcat:latest


PULL了17个镜像image,速度非常快

➜  apache@docker sudo docker images
REPOSITORY                                          TAG                 IMAGE ID            CREATED             SIZE
mysql                                               5.6                 e09f6de95634        22 hours ago        256MB
ubuntu                                              16.04               0b1edfbffd27        3 days ago          113MB
ubuntu                                              14.04               8cef1fa16c77        3 days ago          223MB
ubuntu                                              latest              452a96d81c30        3 days ago          79.6MB
ubuntu                                              17.10               e4422b8da209        3 days ago          99.2MB
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4   latest              aa2b295eb6cc        4 days ago          513MB
php                                                 latest              81fb5e582f8e        4 days ago          357MB
mysql                                               latest              8d65ec712c69        10 days ago         445MB
tomcat                                              latest              33e02377a00f        2 weeks ago         554MB
redmine                                             latest              887149aec1cd        2 weeks ago         663MB
hello-world                                         latest              e38bc07ac18e        2 weeks ago         1.85kB
groovy                                              latest              736b1ab40dcb        4 weeks ago         569MB
nouchka/sqlite3                                     latest              dd652cec76b3        4 weeks ago         125MB
httpd                                               latest              805130e51ae9        5 weeks ago         178MB
flink                                               latest              c8014c2a597d        5 weeks ago         789MB
antage/apache2-php5                                 latest              9f6bb6d24bd3        3 months ago        648MB
ubuntu                                              12.04               5b117edd0b76        12 months ago       104MB
➜  apache@docker


运行了12个容器container

ubuntu:lastest 一个镜像运行了6个容器, tomcat创建了2个容器

➜  test sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
aa7cc0c99e4e        tomcat              "catalina.sh run"        56 seconds ago      Up 51 seconds       0.0.0.0:18081->8080/tcp   tomcat2
e42d78f792da        tomcat              "catalina.sh run"        About an hour ago   Up About an hour    0.0.0.0:18080->8080/tcp   tomcat
9f6c4e6adf80        mysql:5.6           "docker-entrypoint.s…"   About an hour ago   Up About an hour    0.0.0.0:13306->3306/tcp   mysql2
3a83cdf9783f        mysql:5.6           "docker-entrypoint.s…"   About an hour ago   Up About an hour    0.0.0.0:3306->3306/tcp    mymysql
cbcafe1c543e        ubuntu:12.04        "bin/sh -c 'while tr…"   2 hours ago         Up 2 hours                                    u12
617fc0385c38        ubuntu:14.04        "bin/sh -c 'while tr…"   2 hours ago         Up 2 hours                                    focused_wiles
93d03ad052c8        ubuntu              "bin/bash -c ' while…"   3 hours ago         Up 3 hours                                    u3
ff85ac36d387        ubuntu              "bin/bash -c ' while…"   3 hours ago         Up 3 hours                                    u2
e93e21a0926d        ubuntu              "bin/bash -c ' while…"   3 hours ago         Up 3 hours                                    u1
fad177c1aadf        ubuntu              "/bin/bash -c 'while…"   3 hours ago         Up 3 hours                                    sad_engelbart
edc0a745ba40        ubuntu              "/bin/bash"              4 hours ago         Up 4 hours                                    vigorous_snyder
7d6d1f9886e3        ubuntu              "/bin/bash"              4 hours ago         Up 4 hours                                    modest_babbage


12个容器CPU,内存占用率

和虚拟机相比最大的优势,内存占用显著降低,基本上少了操作系统的开销

➜  test sudo docker stats

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
aa7cc0c99e4e        tomcat2             0.18%               99.4MiB / 1.439GiB    6.74%               16.9kB / 105kB      98.3MB / 459kB      41
e42d78f792da        tomcat              0.20%               46.7MiB / 1.439GiB    3.17%               8.82kB / 7.15kB     117MB / 0B          41
9f6c4e6adf80        mysql2              0.10%               103.8MiB / 1.439GiB   7.04%               4.96kB / 0B         142MB / 277MB       21
3a83cdf9783f        mymysql             0.07%               20.16MiB / 1.439GiB   1.37%               4.96kB / 0B         156MB / 410MB       21
cbcafe1c543e        u12                 0.00%               2.305MiB / 1.439GiB   0.16%               5.32kB / 0B         36.4MB / 1.71MB     3
617fc0385c38        focused_wiles       0.00%               1.562MiB / 1.439GiB   0.11%               5.47kB / 0B         161MB / 11.5MB      2
93d03ad052c8        u3                  0.00%               1.184MiB / 1.439GiB   0.08%               5.58kB / 0B         57.3kB / 3.33MB     2
ff85ac36d387        u2                  0.00%               1.262MiB / 1.439GiB   0.09%               5.58kB / 0B         475kB / 2.89MB      2
e93e21a0926d        u1                  0.00%               1.223MiB / 1.439GiB   0.08%               5.58kB / 0B         246kB / 1.02MB      2
fad177c1aadf        sad_engelbart       0.30%               1.055MiB / 1.439GiB   0.07%               6.02kB / 0B         639kB / 11.4MB      2
edc0a745ba40        vigorous_snyder     0.00%               1.418MiB / 1.439GiB   0.10%               6.48kB / 0B         1.69MB / 0B         1
7d6d1f9886e3        modest_babbage      0.00%               1.445MiB / 1.439GiB   0.10%               6.77kB / 0B         9.52MB / 0B         1

不过有个疑问,tomcat2, mysql2为什么都比第一个容器tomcat/mymysql内存使用要大非常多(分别大1倍多,4倍多)?

容器中体验TOMCAT

运行一个tomcat容器:tomcat2, 将容器的8080端口映射到本机的18081

➜  test sudo docker run --name tomcat2 -p 18081:8080 -v $PWD/test:/usr/local/tomcat/webapps/ROOT/test -d tomcat

[sudo] password for samxiao:
aa7cc0c99e4eb215fadb4bd52fbbb4b7faee87b757aaf87e8d8ad1f89a1b6316


tomcat2的主页



主机生成一个文件1.txt

➜  test cat > 1.txt <<-"eo"
hi
this is from tomcat web site @docker
SamXIAO
2018.05.01
EOF
eof
eo


拷贝主机文件 1.txt 到tomcat容器中的WEB服务器中

➜  test ls
1.txt  test

➜  test cat 1.txt
hi
this is from tomcat web site @docker
SamXIAO
2018.05.01
EOF
eof

➜  test sudo docker cp 1.txt tomcat2:/usr/local/tomcat/webapps/ROOT
[sudo] password for samxiao:

➜  test sudo docker exec tomcat2 ls /usr/local/tomcat/webapps/ROOT
1.txt
RELEASE-NOTES.txt
WEB-INF
asf-logo-wide.svg
bg-button.png


浏览器看结果

注意地址栏全部小写



oh-my-zsh byobu 终端展示

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息