1. 程式人生 > >Docker技術學習

Docker技術學習

from js verify -i exit cut finger adb disable detach

(一)Docker介紹

Docker簡介

Docker是一個開源項目,誕生於2013年初,最初是dotCloud公司內部的一個業余項目,基於Google公司推出的Go語言實現。Docker項目的目標是實現輕量級的操作系統虛擬化解決方案。Docker的基礎是Linux容器(LXC)等技術。在LXC的基礎上Docker進行了進一步的封裝,讓用戶不需要去關心容器的管理,使得操作更為簡便。用戶操作Docker的容器就像操作一個快速輕量級的虛擬機一樣簡單。

Docker優點

  • 啟動速度更快:秒級啟動
  • 快速的部署和交付:開發者可以使用一個標準的鏡像來構建一套開發容器,開發完成之後,運維人員可以直接使用這個容器來部署代碼
  • 更高效的虛擬化:內核化的虛擬化,可以實現更高的性能和效率
  • 高效的遷移:Docker可以運行在不同的平臺,用戶可以輕松的將一個應用從一個平臺遷移到另外一個平臺
  • 節省開支:Docker容器除了運行其中應用外,基本不消耗額外的系統資源,一臺設備可以運行上千個容器

Docker基本概念

Docker包括三個基本概念:

  • 鏡像(Image)
    Docker提供了一個很簡單的機制來創建鏡像或者更新現有的鏡像,用戶甚至可以直接從其他人那裏下載一個已經做好的鏡像直接使用。鏡像可以用來創建Docker容器。
  • 容器(Container)
    容器是從鏡像創建的運行實例。可以被啟動、開始、停止、刪除。每個容器都是相互隔離的、保證安全的平臺。
  • 倉庫(Repository)
    倉庫是集中存放鏡像文件的場所。倉庫分為公開倉庫(Public)和私有倉庫(Private)兩種形式。
    最大的公開倉庫是Docker Hub,存放了數量龐大的鏡像供用戶下載。

    (二)Docker安裝

    官方文檔查看詳情
    Docker支持多平臺的安裝:Linux、Windows和OS。下面介紹如何在Ubuntu系統安裝Docker。

    1.設置倉庫

  • 更新apt包索引

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo apt-get update
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
Hit:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial InRelease
Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates InRelease [102 kB]
Get:3 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security InRelease [102 kB]
Get:4 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main Sources [304 kB]
Get:5 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/universe Sources [200 kB]
Get:6 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 Packages [754 kB]
Get:7 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main i386 Packages [698 kB]
Get:8 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main Translation-en [313 kB]
Get:9 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/universe amd64 Packages [619 kB]
Get:10 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/universe i386 Packages [573 kB]
Get:11 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/universe Translation-en [249 kB]
Get:12 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main Sources [119 kB]
Get:13 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/universe Sources [62.8 kB]
Get:14 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main amd64 Packages [472 kB]
Get:15 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main i386 Packages [424 kB]
Get:16 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/main Translation-en [204 kB]
Get:17 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/universe amd64 Packages [340 kB]
Get:18 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/universe i386 Packages [297 kB]
Get:19 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security/universe Translation-en [127 kB]
Fetched 5,959 kB in 1s (4,416 kB/s)                             
Reading package lists... Done
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 
  • 安裝軟件包,使它允許apt通過HTTPS使用倉庫
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
Reading package lists... Done
Building dependency tree       
Reading state information... Done
apt-transport-https is already the newest version (1.2.26).
ca-certificates is already the newest version (20170717~16.04.1).
curl is already the newest version (7.47.0-1ubuntu2.7).
Suggested packages:
  libcurl4-gnutls-dev python-pycurl-doc python3-pycurl-dbg bsd-mailx
The following NEW packages will be installed:
  python3-pycurl python3-software-properties software-properties-common unattended-upgrades
0 upgraded, 4 newly installed, 0 to remove and 20 not upgraded.
Need to get 104 kB of archives.
After this operation, 799 kB of additional disk space will be used.
Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/main amd64 python3-pycurl amd64 7.43.0-1ubuntu1 [42.3 kB]
Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 python3-software-properties all 0.96.20.7 [20.3 kB]
Get:3 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 software-properties-common all 0.96.20.7 [9,452 B]
Get:4 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 unattended-upgrades all 0.90ubuntu0.9 [32.3 kB]
Fetched 104 kB in 0s (5,944 kB/s)         
Preconfiguring packages ...
Selecting previously unselected package python3-pycurl.
(Reading database ... 100357 files and directories currently installed.)
Preparing to unpack .../python3-pycurl_7.43.0-1ubuntu1_amd64.deb ...
Unpacking python3-pycurl (7.43.0-1ubuntu1) ...
Selecting previously unselected package python3-software-properties.
Preparing to unpack .../python3-software-properties_0.96.20.7_all.deb ...
Unpacking python3-software-properties (0.96.20.7) ...
Selecting previously unselected package software-properties-common.
Preparing to unpack .../software-properties-common_0.96.20.7_all.deb ...
Unpacking software-properties-common (0.96.20.7) ...
Selecting previously unselected package unattended-upgrades.
Preparing to unpack .../unattended-upgrades_0.90ubuntu0.9_all.deb ...
Unpacking unattended-upgrades (0.90ubuntu0.9) ...
Processing triggers for dbus (1.10.6-1ubuntu3.3) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for systemd (229-4ubuntu21.2) ...
Processing triggers for ureadahead (0.100.0-19) ...
Setting up python3-pycurl (7.43.0-1ubuntu1) ...
Setting up python3-software-properties (0.96.20.7) ...
Setting up software-properties-common (0.96.20.7) ...
Setting up unattended-upgrades (0.90ubuntu0.9) ...
Creating config file /etc/apt/apt.conf.d/50unattended-upgrades with new version
Synchronizing state of unattended-upgrades.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable unattended-upgrades
Processing triggers for dbus (1.10.6-1ubuntu3.3) ...
Processing triggers for systemd (229-4ubuntu21.2) ...
Processing triggers for ureadahead (0.100.0-19) ...
  • 添加Docker官方GPG密鑰

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

