如何在程序中调用docker容器 docker在web开发中得使用流程是怎样的
\u5982\u4f55\u5728Docker\u5bb9\u5668\u4e2d\u8fd0\u884cGUI\u7a0b\u5e8f\u3000\u3000\u6211\u4eec\u53ef\u4ee5\u8f7b\u6613\u5730\u5728Docker\u5bb9\u5668\u4e2d\u8fd0\u884c\u5927\u591a\u6570GUI\u7a0b\u5e8f\u4e14\u4e0d\u51fa\u9519\u3002Docker\u662f\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6253\u5305\u3001\u5206\u53d1\u548c\u8fd0\u884c\u4efb\u610f\u7a0b\u5e8f\u7684\u8f7b\u91cf\u7ea7\u5bb9\u5668
\u7684\u5f00\u653e\u5e73\u53f0\u3002\u5b83\u6ca1\u6709\u8bed\u8a00\u652f\u6301\u3001\u6846\u67b6\u6216\u8005\u6253\u5305\u7cfb\u7edf\u7684\u9650\u5236\uff0c\u5e76\u53ef\u4ee5\u8fd0\u884c\u5728\u4efb\u4f55\u5730\u65b9\u3001\u4efb\u4f55\u65f6\u5019\uff0c\u4ece\u5c0f\u578b\u7684\u5bb6\u7528\u7535\u8111\u5230\u9ad8\u7aef\u7684\u670d\u52a1\u5668\u90fd\u53ef\u4ee5\u8fd0\u884c\u3002\u8fd9\u8ba9\u4eba\u4eec\u53ef\u4ee5\u6253\u5305\u4e0d
\u540c\u7684\u5305\u7528\u4e8e\u90e8\u7f72\u548c\u6269\u5c55\u7f51\u7edc\u5e94\u7528\uff0c\u6570\u636e\u5e93\u548c\u540e\u7aef\u670d\u52a1\u800c\u4e0d\u5fc5\u4f9d\u8d56\u4e8e\u7279\u5b9a\u7684\u6808\u6216\u8005\u63d0\u4f9b\u5546\u3002
\u4e0b\u9762\u662f\u6211\u4eec\u8be5\u5982\u4f55\u5728Docker\u5bb9\u5668\u4e2d\u8fd0\u884cGUI\u7a0b\u5e8f\u7684\u7b80\u5355\u6b65\u9aa4\u3002\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f1a\u7528Firefox\u4f5c\u4e3a\u4f8b\u5b50\u3002
1. \u5b89\u88c5 Docker
\u5728\u5f00\u59cb\u524d\uff0c\u6211\u4eec\u9996\u5148\u5f97\u786e\u4fdd\u5728Linux\u4e3b\u673a\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Docker\u3002\u8fd9\u91cc\uff0c\u6211\u8fd0\u884c\u7684\u662fCentOS 7 \u4e3b\u673a\uff0c\u6211\u4eec\u5c06\u8fd0\u884cyum\u7ba1\u7406\u5668\u548c\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u5b89\u88c5Docker\u3002
# yum install docker
# systemctl restart docker.service
2. \u521b\u5efa Dockerfile
\u73b0\u5728\uff0cDocker\u5b88\u62a4\u8fdb\u7a0b\u5df2\u7ecf\u5728\u8fd0\u884c\u4e2d\u4e86\uff0c\u6211\u4eec\u73b0\u5728\u51c6\u5907\u521b\u5efa\u81ea\u5df1\u7684Firefox
Docker\u5bb9\u5668\u3002\u6211\u4eec\u8981\u521b\u5efa\u4e00\u4e2aDockerfile\uff0c\u5728\u5176\u4e2d\u6211\u4eec\u8981\u8f93\u5165\u9700\u8981\u7684\u914d\u7f6e\u6765\u521b\u5efa\u4e00\u4e2a\u53ef\u4ee5\u5de5\u4f5c\u7684Firefox\u5bb9\u5668\u3002\u4e3a\u4e86\u8fd0\u884c Docker
\u955c\u50cf\u6211\u4eec\u9700\u8981\u4f7f\u7528\u6700\u65b0\u7248\u672c\u7684CentOS\u3002\u8981\u521b\u5efa Docker \u955c\u50cf\uff0c\u6211\u4eec\u9700\u8981\u7528\u6587\u672c\u7f16\u8f91\u5668\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aDockerfile\u7684\u6587\u4ef6\u3002
# nano Dockerfile
\u63a5\u7740\uff0c\u5728Dockerfile\u4e2d\u6dfb\u52a0\u4e0b\u9762\u7684\u884c\u5e76\u4fdd\u5b58\u3002
#!/bin/bash
FROM centos:7
RUN yum install -y firefox
# \u7528\u4f60\u81ea\u5df1\u7684 uid /gid \u66ff\u6362\u4e0b\u9762\u76840
RUN export uid=0 gid=0
RUN mkdir -p /home/developer
RUN echo "developer:x:${uid}:${gid}:Developer,,,:/home/developer:/bin/bash">>/etc/passwd
RUN echo "developer:x:${uid}:">>/etc/group
RUN echo "developer ALL=(ALL) NOPASSWD: ALL">>/etc/sudoers
RUN chmod 0440/etc/sudoers
RUN chown ${uid}:${gid}-R /home/developer
USER developer
ENV HOME /home/developer
CMD /usr/bin/firefox
\u6ce8\u610f\uff1a\u5728\u7b2c\u56db\u884c\u7684\u914d\u7f6e\u4e2d\uff0c\u7528\u4f60\u81ea\u5df1\u7684\u7528\u6237\u548c\u7ec4id\u6765\u66ff\u63620\u3002 \u6211\u4eec\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u5728shell\u6216\u8005\u7ec8\u7aef\u4e2d\u5f97\u5230uid\u548cgid\u3002
# id $USER
3. \u6784\u9020Docker\u5bb9\u5668
\u4e0b\u9762\u6211\u4eec\u5c31\u8981\u6839\u636e\u4e0a\u9762\u7684Dockerfile\u6784\u5efa\u4e00\u4e2a\u5bb9\u5668\u3002\u5b83\u4f1a\u5b89\u88c5firefox\u6d4f\u89c8\u5668\u548c\u5b83\u9700\u8981\u7684\u5305\u3002\u5b83\u63a5\u7740\u4f1a\u8bbe\u7f6e\u7528\u6237\u6743\u9650\u5e76\u8ba9\u5b83\u53ef\u4ee5\u5de5\u4f5c\u3002\u8fd9\u91cc\u955c\u50cf\u540d\u662ffirefox\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u8981\u547d\u540d\u3002
# docker build --rm -t firefox .
4. \u8fd0\u884cDocker\u5bb9\u5668
\u73b0\u5728\uff0c\u5982\u679c\u4e00\u5207\u987a\u5229\uff0c\u6211\u4eec\u73b0\u5728\u53ef\u4ee5\u5728\u8fd0\u884c\u5728CentOS 7\u955c\u50cf\u4e2d\u7684Docker\u5bb9\u5668\u91cc\u9762\u8fd0\u884c\u6211\u4eec\u7684GUI\u7a0b\u5e8f\u4e5f\u5c31\u662fFirefox\u6d4f\u89c8\u5668\u4e86\u3002
# docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix firefox
\u603b\u7ed3
\u5728Docker\u5bb9\u5668\u4e2d\u8fd0\u884cGUI\u7a0b\u5e8f\u662f\u4e00\u6b21\u5f88\u68d2\u7684\u4f53\u9a8c\uff0c\u5b83\u5bf9\u4f60\u7684\u4e3b\u673a\u6587\u4ef6\u7cfb\u7edf\u6ca1\u6709\u4efb\u4f55\u7684\u4f24\u5bb3\u3002\u5b83\u5b8c\u5168\u4f9d\u8d56\u4f60\u7684Docker\u5bb9\u5668\u3002
docker\u5728web\u5f00\u53d1\u4e2d\u5f97\u4f7f\u7528\u6d41\u7a0b\u6982\u8ff0\uff1a
1\u3001\u521b\u5efajava\u955c\u50cf\uff1a
\u628a\u6240\u6709\u7684\u6587\u4ef6\u90fd\u653e\u7f6e\u5728docker\u76ee\u5f55\u4e0b\uff0c\u5206\u522b\u4e3a\u4e0d\u540c\u7684\u529f\u80fd\u521b\u5efa\u4e0d\u540c\u7684\u76ee\u5f55\u3002
$ sudo mkdir docker/java$ sudo cd docker/java\u63a5\u4e0b\u6765\u5728\u8be5\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2ajdk\u7684Dockerfile\u6587\u4ef6\uff0c\u5177\u4f53\u5185\u5bb9\u5982\u4e0b\uff1a
# openjdk 6# version 1.0FROM ubuntu:14.04MAINTAINER mhy "[email protected]"RUN apt-get updateRUN apt-get install -y -q openjdk-7-jdkWORKDIR /ENV JAVA_HOME /usr/lib/jvm/java-7-openjdk-amd64CMD ["/bin/bash"]\u521b\u5efa\u5b8cDockerfile\u4e4b\u540e\u6765\u751f\u6210\u4e00\u4e2ajdk\u7684\u955c\u50cf
$ sudo docker build -t pobaby/java .
\u53ef\u4ee5\u770b\u5230\u5df2\u7ecf\u751f\u6210\u4e86\u4e00\u4e2a\u955c\u50cf\u540d\u4e3apobaby/java\u7684\u955c\u50cf\uff0c\u63a5\u4e0b\u6765\u68c0\u9a8c\u4e00\u4e0b\u8be5\u955c\u50cf\u80fd\u5426\u6b63\u5e38\u4f7f\u7528\uff0c\u901a\u8fc7\u8be5\u955c\u50cf\u542f\u52a8\u4e00\u4e2a\u5bb9\u5668\uff0c\u7136\u540e\u770b\u770bjava\u547d\u4ee4\u662f\u5426\u80fd\u6267\u884c\u3002
2\u3001\u521b\u5efaresin\u955c\u50cf
\u8fd9\u91cc\u4ee5\u4f7f\u7528\u7684webserver\u662fresin\uff0c\u6ca1\u6709\u4f7f\u7528tomcat\uff0c\u4ee5\u6b64\u8bf4\u660e\uff1a
\u4f7f\u7528\u521a\u624d\u521b\u5efa\u7684pobaby/java\u7684\u955c\u50cf\u4e3a\u57fa\u7840\u955c\u50cf\u6765\u521b\u5efaresin\u955c\u50cf
# resin# version 1.0FROM pobaby/javaMAINTAINER mhy "[email protected]"RUN apt-get updateADD resin-3.1.14.tar.gz /opt/WORKDIR /opt/RUN mv resin-3.1.14 resinEXPOSE 8080ENTRYPOINT ["/opt/resin/bin/httpd.sh"]\u5173\u4e8e\u8fd9\u4e2aDockerfile\u4e2d\u6709\u4e00\u884c
ADD resin-3.1.14.tar.gz /opt/\u662f\u628a\u5f53\u524d\u76ee\u5f55\u4e0b\u7684resin-3.1.14.tar.gz\u6dfb\u52a0\u5230\u5bb9\u5668\u7684/opt/\u76ee\u5f55\u4e0b\u5e76\u89e3\u538b\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u6307\u5b9a\u4ece\u7f51\u7edc\u4e0a\u4e0b\u8f7d
\u63a5\u4e0b\u6765\u518d\u751f\u6210\u4e00\u4e2a\u5305\u542bresin\u7684\u955c\u50cf
$ sudo docker build -t pobaby/resin .
\u8fd9\u91cc\u53c8\u751f\u6210\u4e86\u4e00\u4e2a\u540d\u4e3apobaby/resin\u7684\u955c\u50cf\u3002\u63a5\u4e0b\u6765\u901a\u8fc7\u8be5\u955c\u50cf\u542f\u52a8\u4e00\u4e2a\u5bb9\u5668\uff0c\u89c2\u5bdf\u4e00\u4e0b\u73af\u5883\u662f\u5426\u6b63\u5e38\u3002
$ sudo docker run -d -p 80:8080 --name web001 pobaby/resin
3\u3001\u63a5\u4e0b\u6765\u8bbf\u95ee\u4e00\u4e0b\u8be5\u5bb9\u5668\uff0c\u628a\u672c\u673a80\u7aef\u53e3\u6620\u5c04\u5230\u5bb9\u5668\u76848080\u7aef\u53e3\u4e0a
4\u3001\u542f\u52a8Web\u9879\u76ee
\u521b\u5efa\u4e00\u4e2a\u5305\u542bWebServer\u7684\u57fa\u672c\u955c\u50cf\uff0c\u7136\u540e\u542f\u52a8\u4e00\u4e2a\u5bb9\u5668\uff0c\u5728\u542f\u52a8\u5bb9\u5668\u7684\u65f6\u5019\u6302\u8f7d\u4e0a\u672c\u5730\u7684web\u5e94\u7528\uff0c\u5177\u4f53\u5982\u4e0b\uff1a
$ sudo docker run -d -p 8881:8080 --name web-demo -v $PWD/webapps:/opt/resin/webapps pobaby/resin
\u4ee5\u4e0a\u542f\u52a8\u4e86\u4e00\u4e2a\u540d\u4e3aweb-demo\u5bb9\u5668\uff0c\u5e76\u628awebapps\u4e0b\u7684\u9879\u76eeweb-demo\u6302\u8f7d\u5230\u5bb9\u5668/opt/resin/webapps\u4e0b
\u8bbf\u95ee\u7ed3\u679c\u5982\u4e0b\uff1a
\u4ee5\u4e0a\u5df2\u7ecf\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u57fa\u4e8eDocker\u5bb9\u5668\u8fd0\u884c\u7684Java Web\u7a0b\u5e8f\u3002
[root@localhost ~]# docker run -i -t Ubuntu /bin/bash
Unable to find image ‘ubuntu’ locally
Pulling repository ubuntu
04c5d3b7b065: Download complete
511136ea3c5a: Download complete
c7b7c6419568: Download complete
70c8faa62a44: Download complete
d735006ad9c1: Download complete
Status: Downloaded newer image for ubuntu:latest
注:
-i:打开容器中的STDIN
-t:为容器分配一个伪tty终端
从上面我们可以看出,首先Docker会检查本地是否存在ubuntu镜像,如果在本地没有找到该镜像的话,那么Docker就会去官方的
Docker Hub Registry查看Docker
Hub中是否有该镜像。Docker一旦找到该镜像,就会下载该镜像并将其保存到本地的宿主机中。
然后,Docker在文件系统内部用这个镜像创建了一个新的容器。该容器拥有自己的网络、IP地址,以及一个用来可以和宿主机进行通信的桥接网络接口。最后,我们告诉Docker在新容器中要运行什么命令。
当容器创建完毕之后,Docker就会执行容器中的/bin/bash命令。这时间我们就可以看到容器内的shell
root@8c342c0c275c:/#
注:8c342c0c275c代表容器的ID
2.使用容器
查看该容器的主机名
root@8c342c0c275c:/# hostname
8c342c0c275c
可以看到,容器的主机名就是该容器的ID
查看该主机的hosts文件
root@8c342c0c275c:/# cat /etc/hosts
172.17.0.2 8c342c0c275c
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
可以看到Docker为该容器的IP地址添加了一条主机配置项。
查看该容器的IP地址
root@8c342c0c275c:/# ip a
1: lo: 《LOOPBACK,UP,LOWER_UP》 mtu 65536 qdisc noqueue state UNKNOWN group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
4: eth0: 《BROADCAST,UP,LOWER_UP》 mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:2/64 scope link
valid_lft forever preferred_lft forever
查看容器中运行的进程
root@8c342c0c275c:/# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 18168 1976 ? Ss 09:38 0:00 /bin/bash
root 19 0.0 0.0 15568 1144 ? R+ 09:55 0:00 ps -aux
在容器中安装一个软件包
root@8c342c0c275c:/# apt-get update && apt-get install vim
Ign http://archive.ubuntu.com trusty InRelease
Ign http://archive.ubuntu.com trusty-updates InRelease
Ign http://archive.ubuntu.com trusty-security InRelease
Hit http://archive.ubuntu.com trusty Release.gpg
Get:1 http://archive.ubuntu.com trusty-updates Release.gpg [933 B]
Get:2 http://archive.ubuntu.com trusty-security Release.gpg [933 B]
Hit http://archive.ubuntu.com trusty Release
Get:3 http://archive.ubuntu.com trusty-updates Release [62.0 kB]
Get:4 http://archive.ubuntu.com trusty-security Release [62.0 kB]
Get:5 http://archive.ubuntu.com trusty/main Sources [1335 kB]
Get:6 http://archive.ubuntu.com trusty/restricted Sources [5335 B]
Get:7 http://archive.ubuntu.com trusty/universe Sources [7926 kB]
Get:8 http://archive.ubuntu.com trusty/main amd64 Packages [1743 kB]
Get:9 http://archive.ubuntu.com trusty/restricted amd64 Packages [16.0 kB]
Get:10 http://archive.ubuntu.com trusty/universe amd64 Packages [7589 kB]
Get:11 http://archive.ubuntu.com trusty-updates/main Sources [193 kB]
Get:12 http://archive.ubuntu.com trusty-updates/restricted Sources [1874 B]
Get:13 http://archive.ubuntu.com trusty-updates/universe Sources [119 kB]
Get:14 http://archive.ubuntu.com trusty-updates/main amd64 Packages [493 kB]
Get:15 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [14.8 kB]
Get:16 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [298 kB]
Get:17 http://archive.ubuntu.com trusty-security/main Sources [70.1 kB]
Get:18 http://archive.ubuntu.com trusty-security/restricted Sources [1874 B]
Get:19 http://archive.ubuntu.com trusty-security/universe Sources [19.1 kB]
Get:20 http://archive.ubuntu.com trusty-security/main amd64 Packages [229 kB]
Get:21 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [14.8 kB]
Get:22 http://archive.ubuntu.com trusty-security/universe amd64 Packages [98.1 kB]
Fetched 20.3 MB in 41s (490 kB/s)
Reading package lists.。。 Done
Reading package lists.。。 Done
Building dependency tree
Reading state information.。。 Done
The following extra packages will be installed:
libgpm2 libpython2.7 libpython2.7-minimal libpython2.7-stdlib vim-runtime
Suggested packages:
gpm ctags vim-doc vim-scripts
The following NEW packages will be installed:
libgpm2 libpython2.7 libpython2.7-minimal libpython2.7-stdlib vim
vim-runtime
0 upgraded, 6 newly installed, 0 to remove and 2 not upgraded.
Need to get 9083 kB of archives.
After this operation, 42.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libgpm2 amd64 1.20.4-6.1 [16.5 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/main libpython2.7-minimal amd64 2.7.6-8 [307 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/main libpython2.7-stdlib amd64 2.7.6-8 [1872 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libpython2.7 amd64 2.7.6-8 [1044 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ trusty/main vim-runtime all 2:7.4.052-1ubuntu3 [4888 kB]
Get:6 http://archive.ubuntu.com/ubuntu/ trusty/main vim amd64 2:7.4.052-1ubuntu3 [956 kB]
Fetched 9083 kB in 51s (175 kB/s)
Selecting previously unselected package libgpm2:amd64.
(Reading database 。。。 11527 files and directories currently installed.)
Preparing to unpack 。。。/libgpm2_1.20.4-6.1_amd64.deb 。。。
Unpacking libgpm2:amd64 (1.20.4-6.1) 。。。
Selecting previously unselected package libpython2.7-minimal:amd64.
Preparing to unpack 。。。/libpython2.7-minimal_2.7.6-8_amd64.deb 。。。
Unpacking libpython2.7-minimal:amd64 (2.7.6-8) 。。。
Selecting previously unselected package libpython2.7-stdlib:amd64.
Preparing to unpack 。。。/libpython2.7-stdlib_2.7.6-8_amd64.deb 。。。
Unpacking libpython2.7-stdlib:amd64 (2.7.6-8) 。。。
Selecting previously unselected package libpython2.7:amd64.
Preparing to unpack 。。。/libpython2.7_2.7.6-8_amd64.deb 。。。
Unpacking libpython2.7:amd64 (2.7.6-8) 。。。
Selecting previously unselected package vim-runtime.
Preparing to unpack 。。。/vim-runtime_2%3a7.4.052-1ubuntu3_all.deb 。。。
Adding ‘diversion of /usr/share/vim/vim74/doc/help.txt to /usr/share/vim/vim74/doc/help.txt.vim-tiny by vim-runtime’
Adding ‘diversion of /usr/share/vim/vim74/doc/tags to /usr/share/vim/vim74/doc/tags.vim-tiny by vim-runtime’
Unpacking vim-runtime (2:7.4.052-1ubuntu3) 。。。
Selecting previously unselected package vim.
Preparing to unpack 。。。/vim_2%3a7.4.052-1ubuntu3_amd64.deb 。。。
Unpacking vim (2:7.4.052-1ubuntu3) 。。。
Setting up libgpm2:amd64 (1.20.4-6.1) 。。。
Setting up libpython2.7-minimal:amd64 (2.7.6-8) 。。。
Setting up libpython2.7-stdlib:amd64 (2.7.6-8) 。。。
Setting up libpython2.7:amd64 (2.7.6-8) 。。。
Setting up vim-runtime (2:7.4.052-1ubuntu3) 。。。
Processing /usr/share/vim/addons/doc
Setting up vim (2:7.4.052-1ubuntu3) 。。。
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
Processing triggers for libc-bin (2.19-0ubuntu6.4) 。。。
退出容器
root@8c342c0c275c:/# exit
exit
当我们输入exit后,容器就停止工作了。只有在指定的/bin/bash命令处于运行状态的时间,容器才会相应地处于运行状态。一旦退出容器,/bin/bash命令也就结束了,这时容器也就停止了。
查看系统中容器的列表
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8c342c0c275c ubuntu:latest “/bin/bash” 26 minutes ago Exited (0) 5 minutes ago sharp_bohr
8c342c0c275c:代表容器的ID,
ubuntu:latest :创建容器的镜像
“/bin/bash” :容器最后执行的命令
26 minutes ago :创建时间
Exited (0) 5 minutes :容器退出的状态
sharp_bohr :容器的名称
Docker中容器的命名
Docker在创建容器时会自动为容器生成一个随机的名称。那么如果我们想在创建一个容器时指定该容器的名称可以使用如下命令:
[root@localhost ~]# docker run --name ovcer_the_container -i -t ubuntu /bin/bash
root@1ce9f640478d:/#
上面的命令将会创建一个名为ovcer_the_container的容器。对于一个合法的容器的名称来说只可以包括以下字符:小写字母a~z、大写字母A-Z、数字0~9、下划线、圆点、横线。
绛旓細-i:鎵撳紑瀹瑰櫒涓殑STDIN -t:涓哄鍣ㄥ垎閰嶄竴涓吉tty缁堢 浠庝笂闈㈡垜浠彲浠ョ湅鍑,棣栧厛Docker浼氭鏌ユ湰鍦版槸鍚﹀瓨鍦╱buntu闀滃儚,濡傛灉鍦ㄦ湰鍦版病鏈夋壘鍒拌闀滃儚鐨勮瘽,閭d箞Docker灏变細鍘诲畼鏂圭殑Docker Hub Registry鏌ョ湅Docker Hub涓槸鍚︽湁璇ラ暅鍍忋侱ocker涓鏃︽壘鍒拌闀滃儚,灏变細涓嬭浇璇ラ暅鍍忓苟灏嗗叾淇濆瓨鍒版湰鍦扮殑瀹夸富鏈轰腑銆 鐒跺悗,Docker鍦ㄦ枃浠剁郴缁熷唴閮...
绛旓細涓銆佷娇鐢╠ocker attach杩涘叆Docker瀹瑰櫒 Docker鎻愪緵浜哸ttach鍛戒护鏉ヨ繘鍏ocker瀹瑰櫒銆傛帴涓嬫潵鎴戜滑鍒涘缓涓涓畧鎶ゆ佺殑Docker瀹瑰櫒锛岀劧鍚庝娇鐢╠ocker attach鍛戒护杩涘叆璇ュ鍣ㄣ俒plain] view plain copy sudo docker run -itd ubuntu:14.04 /bin/bash 鐒跺悗鎴戜滑浣跨敤docker ps鏌ョ湅鍒拌瀹瑰櫒淇℃伅锛屾帴涓嬫潵灏变娇鐢╠ocker attach杩涘叆...
绛旓細鎮ㄥ彲浠ヤ娇鐢ㄧ涓夋柟鎴愮啛鐨凴璇█鐨凞ocker闀滃儚鏉ヨ繍琛孯绋嬪簭锛屼緥濡俽hub/r-minimal銆乺ocker/r-base鎴杛ocker/r-ubuntu绛 銆
绛旓細Windows涓嶮AC OS鐩稿悓锛屼篃闇瑕佸畨瑁匓oot2Docker宸ュ叿銆傚畨瑁呮枃浠跺彲浠ュ湪https://github.com/boot2docker/windows-installer/releases/latest鑾峰緱銆俉indows鐗堢殑Boot2Docker鍦ㄥ惎鍔ㄦ椂浼氳嚜鍔ㄧ‘璁ょ幆澧冨彉閲忥紝鍥犳鍙互鐩存帴楠岃瘉瀹夎鏄惁鎴愬姛锛$ dockerrun hello-world銆
绛旓細涓慨鏀逛竴涓嬫墽琛岀殑璇彞鍗冲彲锛屽涓嬪浘鎵绀猴細鍏跺疄灏辨槸娣诲姞鍚姩鏃堕渶瑕佸姞杞界殑閰嶇疆鏂囦欢鍗冲彲锛堥粯璁ゆ槸娌℃湁娣诲姞浠讳綍鍙傛暟鐨勶級銆傝嚦姝わ紝閰嶇疆灏卞叏閮ㄥ畬姣曚簡锛岀偣搴旂敤鍚庯紝灏卞彲浠ュ惎鍔ㄤ簡銆傚鏋滆淇敼绔彛涔嬬被鐨勶紝鍦 楂樼骇璁剧疆 涓紝鑷淇敼灏 OK 浜嗐傞檮涓 Redis 瀹樻柟璇存槑锛 https://hub.docker.com/r/_/redis/ ...
绛旓細1銆佹洿鏂癠buntu鍐呮牳 浣跨敤濡備笅鍛戒护琛屾洿鏂板唴鏍歌嚦3.8.0-25 sudo apt-get install linux-image-3.8.0-25-generic sudo apt-get install linux-headers-3.8.0-25-generic 瀹屾垚鍚庨噸鍚數鑴戯紝閫氳繃鍛戒护 鈥渦name -r鈥 鏉ユ煡鐪嬪唴鏍告槸鍚︽垚鍔熸洿鏂般2銆佸畨瑁卨xc-docker root@ubuntu: sudo apt-get install ...
绛旓細锛1銆佺櫥褰曞埌缇ゆ櫀nas锛屾墦寮dockerr濂椾欢涓績锛岀偣鍑烩滃畨瑁呭浠垛濄2銆佸湪鎼滅储妗嗕腑杈撳叆鈥渃louddrive2鈥濓紝鎵惧埌骞剁偣鍑烩滃畨瑁呭浠垛濊繘琛屽畨瑁呫3銆佸湪鈥滆缃濈獥鍙d腑锛岃缃鍣ㄧ殑绔彛銆佹暟鎹嵎绛夊弬鏁帮紝鏍规嵁闇瑕佽繘琛岄厤缃紝鐒跺悗鐐瑰嚮鈥滃畬鎴愨濇寜閽4銆佺瓑寰呬竴娈垫椂闂村悗锛宒ocker瀹瑰櫒灏变細鍚姩骞惰繍琛宑louddrive2銆
绛旓細1. 浣跨敤鍛戒护鎻愮ず绗:- 鎸変笅 `Win + R` 鎵撳紑鈥滆繍琛屸濆璇濇銆- 杈撳叆 `cmd` 鎴栬 `powershell` 骞舵寜鍥炶溅閿傝繖灏嗘墦寮涓涓柊鐨勫懡浠ゆ彁绀虹鎴朠owerShell绐楀彛銆- 鍦ㄦ墦寮鐨勭獥鍙d腑杈撳叆浣犳兂瑕佽繍琛岀殑绋嬪簭鍚嶇О鎴栬矾寰勶紝渚嬪 `notepad` 鎴栬 `C:\path\to\your\application.exe`銆2. 浣跨敤浠诲姟绠$悊鍣 (瀵逛簬...
绛旓細鍛婅瘔 Docker 鏈嶅姟绔鍣ㄦ毚闇茬殑绔彛鍙凤紝渚涗簰鑱旂郴缁熶娇鐢ㄣ傚湪鍚姩瀹瑰櫒鏃堕渶瑕侀氳繃 -P锛孌ocker 涓绘満浼氳嚜鍔ㄥ垎閰嶄竴涓鍙h浆鍙戝埌鎸囧畾鐨勭鍙c侲NV:鏍煎紡涓 ENV 銆 鎸囧畾涓涓幆澧冨彉閲忥紝浼氳鍚庣画 RUN 鎸囦护浣跨敤锛屽苟鍦ㄥ鍣ㄨ繍琛屾椂淇濇寔銆傝繖灏卞搴绋嬪簭璇█涓殑鍙橀噺瀹氫箟锛屽彲鍦ㄩ渶瑕佺殑鏃跺欏紩鐢ㄣ備緥濡傦細1 2 3 4 ENV PG_...
绛旓細docker info 鏌ョ湅绯荤粺锛坉ocker锛夊眰闈俊鎭紝鍖呮嫭绠$悊鐨刬mages锛 containers鏁扮瓑 docker search 鍦╠ocker index涓悳绱mage docker pull 浠docker registry server 涓笅鎷塱mage docker push 鎺ㄩ佷竴涓猧mage鎴杛epository鍒皉egistry docker push 锛歍AG 鍚屼笂锛屾寚瀹歵ag docker inspect 鏌ョ湅image鎴朿ontainer鐨勫簳灞備俊鎭 dock...