Docker是一个开源项目,诞生于2013年初,最初是dotCloud公司内部的一个业余项目,基于Google公司推出的Go语言实现。Docker项目的目标是实现轻量级的操作系统虚拟化解决方案。Docker的基础是Linux容器(LXC)等技术。在LXC的基础上Docker进行了进一步的封装,让用户不需要去关心容器的管理,使得操作更为简便。用户操作Docker的容器就像操作一个快速轻量级的虚拟机一样简单。
Docker包括三个基本概念:
仓库(Repository)
仓库是集中存放镜像文件的场所。仓库分为公开仓库(Public)和私有仓库(Private)两种形式。
最大的公开仓库是Docker Hub,存放了数量庞大的镜像供用户下载。
官方文档查看详情
Docker支持多平台的安装:Linux、Windows和OS。下面介绍如何在Ubuntu系统安装Docker。
更新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:~#
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) ...
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) <docker@docker.com>
sub 4096R/F273FCD8 2017-02-22
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo add-apt-repository > "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
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
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) ...
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/
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
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 Hub公共注册服务器中的仓库)。
Docker Hub:https://hub.docker.com;
阿里云镜像:https://dev.aliyun.com/search.html
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
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仓库下载的镜像。
简单的说,容器是独立运行的一个或一组应用,以及他们的运行态环境。如果把镜像看成面向对象的类的话,那么容器就是类的实例化对象。
启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态(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:/#
退出容器,使用exit命令
root@edff644a0ab5:/# exit
exit
root@iZ2ze0ht45qn4m8m1wjfdrZ:~#
更多时候,需要让Docker容器在后台以守护态(Daemonized)形式运行
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker run -d ubuntu /bin/echo "hello docker"
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
083a1e62a68b9ed48e157534b493eeac20e4619c06c7903560758c5d5ef365ad
通过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:~#
通过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
命令查看
可以使用docker stop
来终止一个运行中的容器
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker stop 083a1e62a68b
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
083a1e62a68b
root@iZ2ze0ht45qn4m8m1wjfdrZ:~#
可以使用命令docker start
重动容器
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker start 083a1e62a68b
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
083a1e62a68b
root@iZ2ze0ht45qn4m8m1wjfdrZ:~#
通过docker rm
删除指定的容器
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker rm 513c9c3bf48f
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
513c9c3bf48f
下载镜像时已经使用到了Docker仓库。实际上注册服务器是管理仓库的具体服务器,每个服务器可以有多个仓库,而每个仓库下面有多个镜像。从这方面讲,仓库可以被任务是一个具体的项目或目录。例如对于仓库地址,registry.hub.docker.com/ubuntu来说,registry.hub.docker.com是注册服务器地址,ubuntu是仓库名。
目前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:~#
最简单的方法是通过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:~#
查看镜像:
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
处理过程:浏览器->ubuntu(8080)->Nginx容器(80)
主要针对的是Selenium Grid,用于分布式自动化测试,是一套selenium代码可在不同的环境中运行。而Docker可快速的创建各种环境
Selenium Grid有两个概念:
node:分支节点,可以看作“北京总公司的测试小兵A”和“上海分公司的测试小兵B”和“深圳分公司的测试小兵C”等。
也就是说,在Selenium Grid只有一个主hub,但是可以在本地或远程建立N多个分支node,测试脚本指向主hub,由主hub分配给本地/远程node运行测试用例
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:~#
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:~#
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:~#
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# sudo docker run -d -P --name selenium-hub selenium/hub
sudo: unable to resolve host iZ2ze0ht45qn4m8m1wjfdrZ
54870608ec0933a1d614cca81a4094ba9db6b0b95d1999420a3a2d856072afc2
root@iZ2ze0ht45qn4m8m1wjfdrZ:~#
-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:~#
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参数自动分配
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容器。另外,为了验证脚本添加了打印和截图。
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# python grid_demo.py
get baidu
end...
root@iZ2ze0ht45qn4m8m1wjfdrZ:~#
Docker Compose是Docker官方编排(Orchestration)项目之一,负责快速在集群中部署分布式应用。
Compose允许用户在一个模板中定义一组相关联的应用容器(被称为一个project),例如一个Web服务容器再加上后端的数据库服务容器等。
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是最常见的博客系统,一般部署需要LAMP/WAMP环境,本章节介绍通过Docker compose对他进行编排和部署。
安装数据库使用命令docker pull mysql
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# docker run -p 3306:3306 --name some-mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
843969e0ed8f2aa5191c4156ec604e58d1dbd26df6ad3669de5599a2d03f850f
root@iZ2ze0ht45qn4m8m1wjfdrZ:~# docker run --name some-wordpress --link some-mysql:mysql -p 8088:80 -d wordpress
0742e0698ec65020a590690fb1d240d37f0b0bafb75c38f1c4f92628ce8e0775
原文:https://www.cnblogs.com/sandysun/p/8780435.html