通過搜索密鑰的最後8個字符,確認已經擁有指紋9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 的密鑰:

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo apt-key fingerprint 0EBFCD88
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
pub   4096R/0EBFCD88 2017-02-22
      Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) <[email protected]>
sub   4096R/F273FCD8 2017-02-22
  • 使用以下命令設置stable的倉庫
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo add-apt-repository > "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

2.安裝Docker CE

  • 更新apt包索引
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo apt-get update
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
Hit:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial InRelease
Hit:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates InRelease
Hit:3 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security InRelease               
Get:4 https://download.docker.com/linux/ubuntu xenial InRelease [65.8 kB]              
Get:5 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages [3,539 B]
Get:6 https://download.docker.com/linux/ubuntu xenial/test amd64 Packages [11.4 kB]
Fetched 80.7 kB in 3s (23.3 kB/s)
Reading package lists... Done
  • 安裝最新版本Docker CE
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo apt-get install docker-ce
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  aufs-tools cgroupfs-mount git git-man liberror-perl libltdl7 pigz
Suggested packages:
  mountall git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb
  git-arch git-cvs git-mediawiki git-svn
The following NEW packages will be installed:
  aufs-tools cgroupfs-mount docker-ce git git-man liberror-perl libltdl7 pigz
0 upgraded, 8 newly installed, 0 to remove and 20 not upgraded.
Need to get 38.2 MB of archives.
After this operation, 208 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/universe amd64 pigz amd64 2.3.1-2 [61.1 kB]
Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/universe amd64 aufs-tools amd64 1:3.2+20130722-1.1ubuntu1 [92.9 kB]
Get:3 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/universe amd64 cgroupfs-mount all 1.2 [4,970 B]
Get:4 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/main amd64 libltdl7 amd64 2.4.6-0.1 [38.3 kB]
Get:5 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/main amd64 liberror-perl all 0.17-1.2 [19.6 kB]
Get:6 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 git-man all 1:2.7.4-0ubuntu1.3 [736 kB]
Get:7 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 git amd64 1:2.7.4-0ubuntu1.3 [3,102 kB]
Get:8 https://download.docker.com/linux/ubuntu xenial/test amd64 docker-ce amd64 18.04.0~ce~2.2.rc2-0~ubuntu [34.1 MB]
Fetched 38.2 MB in 3s (10.9 MB/s)     
Selecting previously unselected package pigz.
(Reading database ... 100415 files and directories currently installed.)
Preparing to unpack .../pigz_2.3.1-2_amd64.deb ...
Unpacking pigz (2.3.1-2) ...
Selecting previously unselected package aufs-tools.
Preparing to unpack .../aufs-tools_1%3a3.2+20130722-1.1ubuntu1_amd64.deb ...
Unpacking aufs-tools (1:3.2+20130722-1.1ubuntu1) ...
Selecting previously unselected package cgroupfs-mount.
Preparing to unpack .../cgroupfs-mount_1.2_all.deb ...
Unpacking cgroupfs-mount (1.2) ...
Selecting previously unselected package libltdl7:amd64.
Preparing to unpack .../libltdl7_2.4.6-0.1_amd64.deb ...
Unpacking libltdl7:amd64 (2.4.6-0.1) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../docker-ce_18.04.0~ce~2.2.rc2-0~ubuntu_amd64.deb ...
Unpacking docker-ce (18.04.0~ce~2.2.rc2-0~ubuntu) ...
Selecting previously unselected package liberror-perl.
Preparing to unpack .../liberror-perl_0.17-1.2_all.deb ...
Unpacking liberror-perl (0.17-1.2) ...
Selecting previously unselected package git-man.
Preparing to unpack .../git-man_1%3a2.7.4-0ubuntu1.3_all.deb ...
Unpacking git-man (1:2.7.4-0ubuntu1.3) ...
Selecting previously unselected package git.
Preparing to unpack .../git_1%3a2.7.4-0ubuntu1.3_amd64.deb ...
Unpacking git (1:2.7.4-0ubuntu1.3) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu21.2) ...
Setting up pigz (2.3.1-2) ...
Setting up aufs-tools (1:3.2+20130722-1.1ubuntu1) ...
Setting up cgroupfs-mount (1.2) ...
Setting up libltdl7:amd64 (2.4.6-0.1) ...
Setting up docker-ce (18.04.0~ce~2.2.rc2-0~ubuntu) ...
Setting up liberror-perl (0.17-1.2) ...
Setting up git-man (1:2.7.4-0ubuntu1.3) ...
Setting up git (1:2.7.4-0ubuntu1.3) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for systemd (229-4ubuntu21.2) ...
Processing triggers for ureadahead (0.100.0-19) ...
  • 通過運行hello-world鏡像驗證Docker CE是否正確安裝
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker run hello-world
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
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://cloud.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

