永遠十八歲 2021-08-15 20:52:08 阅读数:771
示例 只修改一個鏡像的FROM、LABEL
[[email protected] ~]# mkdir image
[[email protected] ~]# cd image
複制代碼
[[email protected] image]# vim Dockerfile
#Test Image Build
FROM alpine
LABEL maintainer="lixinkuan <[email protected]>"
複制代碼
[[email protected] image]# docker build .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM alpine
---> 3fd9065eaf02
Step 2/2 : LABEL maintainer="lixinkuan <[email protected]>"
---> Running in e1ce9acfc453
Removing intermediate container e1ce9acfc453
---> 1deb17a1af32
Successfully built 1deb17a1af32
複制代碼
[[email protected] image]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 1deb17a1af32 5 minutes ago 4.15MB
nginx latest cd5239a0906a 3 weeks ago 109MB
busybox latest 8c811b4aec35 5 weeks ago 1.15MB
httpd 2.4 fb2f3851a971 8 weeks ago 178MB
alpine latest 3fd9065eaf02 5 months ago 4.15MB
複制代碼
[[email protected] image]# docker image tag 1deb17a1af32 alpine:lxk
[[email protected] image]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine lxk 1deb17a1af32 5 minutes ago 4.41MB
複制代碼
示例:複制單個文件
[[email protected] image]# echo '<h1>hello,docker!</h1>' > index.html
複制代碼
[[email protected] image]# vim Dockerfile
#Test Image Build
FROM alpine
LABEL maintainer="lixinkuan <[email protected]>"
COPY index.html /var/www/html/
複制代碼
[[email protected] image]# docker run -it --name a1 alpine
/ # ps aux
PID USER TIME COMMAND
1 root 0:00 /bin/sh
7 root 0:00 ps aux
/ # ls /var/www/html
ls: /var/www/html: No such file or directory
複制代碼
[[email protected] image]# docker build -t cpindex:latest .
Sending build context to Docker daemon 3.072kB
Step 1/3 : FROM alpine
---> 3fd9065eaf02
Step 2/3 : LABEL maintainer="lixinkuan <[email protected]>"
---> Running in 9d96e0655a82
Removing intermediate container 9d96e0655a82
---> 56049399eb78
Step 3/3 : COPY index.html /var/www/html/
---> bace8e55c97b
Successfully built bace8e55c97b
Successfully tagged cpindex:latest
複制代碼
[[email protected] image]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
cpindex latest bace8e55c97b 52 seconds ago 4.15MB
複制代碼
[[email protected] image]# docker run --name copyfile -it --rm cpindex:latest
/ # ls /var/www/html
index.html
/ # cat /var/www/html/index.html
<h1>hello,docker!</h1>
複制代碼
示例:複制目錄下的多個文件至目錄
[[email protected] image]# cp -r /etc/default/ ./
[[email protected] image]# ls
default Dockerfile index.html
[[email protected] image]# ls default/
grub kibana nss useradd
複制代碼
#Test Image Build
FROM alpine
LABEL maintainer="lixinkuan <[email protected]>"
COPY default /tmp/
複制代碼
[[email protected] image]# docker build -t cpdir:latest ./
Sending build context to Docker daemon 9.216kB
Step 1/3 : FROM alpine
---> 3fd9065eaf02
Step 2/3 : LABEL maintainer="lixinkuan <[email protected]>"
---> Using cache
---> 56049399eb78
Step 3/3 : COPY default /tmp/
---> 18cacf50aef9
Successfully built 18cacf50aef9
Successfully tagged cpdir:latest
複制代碼
[[email protected] image]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
cpdir latest 18cacf50aef9 24 seconds ago 4.15MB
[[email protected] image]# docker run --name cpdir -it --rm cpdir:latest
WARNING: IPv4 forwarding is disabled. Networking will not work.
/ # ls /tmp
grub kibana nss useradd
複制代碼
示例:使用數組格式創建配置文件
#Test Image Build
FROM alpine
LABEL maintainer="lixinkuan <[email protected]>"
COPY ["default","/tmp/default"]
複制代碼
[[email protected] image]# docker build -t cp:latest ./
Sending build context to Docker daemon 9.216kB
Step 1/3 : FROM alpine
---> 3fd9065eaf02
Step 2/3 : LABEL maintainer="lixinkuan <[email protected]>"
---> Using cache
---> 56049399eb78
Step 3/3 : COPY ["default","/tmp/default"]
---> bf0799319943
Successfully built bf0799319943
Successfully tagged cp:latest
[[email protected] image]# docker run --name cp -it --rm cp:latest
/ # cd /tmp
/tmp # ls
default
/tmp # cd default/
/tmp/default # ls
grub kibana nss useradd
/tmp/default # exit
複制代碼
示例:下載一個文件至鏡像文件
#Test Image Build
FROM alpine
LABEL maintainer="lixinkuan <[email protected]>"
COPY ["default","/tmp/default"]
ADD https://mirrors.aliyun.com/centos/7.5.1804/os/x86_64/Packages/zsh-5.0.2-28.el7.x86_64.rpm /tmp/
複制代碼
[[email protected] image]# docker build -t zsh:latest ./
Sending build context to Docker daemon 9.216kB
Step 1/4 : FROM alpine
---> 3fd9065eaf02
Step 2/4 : LABEL maintainer="lixinkuan <[email protected]>"
---> Using cache
---> 56049399eb78
Step 3/4 : COPY ["default","/tmp/default"]
---> Using cache
---> bf0799319943
Step 4/4 : ADD https://mirrors.aliyun.com/centos/7.5.1804/os/x86_64/Packages/zsh-5.0.2-28.el7.x86_64.rpm /tmp/
Downloading [==================================================>] 2.494MB/2.494MB
---> 538ab9c6983e
Successfully built 538ab9c6983e
Successfully tagged zsh:latest
複制代碼
[[email protected] image]# docker run -it --name zsh --rm zsh:latest
/ # cd /tmp
/tmp # ls
default zsh-5.0.2-28.el7.x86_64.rpm
複制代碼
示例:ADD一個壓縮包至鏡像文件
[[email protected] image]# cp /root/wordpress-4.8.1-zh_CN.tar.gz ./
[[email protected] image]# vim Dockerfile
#Test Image Build
FROM alpine
LABEL maintainer="lixinkuan <[email protected]>"
ADD wordpress-4.8.1-zh_CN.tar.gz /tmp/
複制代碼
[[email protected] image]# docker build -t wordpress:latest ./
Sending build context to Docker daemon 8.652MB
Step 1/3 : FROM alpine
---> 3fd9065eaf02
Step 2/3 : LABEL maintainer="lixinkuan <[email protected]>"
---> Using cache
---> 56049399eb78
Step 3/3 : ADD wordpress-4.8.1-zh_CN.tar.gz /tmp/
---> 58c32caba31e
Successfully built 58c32caba31e
Successfully tagged wordpress:latest
複制代碼
[[email protected] image]# docker run --name a1 -it --rm wordpress:latest
/ # ls /tmp/wordpress/
index.php wp-admin wp-content wp-load.php wp-signup.php
license.txt wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php
readme.html wp-comments-post.php wp-includes wp-mail.php xmlrpc.php
wp-activate.php wp-config-sample.php wp-links-opml.php wp-settings.php
/ # exit
複制代碼
示例
#Test Image Build
FROM alpine
LABEL maintainer="lixinkuan <[email protected]>"
WORKDIR /tmp
ADD wordpress-4.8.1-zh_CN.tar.gz src
複制代碼
[[email protected] image]# docker build -t wordpress:v0.1 ./
Sending build context to Docker daemon 8.652MB
Step 1/4 : FROM alpine
---> 3fd9065eaf02
Step 2/4 : LABEL maintainer="lixinkuan <[email protected]>"
---> Using cache
---> 56049399eb78
Step 3/4 : WORKDIR /tmp
Removing intermediate container 08bef4630472
---> 54f97eda6b49
Step 4/4 : ADD wordpress-4.8.1-zh_CN.tar.gz src
---> 0811aff4fa7d
Successfully built 0811aff4fa7d
Successfully tagged wordpress:v0.1
複制代碼
[[email protected] image]# docker run --name a1 -it --rm wordpress:v0.1
/tmp # ls
src
/tmp # ls src/
wordpress
/tmp # ls src/wordpress/
index.php wp-admin wp-content wp-load.php wp-signup.php
license.txt wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php
readme.html wp-comments-post.php wp-includes wp-mail.php xmlrpc.php
wp-activate.php wp-config-sample.php wp-links-opml.php wp-settings.php
/tmp # exit
複制代碼
示例
[[email protected] image]# echo "hello,test dockerfile" > /var/www/html/index.html
複制代碼
#Test Image Build
FROM alpine
LABEL maintainer="lixinkuan <[email protected]>"
#WORKDIR /tmp
#ADD wordpress-4.8.1-zh_CN.tar.gz src/
VOLUME /var/www/html
複制代碼
[[email protected] image]# docker build -t file:v0.1 ./
Sending build context to Docker daemon 8.651MB
Step 1/3 : FROM alpine
---> 3fd9065eaf02
Step 2/3 : LABEL maintainer="lixinkuan <[email protected]>"
---> Using cache
---> 56049399eb78
Step 3/3 : VOLUME /var/www/html
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in da84d9f4ca1e
Removing intermediate container da84d9f4ca1e
---> b26c2d7ea64c
Successfully built b26c2d7ea64c
Successfully tagged file:v0.1
複制代碼
[[email protected] image]# docker run --name a1 -it --rm file
/ # cd /var/www/html/
/var/www/html # echo abc > index.html
/var/www/html # cat index.html
abc
複制代碼
[[email protected] ~]# docker volume ls #查看本機所有容器掛載的目錄
DRIVER VOLUME NAME
local 6368d0a0b462f5329a4b3bdcb7030e0d6f724bf9f801386f87fdac7660cd1735
[[email protected] ~]# docker inspect -f {{.Mounts}} a1 #查看a1容器的掛載文件路徑
[{volume 6368d0a0b462f5329a4b3bdcb7030e0d6f724bf9f801386f87fdac7660cd1735 /var/lib/docker/volumes/6368d0a0b462f5329a4b3bdcb7030e0d6f724bf9f801386f87fdac7660cd1735/_data /var/www/html local true }]
[[email protected] ~]# cd /var/lib/docker/volumes/6368d0a0b462f5329a4b3bdcb7030e0d6f724bf9f801386f87fdac7660cd1735/_data/
[[email protected] _data]# ls
index.html
[[email protected] _data]# cat index.html
abc #該內容與容器中index.html內容一樣
複制代碼
不用EXPOSE時,宿主機內容器若要被外網主機訪問的情况
[[email protected] ~]# docker pull redis:4-alpine
4-alpine: Pulling from library/redis
ff3a5c916c92: Pull complete
5fbab8756652: Pull complete
ff7d4663b06c: Pull complete
0b5cf71258c2: Pull complete
54bbb9bad8ba: Pull complete
8fe9a341d124: Pull complete
Digest: sha256:686ab026fae07b3b99a8e74210c361714a80311ecc55f23b349ae930ed2f5a95
Status: Downloaded newer image for redis:4-alpine
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 4-alpine caaeda72bf8f 12 days ago 27.8MB
複制代碼
[[email protected] ~]# docker run --name db1 -d --rm -p 6379 redis:4-alpine
881d5648c7388449a39c67024206c5710b1538f4c941039fa3905bb601b09699
[[email protected] ~]# docker exec -it db1 ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:8 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:648 (648.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
[[email protected] ~]# docker exec -it db1 /bin/sh
/data # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN
tcp 0 0 :::6379 :::* LISTEN
/data #
複制代碼
[[email protected] ~]# docker container port db1
6379/tcp -> 0.0.0.0:32768
複制代碼
[[email protected] tmp]# redis-cli -h 192.168.1.106 -p 32768
192.168.1.106:32768> select 1
OK
192.168.1.106:32768[1]> set mykey hi
OK
192.168.1.106:32768[1]> keys *
1) "mykey"
192.168.1.106:32768[1]> exit
複制代碼
[[email protected] ~]# docker exec -it db1 /bin/sh
/data # redis-cli
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
1) "mykey"
127.0.0.1:6379[1]> exit
/data # exit
複制代碼
開啟自動端口暴露
#Test Image Build
FROM redis:4-alpine
LABEL maintainer="lixinkuan <[email protected]>"
EXPOSE 6379/tcp 26379/tcp
複制代碼
[[email protected] images]# docker build -t expose_db:latest ./
Sending build context to Docker daemon 8.645MB
Step 1/3 : FROM redis:4-alpine
---> caaeda72bf8f
Step 2/3 : LABEL maintainer="lixinkuan <[email protected]>"
---> Running in f43f9e43b27a
Removing intermediate container f43f9e43b27a
---> e98bb940a8a2
Step 3/3 : EXPOSE 6379/tcp 26379/tcp
---> Running in f53a9be4f661
Removing intermediate container f53a9be4f661
---> ea40417716a0
Successfully built ea40417716a0
Successfully tagged expose_db:latest
複制代碼
[[email protected] images]# docker run --name a1 -d --rm -P redis_expose:latest
ad1225390f8f246cc5bde693ea99b120ee3a2f474416603b0797cda94787cc03
[[email protected] images]# docker container port a1
6379/tcp -> 0.0.0.0:32772
複制代碼
[[email protected] ~]# redis-cli -h 192.168.200.45 -p 32772
192.168.200.45:32772> select 1
OK
192.168.200.45:32772[1]> keys *
(empty list or set)
192.168.200.45:32772[1]> set test dockerfile
OK
192.168.200.45:32772[1]> keys *
1) "test"
192.168.200.45:32772[1]> get test
"dockerfile"
複制代碼
[[email protected] images]# docker exec -it a1 /bin/sh
/data # redis-cli
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get test
"dockerfile"
複制代碼
示例:驗證Dockerfile與docker run時使用-p的優先級
[[email protected] images]# cat Dockerfile
#Test Image Build
FROM redis:4-alpine
LABEL maintainer="lixinkuan <[email protected]>"
EXPOSE 6379/tcp 80/tcp
複制代碼
[[email protected] images]# docker build -t expose_port .
Sending build context to Docker daemon 8.645MB
Step 1/3 : FROM redis:4-alpine
---> caaeda72bf8f
Step 2/3 : LABEL maintainer="lixinkuan <[email protected]>"
---> Using cache
---> 188775dd2e3e
Step 3/3 : EXPOSE 6379/tcp 80/tcp
---> Running in b0f5bfbaafae
Removing intermediate container b0f5bfbaafae
---> 165e707c2b23
Successfully built 165e707c2b23
Successfully tagged expose_port:latest
複制代碼
[[email protected] images]# docker run --name db1 -d --rm -p 25 expose_port
e00f3e304103954c00651d44b00ae9961608900e0d5688eee4c08f140340f480
[[email protected] images]# docker container port db1
25/tcp -> 0.0.0.0:32779
複制代碼
[[email protected] images]# iptables -t nat -nvL
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:32779 to:172.17.0.2:25
複制代碼
示例
FROM busybox
LABEL maintainer="lixinkuan <[email protected]>"
ENV DOCROOT="/data/web/html/"
COPY index.html ${DOCROOT}
VOLUME ${DOCROOT}
複制代碼
[[email protected] bbox]# mkdir -pv /data/web/html
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/web’
mkdir: created directory ‘/data/web/html’
[[email protected] bbox]# echo hello Docker > index.html
[[email protected] bbox]# cat index.html
hello Docker
複制代碼
[[email protected] bbox]# docker build -t bbox_file:latest ./
Sending build context to Docker daemon 3.072kB
Step 1/5 : FROM busybox
latest: Pulling from library/busybox
07a152489297: Pull complete
Digest: sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
Status: Downloaded newer image for busybox:latest
---> 8c811b4aec35
Step 2/5 : LABEL maintainer="lixinkuan <[email protected]>"
---> Running in 87a1f2c22ad6
Removing intermediate container 87a1f2c22ad6
---> 56f723d6220c
Step 3/5 : ENV DOCROOT="/data/web/html/"
---> Running in 21fd1fcb0474
Removing intermediate container 21fd1fcb0474
---> c095f8dd8418
Step 4/5 : COPY index.html ${DOCROOT}
---> ee77cd16629a
Step 5/5 : VOLUME ${DOCROOT}
---> Running in 00474fde8b85
Removing intermediate container 00474fde8b85
---> d51ea735fdd3
Successfully built d51ea735fdd3
Successfully tagged bbox_file:latest
複制代碼
[[email protected] bbox]# docker run --name a1 -it --rm bbox_file:latest
/ # ls /data/web/html
index.html
/ # cat /data/web/html/index.html
hello Docker
複制代碼
[[email protected] ~]# docker volume ls
DRIVER VOLUME NAME
local 1dcd37d2c4f2e6a71e0b96a385714ff01cad5d578e396c9b012922e9993aecbf
local b2df5fcd0e1aa58c403d2e8f0ec880feb7dcb1a80a688697e76122adec55e789
[[email protected] ~]# docker inspect -f {{.Mounts}} a1
[{volume 1dcd37d2c4f2e6a71e0b96a385714ff01cad5d578e396c9b012922e9993aecbf /var/lib/docker/volumes/1dcd37d2c4f2e6a71e0b96a385714ff01cad5d578e396c9b012922e9993aecbf/_data /data/web/html local true }]
複制代碼
示例1:基於centos基礎鏡像創建一個運行nginx的鏡像
FROM centos
LABEL maintainer="lixinkuan <[email protected]>"
COPY base.repo epel.repo /etc/yum.repos.d/
RUN yum -y install nginx \
&& yum clean all \
&& rm -rf /var/cache/yum
複制代碼
[[email protected] nginx]# wget lixinkuan.top/base.repo
--2018-06-30 11:29:08-- http://lixinkuan.top/base.repo
Resolving lixinkuan.top (lixinkuan.top)... 47.94.102.99
Connecting to lixinkuan.top (lixinkuan.top)|47.94.102.99|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 630
Saving to: ‘base.repo’
100%[=================================================================================>] 630 --.-K/s in 0s
2018-06-30 11:29:08 (87.9 MB/s) - ‘base.repo’ saved [630/630]
[[email protected] nginx]# wget lixinkuan.top/epel.repo
--2018-06-30 11:29:16-- http://lixinkuan.top/epel.repo
Resolving lixinkuan.top (lixinkuan.top)... 47.94.102.99
Connecting to lixinkuan.top (lixinkuan.top)|47.94.102.99|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 214
Saving to: ‘epel.repo’
100%[=================================================================================>] 214 --.-K/s in 0s
2018-06-30 11:29:16 (44.2 MB/s) - ‘epel.repo’ saved [214/214]
[[email protected] nginx]# ls
base.repo Dockerfile epel.repo
複制代碼
[[email protected] nginx]# docker build -t nginx:v0.1 ./
Sending build context to Docker daemon 4.608kB
Step 1/4 : FROM centos
latest: Pulling from library/centos
7dc0dca2b151: Pull complete
Digest: sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322
Status: Downloaded newer image for centos:latest
---> 49f7960eb7e4
Step 2/4 : LABEL maintainer="lixinkuan <[email protected]>"
---> Running in 6b16128ed7ca
Removing intermediate container 6b16128ed7ca
---> b6ef19a3311f
Step 3/4 : COPY base.repo epel.repo /etc/yum.repos.d/
---> e571c2837442
Step 4/4 : RUN yum -y install nginx && yum clean all && rm -rf /var/cache/yum
---> Running in 445372de8e8d
Loaded plugins: fastestmirror, ovl
.....
執行安裝過程省略
...
Cleaning repos: base epel extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
Removing intermediate container 445372de8e8d
---> 5cf6e8e3517e
Successfully built 5cf6e8e3517e
Successfully tagged nginx:v0.1
複制代碼
[[email protected] nginx]# docker run --name web -it nginx:v0.1
[[email protected] /]# rpm -q nginx
nginx-1.12.2-2.el7.x86_64 #nginx已安裝
[[email protected] /]#
複制代碼
示例2:以busybox制作一個掛載本地/data/web/html目錄並自動運行httpd的鏡像
FROM busybox
LABEL maintainer="lixinkuan <[email protected]>"
ENV DOCROOT="/data/web/html/"
COPY index.html ${DOCROOT}
VOLUME ${DOCROOT}
CMD /bin/httpd -f -h ${DOCROOT}
複制代碼
[[email protected] bbox]# echo hello Docker > index.html
[[email protected] bbox]# cat index.html
hello Docker
複制代碼
[[email protected] bbox]# docker build -t web:v0.1 ./
Sending build context to Docker daemon 3.072kB
Step 1/6 : FROM busybox
---> 8c811b4aec35
Step 2/6 : LABEL maintainer="lixinkuan <[email protected]>"
---> Using cache
---> 56f723d6220c
Step 3/6 : ENV DOCROOT="/data/web/html/"
---> Using cache
---> c095f8dd8418
Step 4/6 : COPY index.html ${DOCROOT}
---> Using cache
---> ee77cd16629a
Step 5/6 : VOLUME ${DOCROOT}
---> Using cache
---> d51ea735fdd3
Step 6/6 : CMD /bin/httpd -f -h ${DOCROOT}
---> Running in f2fa2b284306
Removing intermediate container f2fa2b284306
---> b8613217ad3c
Successfully built b8613217ad3c
Successfully tagged web:v0.1
複制代碼
[[email protected] bbox]# docker run --name web -d --rm web:v0.1
7b71084ebd922728ebf21d22a4e5ff3462443761c82bc22c640764c6d4925b2a
[[email protected] bbox]# docker container inspect -f {{.Config.Cmd}} web
[/bin/sh -c /bin/httpd -f -h ${DOCROOT}]
[[email protected] bbox]# docker exec -it web /bin/sh
/ # ps aux
PID USER TIME COMMAND
1 root 0:00 /bin/httpd -f -h /data/web/html/
7 root 0:00 /bin/sh
13 root 0:00 ps aux
/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :::80 :::* LISTEN
複制代碼
示例3:docker run 時不運行鏡像默認進程,運行指定指令
[[email protected] bbox]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
web v0.1 b8613217ad3c 2 hours ago 1.15MB
複制代碼
[[email protected] bbox]# docker run --name web -it --rm web:v0.1 /bin/sh
/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
/ # ps aux
PID USER TIME COMMAND
1 root 0:00 /bin/sh
7 root 0:00 ps aux
複制代碼
示例1:
FROM busybox
LABEL maintainer="lixinkuan <[email protected]>"
VOLUME /data/web/html/
COPY index.html /data/web/html/
EXPOSE 80/tcp
ENTRYPOINT ["/bin/httpd","-f","-h","/data/web/html"]
複制代碼
[[email protected] bbox]# docker build -t web:v0.2 ./
Sending build context to Docker daemon 3.072kB
Step 1/6 : FROM busybox
---> 8c811b4aec35
Step 2/6 : LABEL maintainer="lixinkuan <[email protected]>"
---> Using cache
---> 56f723d6220c
Step 3/6 : VOLUME /data/web/html/
---> Running in 3095065d0ebb
Removing intermediate container 3095065d0ebb
---> 36dc68fabc6f
Step 4/6 : COPY index.html /data/web/html/
---> e47f81ec7728
Step 5/6 : EXPOSE 80/tcp
---> Running in f86f957ec882
Removing intermediate container f86f957ec882
---> 01a005644fe6
Step 6/6 : ENTRYPOINT ["/bin/httpd","-f","-h","/data/web/html"]
---> Running in 7a5f8b4f4acf
Removing intermediate container 7a5f8b4f4acf
---> 43d514096d34
Successfully built 43d514096d34
Successfully tagged web:v0.2
複制代碼
[[email protected] bbox]# docker exec -it web /bin/sh
/ # ps aux
PID USER TIME COMMAND
1 root 0:00 /bin/httpd -f -h /data/web/html
7 root 0:00 /bin/sh
13 root 0:00 ps aux
/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :::80 :::* LISTEN
/ #
複制代碼
[[email protected] bbox]# docker run --name web -it --rm web:v0.2 /bin/sh
複制代碼
[[email protected] bbox]# docker exec -it web /bin/sh
/ # ps aux
PID USER TIME COMMAND
1 root 0:00 /bin/httpd -f -h /data/web/html /bin/sh
7 root 0:00 /bin/sh
13 root 0:00 ps aux
複制代碼
並未執行/bin/sh,而是執行默認程序,/bin/sh被當作參數傳遞給/bin/httpd
示例:在docker run時使用entrypoint的時候更換默認運行的程序
[[email protected] bbox]# docker run --name web -it --rm --entrypoint /bin/sh web:v0.2
/ # ps aux
PID USER TIME COMMAND
1 root 0:00 /bin/sh
7 root 0:00 ps aux
/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
/ #
複制代碼
示例
FROM busybox
LABEL maintainer="lixinkuan <[email protected]>"
ENV DOCROOT="/data/web/html/" MYPORT="80"
COPY index.html ${DOCROOT}
COPY entrypoint.sh /bin/
COPY test.conf /etc/
VOLUME ${DOCROOT}
EXPOSE 80/tcp
#CMD /bin/httpd -f -h ${DOCROOT}
#CMD ["/bin/sh","-c","/bin/httpd","-f","-h","${DOCROOT}"]
ENTRYPOINT ["/bin/entrypoint.sh"]
CMD ["/bin/httpd","-f","-h","/data/web/html/"]
複制代碼
[[email protected] bbox]# cat entrypoint.sh
#!/bin/sh
sed -i "[email protected]^PORT=.*@PORT=${MYPORT}@g" /etc/test.conf
exec "[email protected]"
[[email protected] bbox]# cat test.conf
PORT=8080
複制代碼
[[email protected] bbox]# docker build -t web:v0.3 ./
Sending build context to Docker daemon 5.12kB
Step 1/10 : FROM busybox
---> 8c811b4aec35
Step 2/10 : LABEL maintainer="lixinkuan <[email protected]>"
---> Using cache
---> 56f723d6220c
Step 3/10 : ENV DOCROOT="/data/web/html/" MYPORT="80"
---> Running in f237100ec645
Removing intermediate container f237100ec645
---> f754b5dcea84
Step 4/10 : COPY index.html ${DOCROOT}
---> 3c31424c9b3d
Step 5/10 : COPY entrypoint.sh /bin/
---> 46ec2f5ede8c
Step 6/10 : COPY test.conf /etc/
---> 7db53e00338a
Step 7/10 : VOLUME ${DOCROOT}
---> Running in 5ae02469f585
Removing intermediate container 5ae02469f585
---> 0e1e3e966318
Step 8/10 : EXPOSE 80/tcp
---> Running in ae76bcf870ca
Removing intermediate container ae76bcf870ca
---> dea89896460d
Step 9/10 : ENTRYPOINT ["/bin/entrypoint.sh"]
---> Running in 6862bf4a336e
Removing intermediate container 6862bf4a336e
---> ca568e1ff983
Step 10/10 : CMD ["/bin/httpd","-f","-h","/data/web/html/"]
---> Running in 2aa5dea11848
Removing intermediate container 2aa5dea11848
---> 26bb44795880
Successfully built 26bb44795880
Successfully tagged web:v0.3
複制代碼
[[email protected] bbox]# docker run --name web -d --rm web:v0.3
6ec1f5a008e6a08047e8666f6ed3ad4673360805148789faf780baf335ee5637
[[email protected] bbox]# docker exec -it web /bin/sh
/ # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :::80 :::* LISTEN
/ # cat /etc/test.conf
PORT=80
/ # exit
複制代碼
示例:通過傳遞變量更改配置文件的方法: [[email protected] bbox]# docker run --name web1 -d --rm -e MYPORT=10080 web:v0.3 7e3b353e423839d598ee9423e881673066cf99626940b6590e78f34b7622834d [[email protected] bbox]# docker exec -it web1 /bin/sh / # cat /etc/test.conf PORT=10080 / #
### 15.
-
複制代碼
版权声明:本文为[永遠十八歲]所创,转载请带上原文链接,感谢。 https://gsmany.com/2021/08/20210815205202522b.html