1 編輯dockerfile文件html
vim Dockerfile #Set the base image to CentOS FROM centos # File Author / Maintainer MAINTAINER lv # Install necessary tools RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel # Install Nginx ADD http://nginx.org/download/nginx-1.8.0.tar.gz . RUN tar zxvf nginx-1.8.0.tar.gz RUN mkdir -p /usr/local/nginx RUN cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx && make && make install RUN rm -fv /usr/local/nginx/conf/nginx.conf COPY .nginx_conf /usr/local/nginx/conf/nginx.conf // 在當前目錄準備一個配置文件。 # Expose ports EXPOSE 80 # Set the default command to execute when creating a new container ENTRYPOINT /usr/local/nginx/sbin/nginx && tail -f /etc/passwd
備註:這裏tail -f /etc/passwd的做用是能讓容器持續運行。不加的話容器剛啓動就會退出。
2建立鏡像:docker build -t centos_nginx
//建立鏡像 .docker images
//能夠看到咱們新建的鏡像docker run -itd -p 81:80 centos_nginx bash
//啓動容器
3 簡單測試
若是容器裏面的nginx,配置文件,默認虛擬主機都正確的話,咱們就能宿主機上直接訪問web了curl 127.0.0.1:81
會訪問到默認頁。mysql
docker compose能夠方便咱們快捷高效地管理容器的啓動、中止、重啓等操做,它相似於linux下的shell腳本,基於yaml語法,在該文件裏咱們能夠描述應用的架構,好比用什麼鏡像、數據卷、網絡模式、監聽端口等信息。咱們能夠在一個compose文件中定義一個多容器的應用(好比jumpserver),而後經過該compose來啓動這個應用。
安裝compose方法以下linux
curl -L https://github.com/docker/compose/releases/download/1.17.0-rc1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose chmod 755 !$
docker-compose version
查看版本信息
Compose區分Version 1和Version 2(Compose 1.6.0+,Docker Engine 1.10.0+)。Version 2支持更多的指令。Version 1沒有聲明版本默認是"version 1"。Version 1未來會被棄用。
vim docker-compose.yml //nginx
version: "2" //定義使用的命令版本 services: app1: // 定義第一個應用。名字沒有實際含義 image: centos_nginx // 定義要使用的鏡像 ports: - "8080:80" // 定義容器和宿主機的端口映射關係 networks: - "net1" //定義容器使用的網絡 volumes: - /data/:/data // 定義數據卷容器共享目錄及和宿主機目錄映射關係,至關於-v選項 app2: image: centos networks: - "net2" volumes: - /data/:/data1 entrypoint: tail -f /etc/passwd //啓動容器時附加執行一條命令 networks: net1: driver: bridge //定義容器使用的網絡模型爲橋接 net2: driver: bridge
備註:這裏的tail -f /etc/passwd 跟上面建立的centos_nginx裏面的做用相同。也是爲了讓容器持續運行。app1裏面的鏡像centos_nginx中已經有了這句,再也不重複。app2當中centos鏡像裏沒有相似的語句,所以須要在entrypoint 裏面加上這句。docker-compose up -d
能夠啓動兩個容器 // up 至關於先create 再start ,-d 丟入後臺。
docker-compose --help // 查看可用的選項
docker-compose ps/down/stop/start/rm
關於docker-compose語法的參考文檔 http://www.web3.xin/index/article/182.htmlgit