3.其他

  • 查看Docker版本
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker version
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
Client:
 Version:       18.04.0-ce-rc2
 API version:   1.37
 Go version:    go1.9.4
 Git commit:    f4926a2
 Built: Thu Apr  5 05:43:46 2018
 OS/Arch:       linux/amd64
 Experimental:  false
 Orchestrator:  swarm
Server:
 Engine:
  Version:      18.04.0-ce-rc2
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.4
  Git commit:   f4926a2
  Built:        Thu Apr  5 05:41:56 2018
  OS/Arch:      linux/amd64
  Experimental: false
  • 顯示Docker系統信息
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker info
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
Containers: 2
 Running: 0
 Paused: 0
 Stopped: 2
Images: 1
Server Version: 18.04.0-ce-rc2
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 773c489c9c1b21a6d78b5c538cd395416ec50f88
runc version: 4fc53a81fb7c994640722ac585fa9ca548971871
init version: 949e6fa
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.4.0-93-generic
Operating System: Ubuntu 16.04.4 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 992.2MiB
Name: iZ2ze0ht45qn4m8m1wjfdrZ
ID: OA4B:DOC2:P3MG:KY3Y:GEWX:DBZV:3DOD:GF5H:L3RB:KFQN:KVKX:K3C4
Docker Root Dir: /var/lib/docker

(三)Docker鏡像

Docker運行容器前需要本地存在對應的鏡像,如果鏡像不存在本地,Docker會從鏡像倉庫下載(默認是Docker Hub公共註冊服務器中的倉庫)。
Docker Hub:https://hub.docker.com;
阿裏雲鏡像:https://dev.aliyun.com/search.html

鏡像

1.查看鏡像

  • 列出本地鏡像:
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker images
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              f2a91732366c        4 months ago        1.85kB
在列出信息中,可以看到以下字段信息:
  • 來自哪個倉庫,比如ubuntu
  • 鏡像的標誌,比如14.04
  • 它的ID號
  • 創建時間
  • 鏡像大小

2.獲取鏡像

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker pull ubuntu
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
Using default tag: latest
latest: Pulling from library/ubuntu
22dc81ace0ea: Pull complete 
1a8b3c87dba3: Pull complete 
91390a1c435a: Pull complete 
07844b14977e: Pull complete 
b78396653dae: Pull complete 
Digest: sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6
Status: Downloaded newer image for ubuntu:latest

該命令實際相當於$ docker pull registry.hub.docker.com/ubuntu命令,即從註冊服務器registry.hub.docker.com的ubuntu倉庫下載的鏡像。


(四)Docker容器

簡單的說,容器是獨立運行的一個或一組應用,以及他們的運行態環境。如果把鏡像看成面向對象的類的話,那麽容器就是類的實例化對象。

容器

1.啟動容器

