docker技術是基於Linux內核的cgroup技術實現的,那麼問題來了,在非Linux平臺上是否就不能使用docker技術了呢?答案是能夠的,不過顯然須要藉助虛擬機去模擬出Linux環境來。 docker-machine就是docker公司官方提出的,用於在各類平臺上快速建立具備docker服務的虛擬機的技術,甚至能夠經過指定driver來定製虛擬機的實現原理(通常是virtualbox)。html
docker鏡像在建立以後,每每須要本身手動pull來獲取鏡像,而後執行run命令來運行。當服務須要用到多種容器,容器之間又產生了各類依賴和鏈接的時候,部署一個服務的手動操做是使人感到十分厭煩的。 dcoker-compose技術,就是經過一個.yml配置文件,將全部的容器的部署方法、文件映射、容器鏈接等等一系列的配置寫在一個配置文件裏,最後只須要執行docker-compose up命令就會像執行腳本同樣的去一個個安裝容器並自動部署他們,極大的便利了複雜服務的部署。java
swarm是基於docker平臺實現的集羣技術,他能夠經過幾條簡單的指令快速的建立一個docker集羣,接着在集羣的共享網絡上部署應用,最終實現分佈式的服務。linux
使用Compose僅須要三步:nginx
一、用一個定義你的應用程序的環境,Dockerfile這樣它就能夠在任何地方再現。 二、定義組成您的應用程序的服務,docker-compose.yml 以便它們能夠在隔離的環境中一塊兒運行。 三、運行docker-compose up和撰寫啓動並運行您的整個應用程序。git
參考文章:https://docs.docker.com/compose/overview/github
[root@docker ~]# curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 396 0 --:--:-- 0:00:01 --:--:-- 397
100 10.3M 100 10.3M 0 0 678k 0 0:00:15 0:00:15 --:--:-- 1876k
[root@docker ~]# chmod +x /usr/local/bin/docker-compose
[root@docker ~]# docker-compose --version
docker-compose version 1.21.2, build a133471
複製代碼
- 啓動,中止和重建服務
[root@docker ~]# docker-compose --help
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert deploy
keys in v3 files to their non-Swarm equivalent
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
複製代碼
**docker-compose 運行時是須要指定service名稱,**能夠同時指定多個,也能夠不指定。不指定時默認就是對配置文件中全部的service執行命令。web
-f #用於指定配置文件 -p #用於指定項目名稱docker
**docker-compose build ** 用來建立或從新建立服務使用的鏡像 docker-compose build service_a 建立一個鏡像名叫service_aapache
docker-compose kill 用於經過容器發送SIGKILL信號強行中止服務centos
docker-compose logs 顯示service的日誌信息
docker-compose pause/unpause docker-compose pause #暫停服務 docker-compose unpause #恢復被暫停的服務
docker-compose port 用於查看服務中的端口與物理機的映射關係 docker-compose port nginx_web 80 查看服務中80端口映射到物理機上的那個端口
dokcer-compose ps 用於顯示當前項目下的容器 注意,此命令與docker ps不一樣做用,此命令會顯示中止後的容器(狀態爲Exited),只徵對某個項目。
docker-compose pull 用於拉取服務依賴的鏡像
docker-compose restart 用於重啓某個服務中的全部容器 docker-compose restart service_name 只有正在運行的服務可使用重啓命令,中止的服務是不能夠重啓
docker-compose rm 刪除中止的服務(服務裏的容器)
-f #強制刪除 -v #刪除與容器相關的卷(volumes)
docker-compose run 用於在服務中運行一個一次性的命令。這個命令會新建一個容器,它的配置和srvice的配置相同。 但二者之間仍是有兩點不一樣之處
一、run指定的命令會直接覆蓋掉service配置中指定的命令 二、run命令啓動的容器不會建立在service配置中指定的端口,若是須要指定使用--service-ports指定
docker-compose start/stop docker-compose start 啓動運行某個服務的全部容器 docker-compose stop 中止運行某個服務的全部容器
docker-compose scale 指定某個服務啓動的容器個數
[root@docker ~]# docker-compose scale --help
Numbers are specified in the form `service=num` as arguments.
For example:
$ docker-compose scale web=2 worker=3
This command is deprecated. Use the up command with the `--scale` flag
instead.
Usage: scale [options] [SERVICE=NUM...]
Options:
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
(default: 10)
複製代碼
一個docker-compose.yml的實例文件以下
version: "3"
services:
nginx:
container_name: web-nginx
image: nginx:latest
restart: always
ports:
- 80:80
volumes:
- ./webserver:/webserver
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
複製代碼
下面將配置文件作個簡單的解釋說明
docker-compose的配置文件是一個.yml格式的文件
第一部分
version: "3" #指定語法的版本
第二部分
services: #定義服務
nginx: #服務的名稱,-p參數後接服務名稱
container_name: web-nginx #容器的名稱
image: nginx:latest #鏡像
restart: always
ports: #端口映射
- 80:80
第三部分
volumes: #物理機與容器的磁盤映射關係
- ./webserver:/webserver
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
複製代碼
總體的目錄結構以下
[root@docker docker]# tree ./
./
├── docker-compose.yml
├── nginx
│?? └── nginx.conf
└── webserver
└── index.html
2 directories, 3 files
複製代碼
配置文件以下
[root@docker docker]# cat webserver/index.html
welcome to nginx server!!!!!!!!!
[root@docker docker]# cat nginx/nginx.conf
#user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
client_max_body_size 10m;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root /webserver;
index index.html index.htm;
}
}
include /etc/nginx/conf.d/*.conf;
}
複製代碼
使用命令啓動容器
[root@docker docker]# docker-compose up -d
Pulling nginx (nginx:1.14)...
Trying to pull repository docker.io/library/nginx ...
1.14: Pulling from docker.io/library/nginx
f2aa67a397c4: Already exists
6160d1ac49e9: Pull complete
046b67408776: Pull complete
Digest: sha256:85ab7c44474df01422fe8fdbf9c28e497df427e8a67ce6d47ba027c49be4bdc6
Status: Downloaded newer image for docker.io/nginx:1.14
Creating nginx-server ... done
[root@docker docker]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 891 root 4u IPv6 1187080 0t0 TCP *:http (LISTEN)
[root@docker docker]# docker ps |grep nginx
07ca899cc44b nginx:1.14 "nginx -g 'daemon ..." 29 seconds ago Up 28 seconds 0.0.0.0:80->80/tcp nginx-server
#若是啓動時不指定裏面的服務名稱,就是直接啓動配置文件裏全部的服務
複製代碼
瀏覽器測試是否正常訪問
而後咱們修改相應的首頁文件以下[root@docker docker]# cat webserver/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>welcome to nginx web stie</title>
</head>
<body>
<h2>歡迎來nginx站點</h2>
</body>
</html>
複製代碼
再次打開瀏覽器查看效果
#是否是很是的方便大致步驟分爲如下四步
一、下載所需的文件tomcat,jdk 二、編寫dockerfile來佈署tomcat與java環境,生成鏡像文件 三、編寫docker-compose.yml配置文件,啓動全部容器服務 四、測試負載均衡
具體配置文件以下
#整個目錄結構
[root@master java]# tree ./
./
├── docker-compose.yml
├── etc
│ └── localtime
├── nginx
│ └── nginx.conf
├── tomcat
│ ├── apache-tomcat-8.5.31.tar.gz
│ ├── Dockerfile
│ └── jdk-8u144-linux-x64.tar.gz
└── webserver
├── tomcatA
│ └── index.jsp
└── tomcatB
└── index.jsp
6 directories, 8 files
複製代碼
兩個測試首頁文件
[root@master java]# cat webserver/tomcatA/index.jsp
welcome to tomcat-A server
[root@master java]# cat webserver/tomcatB/index.jsp
welcome to tomcat-B server
複製代碼
配置文件
[root@master java]# cat docker-compose.yml
version: "3"
services:
nginx:
image: nginx:1.14
restart: always
ports:
- 80:80
links:
- tomcat1:tomcat1
- tomcat2:tomcat2
volumes:
- ./webserver:/webserver
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./etc/localtime:/etc/localtime
depends_on:
- tomcat1
- tomcat2
tomcat1:
hostname: tomcat1
build: ./tomcat
volumes:
- ./webserver/tomcatA:/usr/local/apache-tomcat-8.5.31/webapps/ROOT
- ./etc/localtime:/etc/localtime
tomcat2:
hostname: tomcat2
build: ./tomcat
volumes:
- ./webserver/tomcatB:/usr/local/apache-tomcat-8.5.31/webapps/ROOT
- ./etc/localtime:/etc/localtime
複製代碼
安裝JAVA環境
[root@master java]# cat tomcat/Dockerfile
FROM centos
ADD jdk-8u144-linux-x64.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_144
ADD apache-tomcat-8.5.31.tar.gz /usr/local
EXPOSE 8080
ENTRYPOINT ["/usr/local/apache-tomcat-8.5.31/bin/catalina.sh", "run"]
複製代碼
啓動全部容器服務
[root@master java]# docker-compose up
Building tomcat1
Step 1/6 : FROM centos
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
7dc0dca2b151: Pull complete
Digest: sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322
Status: Downloaded newer image for docker.io/centos:latest
---> 49f7960eb7e4
Step 2/6 : ADD jdk-8u144-linux-x64.tar.gz /usr/local
---> 8c9e14062a24
Removing intermediate container a499940235ac
Step 3/6 : ENV JAVA_HOME /usr/local/jdk1.8.0_144
---> Running in cefedfd97f61
---> 12528cd5a517
Removing intermediate container cefedfd97f61
Step 4/6 : ADD apache-tomcat-8.5.31.tar.gz /usr/local
---> 246fa08bea1c
Removing intermediate container a1aaaa2bf0b8
Step 5/6 : EXPOSE 8080
---> Running in 87c4b41f3c1e
---> fd207f27b830
Removing intermediate container 87c4b41f3c1e
Step 6/6 : ENTRYPOINT /usr/local/apache-tomcat-8.5.31/bin/catalina.sh run
---> Running in 9adaed8e3ab9
---> b6fc6d3925f7
Removing intermediate container 9adaed8e3ab9
Successfully built b6fc6d3925f7
WARNING: Image for service tomcat1 was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Building tomcat2
Step 1/6 : FROM centos
---> 49f7960eb7e4
Step 2/6 : ADD jdk-8u144-linux-x64.tar.gz /usr/local
---> Using cache
---> 8c9e14062a24
Step 3/6 : ENV JAVA_HOME /usr/local/jdk1.8.0_144
---> Using cache
---> 12528cd5a517
Step 4/6 : ADD apache-tomcat-8.5.31.tar.gz /usr/local
---> Using cache
---> 246fa08bea1c
Step 5/6 : EXPOSE 8080
---> Using cache
---> fd207f27b830
Step 6/6 : ENTRYPOINT /usr/local/apache-tomcat-8.5.31/bin/catalina.sh run
---> Using cache
---> b6fc6d3925f7
Successfully built b6fc6d3925f7
WARNING: Image for service tomcat2 was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Pulling nginx (nginx:1.14)...
Trying to pull repository docker.io/library/nginx ...
1.14: Pulling from docker.io/library/nginx
f2aa67a397c4: Already exists
6160d1ac49e9: Pull complete
046b67408776: Pull complete
Digest: sha256:85ab7c44474df01422fe8fdbf9c28e497df427e8a67ce6d47ba027c49be4bdc6
Status: Downloaded newer image for docker.io/nginx:1.14
Creating java_tomcat2_1 ... done
Creating java_tomcat1_1 ... done
Creating java_nginx_1 ... done
複製代碼
查看啓動狀況
[root@master java]# docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------
java_nginx_1 nginx -g daemon off; Up 0.0.0.0:80->80/tcp
java_tomcat1_1 /usr/local/apache-tomcat-8 ... Up 8080/tcp
java_tomcat2_1 /usr/local/apache-tomcat-8 ... Up 8080/tcp
複製代碼
[root@master java]# curl http://localhost
welcome to tomcat-A server
[root@master java]# curl http://localhost
welcome to tomcat-B server
[root@master java]# curl http://localhost
welcome to tomcat-A server
[root@master java]# curl http://localhost
welcome to tomcat-B server
複製代碼
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:33 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:34 +0000] "GET / HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:34 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:36 +0000] "GET / HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:36 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:36 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:37 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:38 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:14:39 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:30 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:31 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:32 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 172.19.0.1 - - [08/Jun/2018:02:18:33 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0" "-"
nginx_1 | 192.168.22.170 - - [08/Jun/2018:02:19:32 +0000] "GET / HTTP/1.1" 200 27 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
複製代碼