啟動容器有兩種方式,一種是基於鏡像新建一個容器並啟動,另外一個是將在終止狀態(stopped)的容器重新啟動。
通過docker run命令來啟動容器

  • 查看運行幫助
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to
                                       disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cidfile string                 Write the container ID to the file
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                 CPU shares (relative weight)
      --cpus decimal                   Number of CPUs
      --cpuset-cpus string             CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string             MEMs in which to allow execution (0-3, 0,1)
  -d, --detach                         Run container in background and print container ID
      --detach-keys string             Override the key sequence for detaching a container
      --device list                    Add a host device to the container
      --device-cgroup-rule list        Add a rule to the cgroup allowed devices list
      --device-read-bps list           Limit read rate (bytes per second) from a device (default [])
      --device-read-iops list          Limit read rate (IO per second) from a device (default [])
      --device-write-bps list          Limit write rate (bytes per second) to a device (default [])
      --device-write-iops list         Limit write rate (IO per second) to a device (default [])
      --disable-content-trust          Skip image verification (default true)
      --dns list                       Set custom DNS servers
      --dns-option list                Set DNS options
      --dns-search list                Set custom DNS search domains
      --entrypoint string              Overwrite the default ENTRYPOINT of the image
  -e, --env list                       Set environment variables
      --env-file list                  Read in a file of environment variables
      --expose list                    Expose a port or a range of ports
      --group-add list                 Add additional groups to join
      --health-cmd string              Command to run to check health
      --health-interval duration       Time between running the check (ms|s|m|h) (default 0s)
      --health-retries int             Consecutive failures needed to report unhealthy
      --health-start-period duration   Start period for the container to initialize before
                                       starting health-retries countdown (ms|s|m|h) (default 0s)
      --health-timeout duration        Maximum time to allow one check to run (ms|s|m|h) (default 0s)
      --help                           Print usage
  -h, --hostname string                Container host name
      --init                           Run an init inside the container that forwards signals and
                                       reaps processes
  -i, --interactive                    Keep STDIN open even if not attached
      --ip string                      IPv4 address (e.g., 172.30.100.104)
      --ip6 string                     IPv6 address (e.g., 2001:db8::33)
      --ipc string                     IPC mode to use
      --isolation string               Container isolation technology
      --kernel-memory bytes            Kernel memory limit
  -l, --label list                     Set meta data on a container
      --label-file list                Read in a line delimited file of labels
      --link list                      Add link to another container
      --link-local-ip list             Container IPv4/IPv6 link-local addresses
      --log-driver string              Logging driver for the container
      --log-opt list                   Log driver options
      --mac-address string             Container MAC address (e.g., 92:d0:c6:0a:29:33)
  -m, --memory bytes                   Memory limit
      --memory-reservation bytes       Memory soft limit
      --memory-swap bytes              Swap limit equal to memory plus swap: ‘-1‘ to enable
                                       unlimited swap
      --memory-swappiness int          Tune container memory swappiness (0 to 100) (default -1)
      --mount mount                    Attach a filesystem mount to the container
      --name string                    Assign a name to the container
      --network string                 Connect a container to a network (default "default")
      --network-alias list             Add network-scoped alias for the container
      --no-healthcheck                 Disable any container-specified HEALTHCHECK
      --oom-kill-disable               Disable OOM Killer
      --oom-score-adj int              Tune host‘s OOM preferences (-1000 to 1000)
      --pid string                     PID namespace to use
      --pids-limit int                 Tune container pids limit (set -1 for unlimited)
      --privileged                     Give extended privileges to this container
  -p, --publish list                   Publish a container‘s port(s) to the host
  -P, --publish-all                    Publish all exposed ports to random ports
      --read-only                      Mount the container‘s root filesystem as read only
      --restart string                 Restart policy to apply when a container exits (default "no")
      --rm                             Automatically remove the container when it exits
      --runtime string                 Runtime to use for this container
      --security-opt list              Security Options
      --shm-size bytes                 Size of /dev/shm
      --sig-proxy                      Proxy received signals to the process (default true)
      --stop-signal string             Signal to stop a container (default "SIGTERM")
      --stop-timeout int               Timeout (in seconds) to stop a container
      --storage-opt list               Storage driver options for the container
      --sysctl map                     Sysctl options (default map[])
      --tmpfs list                     Mount a tmpfs directory
  -t, --tty                            Allocate a pseudo-TTY
      --ulimit ulimit                  Ulimit options (default [])
  -u, --user string                    Username or UID (format: <name|uid>[:<group|gid>])
      --userns string                  User namespace to use
      --uts string                     UTS namespace to use
  -v, --volume list                    Bind mount a volume
      --volume-driver string           Optional volume driver for the container
      --volumes-from list              Mount volumes from the specified container(s)
  -w, --workdir string                 Working directory inside the container

下面命令輸出一個“Hello World”,之後終止容器。

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker run ubuntu /bin/echo "hello world"
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
hello world
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 

下面進入到ubuntu容器中

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker run -t -i ubuntu /bin/bash
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
root@edff644a0ab5:/# 
  • -t選項讓Docker分配一個偽終端(pseudo-tty)並綁定到容器的標準輸入上
  • -i則讓容器的標準輸入保持打開
    此時,已經在ubuntu容器中,這是一個獨立的ubuntu系統,通過root@edff644a0ab5標識可以看出。
    當利用docker run來創建容器時,Docker在後臺運行的標準操作包括:
  • 檢查本地是否存在指定的鏡像,不存在就從公有倉庫下載
  • 利用鏡像創建並啟動一個容器
  • 分配一個文件系統,並在只讀的鏡像層外面掛載一層可讀可層
  • 從宿主主機配置的網橋接口中橋接一個虛擬接口到容器中去
  • 從地址池配置一個IP地址給容器
  • 執行用戶指定的應用程序
  • 執行完畢後容器被終止

退出容器,使用exit命令

root@edff644a0ab5:/# exit
exit
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 

2.守護狀態運行

更多時候,需要讓Docker容器在後臺以守護態(Daemonized)形式運行

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker run -d ubuntu /bin/echo "hello docker"
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
083a1e62a68b9ed48e157534b493eeac20e4619c06c7903560758c5d5ef365ad
  • -d表示容器以守護態(Daemonized)形式運行

3.查看容器

通過docker ps命令查看當前運行的所有容器

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker ps -a
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                          PORTS               NAMES
083a1e62a68b        ubuntu              "/bin/echo ‘hello do…"   About a minute ago   Exited (0) About a minute ago                       gallant_williams
edff644a0ab5        ubuntu              "/bin/bash"              8 minutes ago        Exited (0) 3 minutes ago                            competent_turing
4975cdea26b6        ubuntu              "/bin/echo ‘hello wo…"   9 minutes ago        Exited (0) 9 minutes ago                            trusting_zhukovsky
abbed7135c45        hello-world         "/hello"                 18 hours ago         Exited (0) 18 hours ago                             silly_perlman
513c9c3bf48f        hello-world         "/hello"                 18 hours ago         Exited (0) 18 hours ago                             hopeful_poincare
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 

4.獲取容器的輸出信息

通過docker logs命令

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker logs gallant_williams
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
hello docker
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker logs 083a1e62a68b
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
hello docker

gallant_williams為容器的NAMES,083a1e62a68b為容器的ID,可通過docker ps -a命令查看

5.停止容器

可以使用docker stop來終止一個運行中的容器

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker stop 083a1e62a68b
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
083a1e62a68b
root@iZ2ze0ht45qn4m8m1wjfdrZ:~#

6.重啟容器

可以使用命令docker start重動容器

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker start 083a1e62a68b
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
083a1e62a68b
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 

7.刪除容器

通過docker rm刪除指定的容器

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker rm 513c9c3bf48f      
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
513c9c3bf48f

(五)Docker倉庫

下載鏡像時已經使用到了Docker倉庫。實際上註冊服務器是管理倉庫的具體服務器,每個服務器可以有多個倉庫,而每個倉庫下面有多個鏡像。從這方面講,倉庫可以被任務是一個具體的項目或目錄。例如對於倉庫地址,registry.hub.docker.com/ubuntu來說,registry.hub.docker.com是註冊服務器地址,ubuntu是倉庫名。

Docker Hub

目前Docker官方維護了一個公共倉庫Docker Hub:https://hub.docker.com
可以通過docker search命令來查找官方倉庫中的鏡像,並利用docker pull命令下載到本地

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker search ubuntu
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
NAME                                                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                    Ubuntu is a Debian-based Linux operating sys…   7482                [OK]                
dorowu/ubuntu-desktop-lxde-vnc                            Ubuntu with openssh-server and NoVNC            178                                     [OK]
rastasheep/ubuntu-sshd                                    Dockerized SSH service, built on top of offi…   139                                     [OK]
ansible/ubuntu14.04-ansible                               Ubuntu 14.04 LTS with ansible                   91                                      [OK]
ubuntu-upstart                                            Upstart is an event-based replacement for th…   85                  [OK]                
neurodebian                                               NeuroDebian provides neuroscience research s…   46                  [OK]                
ubuntu-debootstrap                                        debootstrap --variant=minbase --components=m…   37                  [OK]                
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5      ubuntu-16-nginx-php-phpmyadmin-mysql-5          33                                      [OK]
nuagebec/ubuntu                                           Simple always updated Ubuntu docker images w…   22                                      [OK]
tutum/ubuntu                                              Simple Ubuntu docker images with SSH access     18                                      
ppc64le/ubuntu                                            Ubuntu is a Debian-based Linux operating sys…   12                                      
i386/ubuntu                                               Ubuntu is a Debian-based Linux operating sys…   11                                      
1and1internet/ubuntu-16-apache-php-7.0                    ubuntu-16-apache-php-7.0                        9                                       [OK]
eclipse/ubuntu_jdk8                                       Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, …   5                                       [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10   ubuntu-16-nginx-php-phpmyadmin-mariadb-10       4                                       [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4         ubuntu-16-nginx-php-5.6-wordpress-4             3                                       [OK]
codenvy/ubuntu_jdk8                                       Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, …   3                                       [OK]
darksheer/ubuntu                                          Base Ubuntu Image -- Updated hourly             3                                       [OK]
1and1internet/ubuntu-16-apache                            ubuntu-16-apache                                3                                       [OK]
pivotaldata/ubuntu                                        A quick freshening-up of the base Ubuntu doc…   1                                       
1and1internet/ubuntu-16-sshd                              ubuntu-16-sshd                                  0                                       [OK]
ossobv/ubuntu                                             Custom ubuntu image from scratch (based on o…   0                                       
smartentry/ubuntu                                         ubuntu with smartentry                          0                                       [OK]
pivotaldata/ubuntu-gpdb-dev                               Ubuntu images for GPDB development              0                                       
1and1internet/ubuntu-16-healthcheck                       ubuntu-16-healthcheck                           0                                       [OK]
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 

(六)創建Nginx容器

1.獲取Nginx鏡像

最簡單的方法是通過docker pull nginx命令創建Nginx容器

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker pull nginx
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
Using default tag: latest
latest: Pulling from library/nginx
2a72cbf407d6: Pull complete 
e19f9e910af9: Pull complete 
2f3d26a87e79: Pull complete 
Digest: sha256:e36d7f5dabf1429d84135bb8a8086908e1150f1a178c75719a9e0e53ebb90353
Status: Downloaded newer image for nginx:latest
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 

2.啟動Nginx鏡像

查看鏡像:

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker images
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              c5c4e8fa2cf7        6 days ago          109MB
ubuntu              latest              f975c5035748        4 weeks ago         112MB
hello-world         latest              f2a91732366c        4 months ago        1.85kB
root@iZ2ze0ht45qn4m8m1wjfdrZ:~#

啟動nginx容器:

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker run --name some-nginx -d -p 8080:80 nginx
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
5fad62588ef5dcaa14b3119f7d4d81f1d7b1fa67e98f0ceaaf0083e9b88e3ccd
  • -name指定啟動容器的名稱為some-nginx
  • -d讓Docker容器在後臺以守護態(Daemonized)形式運行
  • -p將容器的80端口映射到主機的8080端口
  • nginx為啟動容器的鏡像

3.訪問Nginx鏡像

處理過程:瀏覽器->ubuntu(8080)->Nginx容器(80)
技術分享圖片


(七)創建Docker selenium容器


Selenium

主要針對的是Selenium Grid,用於分布式自動化測試,是一套selenium代碼可在不同的環境中運行。而Docker可快速的創建各種環境
Selenium Grid有兩個概念:

  • hub:主節點,可以看作“北京總公司的測試經理”;
  • node:分支節點,可以看作“北京總公司的測試小兵A”和“上海分公司的測試小兵B”和“深圳分公司的測試小兵C”等。
    也就是說,在Selenium Grid只有一個主hub,但是可以在本地或遠程建立N多個分支node,測試腳本指向主hub,由主hub分配給本地/遠程node運行測試用例

    docker selenium環境安裝

    以ubuntu環境為例進行安裝:
  • 1.下載主hub鏡像(北京總公司的測試經理)

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker pull selenium/hub
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
Using default tag: latest
latest: Pulling from selenium/hub
22dc81ace0ea: Already exists 
1a8b3c87dba3: Already exists 
91390a1c435a: Already exists 
07844b14977e: Already exists 
b78396653dae: Already exists 
884e48e3a86f: Pull complete 
b6fd7856c87e: Pull complete 
8e5b73f31318: Pull complete 
721fae97e0e4: Pull complete 
33032a64c6c4: Pull complete 
f8992564d2f4: Pull complete 
7fe2728e863a: Pull complete 
Digest: sha256:88b90958b774a087179e9072d08ec9ecc32b2b9ea4b141ba229557c139d4db18
Status: Downloaded newer image for selenium/hub:latest
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 
  • 2.下載主node chrome鏡像(上海分公司的測試小兵B)
latest: Pulling from selenium/node-chrome
22dc81ace0ea: Already exists 
1a8b3c87dba3: Already exists 
91390a1c435a: Already exists 
07844b14977e: Already exists 
b78396653dae: Already exists 
884e48e3a86f: Already exists 
b6fd7856c87e: Already exists 
8e5b73f31318: Already exists 
721fae97e0e4: Already exists 
33032a64c6c4: Already exists 
e49dcc7a4a51: Pull complete 
c6cf662c7103: Pull complete 
3bdfd3451d60: Pull complete 
513ed68316d2: Pull complete 
038766162d32: Pull complete 
0b1c9987c144: Pull complete 
b22a907c6e95: Pull complete 
c6ca885bd99c: Pull complete 
1d3052e345ac: Pull complete 
8327fd5cf6f6: Pull complete 
Digest: sha256:dfe989a91ae710e1d234e6ff43a2a027fd0f017d6f1727106f8a4d78ce59ecfc
Status: Downloaded newer image for selenium/node-chrome:latest
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 
  • 3.查看鏡像
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker images
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
nginx                  latest              c5c4e8fa2cf7        6 days ago          109MB
selenium/node-chrome   latest              714e0f6c70e6        6 days ago          887MB
selenium/hub           latest              d6f4bd1f74a4        6 days ago          287MB
ubuntu                 latest              f975c5035748        4 weeks ago         112MB
hello-world            latest              f2a91732366c        4 months ago        1.85kB
root@iZ2ze0ht45qn4m8m1wjfdrZ:~#
  • 4.啟動主hub容器
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker run -d -P --name selenium-hub selenium/hub
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
54870608ec0933a1d614cca81a4094ba9db6b0b95d1999420a3a2d856072afc2
root@iZ2ze0ht45qn4m8m1wjfdrZ:~#
  • -d表示容器以守護態(Daemonized)形式運行
  • -P表示Docker會隨機映射一個49000~49900的端口到內部容器開放的網絡端口

  • 5.啟動分支node chrome容器

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker run -d --link selenium-hub:hub selenium/node-chrome
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
e17e25b4714ec1b6107262d35efea1ea1cc62636b213fe67176a5b3dd0b44edd
root@iZ2ze0ht45qn4m8m1wjfdrZ:~#
  • -link通過link關聯selenium-hub容器,並為其設置了別名hub
  • 6.查看容器
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker ps -a
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS                    PORTS                     NAMES
e17e25b4714e        selenium/node-chrome   "/opt/bin/entry_poin…"   2 minutes ago       Up 2 minutes                                        adoring_thompson
54870608ec09        selenium/hub           "/opt/bin/entry_poin…"   5 minutes ago       Up 5 minutes              0.0.0.0:32768->4444/tcp   selenium-hub
5fad62588ef5        nginx                  "nginx -g ‘daemon of…"   2 hours ago         Up 2 hours                0.0.0.0:8080->80/tcp      some-nginx
083a1e62a68b        ubuntu                 "/bin/echo ‘hello do…"   3 hours ago         Exited (0) 3 hours ago                              gallant_williams
edff644a0ab5        ubuntu                 "/bin/bash"              3 hours ago         Exited (0) 3 hours ago                              competent_turing
4975cdea26b6        ubuntu                 "/bin/echo ‘hello wo…"   3 hours ago         Exited (0) 3 hours ago                              trusting_zhukovsky
abbed7135c45        hello-world            "/hello"                 21 hours ago        Exited (0) 21 hours ago                             silly_perlman
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 

需要註意,selenium/hub容器端口號為4444.對Ubuntu映射的端口為32768,前面通過-P參數自動分配


創建Grid測試腳本與運行

  • 1.編寫Selenium Grid腳本(grid_demo.py)
from selenium import webdriver
from time import sleep

driver = webdriver.Remote(command_executor=‘http://127.0.0.1:32768/wd/hub‘,
desired_capabilities={‘browserName‘: ‘chrome‘}
)

driver.get("https://www.baidu.com")
print("get baidu")

driver.find_element_by_id("kw").send_keys("docker selenium")
driver.find_element_by_id("su").click()

sleep(1)

driver.get_screenshot_as_file("/home/test/baidu_img.png")

driver.quit()
print("end...")

註意訪問的端口號和瀏覽器,只啟動了node chrome容器。另外,為了驗證腳本添加了打印和截圖。

  • 2.運行腳本
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# python grid_demo.py
get baidu
end...
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 
  • 3.查看截圖
    技術分享圖片

(八)Docker Compose安裝

Docker compose是什麽

Docker Compose是Docker官方編排(Orchestration)項目之一,負責快速在集群中部署分布式應用。
Compose允許用戶在一個模板中定義一組相關聯的應用容器(被稱為一個project),例如一個Web服務容器再加上後端的數據庫服務容器等。

Docker compose安裝

  • 調用Docker提供的API,使用pip安裝:python -m pip install docker-compose
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# python -m pip install docker-compose
Collecting docker-compose
  Downloading http://mirrors.aliyun.com/pypi/packages/2e/66/cf3e52f24485b1e0537a9d8a9d2a6bae2703775f4bd585d017a28adf1758/docker_compose-1.20.1-py2.py3-none-any.whl (119kB)
    100% |████████████████████████████████| 122kB 23.0MB/s 
Collecting websocket-client<1.0,>=0.32.0 (from docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/9d/fb/f51a03e232e00d6c504dfe815aed090c894ba3f8d3f7fd9612f3e227bf24/websocket_client-0.47.0-py2.py3-none-any.whl (200kB)
    100% |████████████████████████████████| 204kB 44.0MB/s 
Collecting backports.ssl-match-hostname>=3.5; python_version < "3.5" (from docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/76/21/2dc61178a2038a5cb35d14b61467c6ac632791ed05131dda72c20e7b9e23/backports.ssl_match_hostname-3.5.0.1.tar.gz
Requirement already satisfied: PyYAML<4,>=3.10 in /usr/local/lib/python2.7/dist-packages (from docker-compose)
Collecting dockerpty<0.5,>=0.4.1 (from docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/8d/ee/e9ecce4c32204a6738e0a5d5883d3413794d7498fe8b06f44becc028d3ba/dockerpty-0.4.1.tar.gz
Collecting ipaddress>=1.0.16; python_version < "3.3" (from docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/f0/ba/860a4a3e283456d6b7e2ab39ce5cf11a3490ee1a363652ac50abf9f0f5df/ipaddress-1.0.19.tar.gz
Collecting docopt<0.7,>=0.6.1 (from docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/a2/55/8f8cab2afd404cf578136ef2cc5dfb50baa1761b68c9da1fb1e4eed343c9/docopt-0.6.2.tar.gz
Collecting enum34<2,>=1.0.4; python_version < "3.4" (from docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/c5/db/e56e6b4bbac7c4a06de1c50de6fe1ef3810018ae11732a50f15f62c7d050/enum34-1.1.6-py2-none-any.whl
Requirement already satisfied: requests!=2.11.0,!=2.12.2,!=2.18.0,<2.19,>=2.6.1 in /usr/local/lib/python2.7/dist-packages (from docker-compose)
Collecting texttable<0.10,>=0.9.0 (from docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/02/e1/2565e6b842de7945af0555167d33acfc8a615584ef7abd30d1eae00a4d80/texttable-0.9.1.tar.gz
Collecting cached-property<2,>=1.2.0 (from docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/ef/01/58e0ee54e37cb5fb54902bb0ddfc8b0c7695bb3041d361db6a67d9738c18/cached_property-1.4.0-py2.py3-none-any.whl
Collecting jsonschema<3,>=2.5.1 (from docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/77/de/47e35a97b2b05c2fadbec67d44cfcdcd09b8086951b331d82de90d2912da/jsonschema-2.6.0-py2.py3-none-any.whl
Requirement already satisfied: six<2,>=1.3.0 in /usr/local/lib/python2.7/dist-packages (from docker-compose)
Collecting docker<4.0,>=3.1.4 (from docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/e4/c0/ab62cdb6117c1b9824e54f707060eb63ef7e4026d9aedf2d10853ba6df98/docker-3.2.1-py2.py3-none-any.whl (123kB)
    100% |████████████████████████████████| 133kB 78.6MB/s 
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/dist-packages (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.19,>=2.6.1->docker-compose)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python2.7/dist-packages (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.19,>=2.6.1->docker-compose)
Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/lib/python2.7/dist-packages (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.19,>=2.6.1->docker-compose)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python2.7/dist-packages (from requests!=2.11.0,!=2.12.2,!=2.18.0,<2.19,>=2.6.1->docker-compose)
Collecting functools32; python_version == "2.7" (from jsonschema<3,>=2.5.1->docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/5e/1a/0aa2c8195a204a9f51284018562dea77e25511f02fe924fac202fc012172/functools32-3.2.3-2.zip
Collecting docker-pycreds>=0.2.2 (from docker<4.0,>=3.1.4->docker-compose)
  Downloading http://mirrors.aliyun.com/pypi/packages/1c/8b/538e392ecd02aef761390fd30f057af73679839bd2096130689d73142aea/docker_pycreds-0.2.2-py2.py3-none-any.whl
Building wheels for collected packages: backports.ssl-match-hostname, dockerpty, ipaddress, docopt, texttable, functools32
  Running setup.py bdist_wheel for backports.ssl-match-hostname ... done
  Stored in directory: /root/.cache/pip/wheels/de/86/7a/b60c8892039e60d59bedfca7f812b25b5dc86e664ca80b65c9
  Running setup.py bdist_wheel for dockerpty ... done
  Stored in directory: /root/.cache/pip/wheels/1b/96/b8/f3918a2fd47437b6464e9c187aff71ec8be668aafc0f36bac4
  Running setup.py bdist_wheel for ipaddress ... done
  Stored in directory: /root/.cache/pip/wheels/16/57/47/a5853dc33c401e02dc9299e576ca6d8c69515d0c66963239ab
  Running setup.py bdist_wheel for docopt ... done
  Stored in directory: /root/.cache/pip/wheels/81/7a/3b/9f1ec9da9d3d4116a4ad1ab9bc007239ef5c1131793d825567
  Running setup.py bdist_wheel for texttable ... done
  Stored in directory: /root/.cache/pip/wheels/a7/55/84/4a547c64651b6576e5a8d07748fb427bc83988a66f862da88b
  Running setup.py bdist_wheel for functools32 ... done
  Stored in directory: /root/.cache/pip/wheels/4b/ec/cd/36bb2a03a11dff82575e43e0ac512eb42d8afa16fbfa72bd8a
Successfully built backports.ssl-match-hostname dockerpty ipaddress docopt texttable functools32
Installing collected packages: websocket-client, backports.ssl-match-hostname, dockerpty, ipaddress, docopt, enum34, texttable, cached-property, functools32, jsonschema, docker-pycreds, docker, docker-compose
Successfully installed backports.ssl-match-hostname-3.5.0.1 cached-property-1.4.0 docker-3.2.1 docker-compose-1.20.1 docker-pycreds-0.2.2 dockerpty-0.4.1 docopt-0.6.2 enum34-1.1.6 functools32-3.2.3.post2 ipaddress-1.0.19 jsonschema-2.6.0 texttable-0.9.1 websocket-client-0.47.0
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# 
  • 安裝成功後,使用docker-compose命令查看
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# docker-compose
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:

(九)創建Wordpress應用

wordpress是最常見的博客系統,一般部署需要LAMP/WAMP環境,本章節介紹通過Docker compose對他進行編排和部署。


wordpress應用部署


1. 安裝mysql服務並創建mysql容器,指定數據庫root密碼為123456

安裝數據庫使用命令docker pull mysql

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# docker run -p 3306:3306 --name some-mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
843969e0ed8f2aa5191c4156ec604e58d1dbd26df6ad3669de5599a2d03f850f 

2. 創建wordpress容器應用並後臺運行

root@iZ2ze0ht45qn4m8m1wjfdrZ:~# docker run --name some-wordpress --link some-mysql:mysql -p 8088:80 -d wordpress
0742e0698ec65020a590690fb1d240d37f0b0bafb75c38f1c4f92628ce8e0775

技術分享圖片

Docker技術學習