Docker镜像管理
镜像的结构
img-
docker镜像是一个典型的分层结构
-
只有最上面一层是可写的 其他都是只读的固化到镜像的
-
每次推送都是增量的
镜像名称的结构
${registry_ name}/${repository. name}/${image. name}:${tag. name}
例如:
docker.io/library/alpine:3.10.1
登陆到dokcer.io
[root@alice ~]# docker login docker.io
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: mmdghh
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@alice ~]#
查看已经登陆的信息
[root@alice ~]# cat /root/.docker/config.json
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "5bCP5LiR56uf5Zyo5oiR6Lqr6L65"
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/18.09.5 (linux)"
}
}[root@alice ~]#
注: 此处的密码可以用echo "5bCP5LiR56uf5Zyo5oiR6Lqr6L65" |base64 -d 进行解码
搜索镜像
[root@alice ~]# docker search alpine
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
alpine A minimal Docker image based on Alpine Linux… 7047 [OK]
mhart/alpine-node Minimal Node.js built on Alpine Linux 478
anapsix/alpine-java Oracle Java 8 (and 7) with GLIBC 2.28 over A… 466 [OK]
frolvlad/alpine-glibc Alpine Docker image with glibc (~12MB) 251 [OK]
gliderlabs/alpine Image based on Alpine Linux will help you wi… 183
alpine/git A simple git container running in alpine li… 161 [OK]
mvertes/alpine-mongo light MongoDB container 117 [OK]
yobasystems/alpine-mariadb MariaDB running on Alpine Linux [docker] [am… 79 [OK]
alpine/socat Run socat command in alpine container 62 [OK]
kiasaki/alpine-postgres PostgreSQL docker image based on Alpine Linux 45 [OK]
davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 with… 43 [OK]
jfloff/alpine-python A small, more complete, Python Docker image … 38 [OK]
byrnedo/alpine-curl Alpine linux with curl installed and set as … 34 [OK]
hermsi/alpine-sshd Dockerize your OpenSSH-server with rsync and… 32 [OK]
zenika/alpine-chrome Chrome running in headless mode in a tiny Al… 27 [OK]
hermsi/alpine-fpm-php FPM-PHP 7.0 to 8.0, shipped along with tons … 25 [OK]
etopian/alpine-php-wordpress Alpine WordPress Nginx PHP-FPM WP-CLI 24 [OK]
bashell/alpine-bash Alpine Linux with /bin/bash as a default she… 18 [OK]
davidcaste/alpine-java-unlimited-jce Oracle Java 8 (and 7) with GLIBC 2.21 over A… 13 [OK]
roribio16/alpine-sqs Dockerized ElasticMQ server + web UI over Al… 11 [OK]
spotify/alpine Alpine image with `bash` and `curl`. 11 [OK]
cfmanteiga/alpine-bash-curl-jq Docker Alpine image with Bash, curl and jq p… 6 [OK]
ellerbrock/alpine-mysql-client MySQL Client based on Alpine Linux 1 [OK]
bushrangers/alpine-caddy Alpine Linux Docker Container running Caddys… 1 [OK]
dwdraju/alpine-curl-jq Alpine Docker Image with curl, jq, bash 0 [OK]
[root@alice ~]#
拉取镜像
如果不指定tag 默认下载最新版本 latest
[root@alice ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
801bfaa63ef2: Pull complete
Digest: sha256:3c7497bf0c7af93428242d6176e8f7905f2201d8fc5861f45be7a346b5f23436
Status: Downloaded newer image for alpine:latest
[root@alice ~]#
也可以指定版本下载
[root@alice ~]# docker pull alpine:3.10.3
3.10.3: Pulling from library/alpine
89d9c30c1d48: Pull complete
Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
Status: Downloaded newer image for alpine:3.10.3
[root@alice ~]# docker pull docker.io/library/alpine:3.10.3
3.10.3: Pulling from library/alpine
Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a
Status: Image is up to date for alpine:3.10.3
[root@alice ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 389fef711851 3 weeks ago 5.58MB
alpine 3.10.3 965ea09ff2eb 14 months ago 5.55MB
[root@alice ~]#
如果使用官方的docker.io 可以不写前面的docker.io/library/ 因为默认就是公开的 如果是自己的或者其他仓库 需要写全
给镜像打tag(标签)
[root@alice ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 389fef711851 3 weeks ago 5.58MB
alpine 3.10.3 965ea09ff2eb 14 months ago 5.55MB
[root@alice ~]# docker tag 965ea09ff2eb docker.io/mmdghh/alpine:v3.10.3
[root@alice ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 389fef711851 3 weeks ago 5.58MB
alpine 3.10.3 965ea09ff2eb 14 months ago 5.55MB
mmdghh/alpine v3.10.3 965ea09ff2eb 14 months ago 5.55MB
[root@alice ~]#
IMAGE ID一样的话说明镜像是一样的 前面的tag只是一个指针 就像软链接
推送到远程仓库
[root@alice ~]# docker push docker.io/mmdghh/alpine:v3.10.3
The push refers to repository [docker.io/mmdghh/alpine]
77cae8ab23bf: Mounted from library/alpine
received unexpected HTTP status: 504 Gateway Time-out
[root@alice ~]# vim /etc/docker/daemon.json
[root@alice ~]# docker push docker.io/mmdghh/alpine:v3.10.3
The push refers to repository [docker.io/mmdghh/alpine]
77cae8ab23bf: Mounted from library/alpine
Head https://registry-1.docker.io/v2/mmdghh/alpine/blobs/sha256:965ea09ff2ebd2b9eeec88cd822ce156f6674c7e99be082c7efac3c62f3ff652: net/http: TLS handshake timeout
[root@alice ~]# docker push docker.io/mmdghh/alpine:v3.10.3
The push refers to repository [docker.io/mmdghh/alpine]
77cae8ab23bf: Mounted from library/alpine
Post https://registry-1.docker.io/v2/mmdghh/alpine/blobs/uploads/: net/http: TLS handshake timeout
[root@alice ~]# docker push docker.io/mmdghh/alpine:v3.10.3
The push refers to repository [docker.io/mmdghh/alpine] # 能推送的原因是之前登陆过了
77cae8ab23bf: Layer already exists
v3.10.3: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528
[root@alice ~]#
前面几次都失败了 因为网络不稳定
推送后可以在网页看到对应的镜像
img img推送一个latest版本
[root@alice ~]# docker tag 965ea09ff2eb docker.io/mmdghh/alpine:latest
[root@alice ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 389fef711851 3 weeks ago 5.58MB
alpine 3.10.3 965ea09ff2eb 14 months ago 5.55MB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
mmdghh/alpine v3.10.3 965ea09ff2eb 14 months ago 5.55MB
[root@alice ~]# docker push docker.io/mmdghh/alpine:latest
The push refers to repository [docker.io/mmdghh/alpine]
77cae8ab23bf: Layer already exists # 这层已经存在 不会重复推送(增量)
latest: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528
[root@alice ~]#
img
删除镜像
[root@alice ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 389fef711851 3 weeks ago 5.58MB
alpine 3.10.3 965ea09ff2eb 14 months ago 5.55MB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
mmdghh/alpine v3.10.3 965ea09ff2eb 14 months ago 5.55MB
[root@alice ~]# docker rmi 965ea09ff2eb
Error response from daemon: conflict: unable to delete 965ea09ff2eb (must be forced) - image is referenced in multiple repositories #这个ID有多个tag 所以需要用-f 来删除
[root@alice ~]# docker rmi -f 965ea09ff2eb
Untagged: alpine:3.10.3
Untagged: alpine@sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a 先去掉tag再删除
Untagged: mmdghh/alpine:latest
Untagged: mmdghh/alpine:v3.10.3
Untagged: mmdghh/alpine@sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a
Deleted: sha256:965ea09ff2ebd2b9eeec88cd822ce156f6674c7e99be082c7efac3c62f3ff652
Deleted: sha256:77cae8ab23bf486355d1b3191259705374f4a11d483b24964d2f729dd8c076a0
[root@alice ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 389fef711851 3 weeks ago 5.58MB
[root@alice ~]#
上面的删除操作只是删除了本地的镜像,不会对远程仓库的镜像产生影响
img[root@alice ~]# docker pull docker.io/mmdghh/alpine
Using default tag: latest
latest: Pulling from mmdghh/alpine
89d9c30c1d48: Pull complete
Digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a
Status: Downloaded newer image for mmdghh/alpine:latest
[root@alice ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 389fef711851 3 weeks ago 5.58MB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
[root@alice ~]#
容器的基本操作
查看所有容器
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5895a16fb3d hello-world "/hello" 34 seconds ago Exited (0) 33 seconds ago hopeful_edison
[root@alice ~]#
启动容器(运行镜像)
docker run是日常用的最频繁用的命令之一,同样也是较为复杂的命令之一
命令格式: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS :选项
-i
:表示启动-一个可交互的容器, 并持续打开标准输入
-t
:表示使用终端关联到容器的标准输入输出上
-d
:表示将容器放置后台运行
--rm
:退出后即删除容器
--name
:表示定义容器唯一名称
IMAGE
:表示要运行的镜像
COMMAND
:表示启动容器时要运行的命令*
ARG
:参数
交互式启动容器
[root@alice ~]# docker run -it mmdghh/alpine:latest
/ #
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
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
47: eth0@if48: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:18:26:02 brd ff:ff:ff:ff:ff:ff
inet 172.24.38.2/24 brd 172.24.38.255 scope global eth0 # 之前/etc/docker/daemon.json写了网段地址
valid_lft forever preferred_lft forever
/ # [root@alice ~]#
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
facbbda54346 mmdghh/alpine:latest "/bin/sh" 2 minutes ago Exited (130) 6 seconds ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 18 minutes ago Exited (0) 18 minutes ago hopeful_edison
[root@alice ~]#
这里退出之后容器就挂了 因为init为1的进程没有夯住 执行完就退出了
非交互式启动容器
[root@alice ~]# docker run -d --name alpine_sleep docker.io/mmdghh/alpine:latest /bin/sleep 300
ac75ed049d2a2a3020310a3bb24496d4c72aae76a4a71449cb3c37c589df9395
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ac75ed049d2a mmdghh/alpine:latest "/bin/sleep 300" 21 seconds ago Up 20 seconds alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 12 minutes ago Exited (130) 9 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 27 minutes ago Exited (0) 27 minutes ago hopeful_edison
[root@alice ~]#
这里有进程夯住之后 容器就不会挂掉了
在宿主机查看进程
[root@alice ~]# ps aux |grep sleep|grep -v grep
root 20957 0.0 0.0 1540 248 ? Ss 16:30 0:00 /bin/sleep 300
[root@alice ~]#
docker用了宿主机的内核 所以虽然是隔离的 但是在宿主机仍然可以查看到docker的进程 而且有自己的pid
进入容器
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 33 seconds ago Up 32 seconds alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 18 minutes ago Exited (130) 16 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 33 minutes ago Exited (0) 33 minutes ago hopeful_edison
[root@alice ~]# docker exec -it 06fbbee401aa /bin/sh
/ # ps aux
PID USER TIME COMMAND
1 root 0:00 /bin/sleep 300
6 root 0:00 /bin/sh
11 root 0:00 ps aux
/ #
[root@alice ~]# docker exec -it alpine_sleep /bin/sh # 也可以使用容器的名称进入
/ #
容器的启动/停止/重启
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 2 minutes ago Up 2 minutes alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 20 minutes ago Exited (130) 17 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 35 minutes ago Exited (0) 35 minutes ago hopeful_edison
[root@alice ~]# docker stop 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 2 minutes ago Exited (137) 4 seconds ago alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 20 minutes ago Exited (130) 18 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 36 minutes ago Exited (0) 36 minutes ago hopeful_edison
[root@alice ~]# docker start 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 3 minutes ago Up 1 second alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 21 minutes ago Exited (130) 18 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 36 minutes ago Exited (0) 36 minutes ago hopeful_edison
[root@alice ~]# docker restart 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 3 minutes ago Up 3 seconds alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 21 minutes ago Exited (130) 19 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 36 minutes ago Exited (0) 36 minutes ago hopeful_edison
[root@alice ~]# docker restart alpine_sleep # 可以用名字也可以用ID
alpine_sleep
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 4 minutes ago Up 3 seconds alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 22 minutes ago Exited (130) 19 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 37 minutes ago Exited (0) 37 minutes ago hopeful_edison
[root@alice ~]#
在宿主机和容器之间传输文件
docker cp container_id:/tmp/xxx.txt .
删除容器
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 5 minutes ago Up About a minute alpine_sleep
facbbda54346 mmdghh/alpine:latest "/bin/sh" 23 minutes ago Exited (130) 21 minutes ago nostalgic_bartik
f5895a16fb3d hello-world "/hello" 39 minutes ago Exited (0) 39 minutes ago hopeful_edison
[root@alice ~]# docker rm facbbda54346
facbbda54346
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06fbbee401aa mmdghh/alpine:latest "/bin/sleep 300" 5 minutes ago Up About a minute alpine_sleep
f5895a16fb3d hello-world "/hello" 39 minutes ago Exited (0) 39 minutes ago hopeful_edison
[root@alice ~]# docker rm 06fbbee401aa # 正在运行的容器需要用-f 来强制删除
Error response from daemon: You cannot remove a running container 06fbbee401aaad02da272f920dcb264d539187121f962c6bb3dea5acb90321dd. Stop the container before attempting removal or force remove
[root@alice ~]# docker rm -f 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5895a16fb3d hello-world "/hello" 39 minutes ago Exited (0) 39 minutes ago hopeful_edison
[root@alice ~]#
删除所有未在运行的容器
docker rm
docker ps -a -q``
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9072af8ca86b mmdghh/alpine:latest "/bin/sh" 55 seconds ago Exited (0) 54 seconds ago nifty_dirac
a21628344d61 mmdghh/alpine:latest "/bin/sleep 30" About a minute ago Exited (0) 31 seconds ago blissful_varahamihira
9c494e9667b9 mmdghh/alpine:latest "/bin/sleep 300" About a minute ago Up About a minute alpine_sleep
f5895a16fb3d hello-world "/hello" 42 minutes ago Exited (0) 42 minutes ago hopeful_edison
[root@alice ~]# docker ps -a -q
9072af8ca86b
a21628344d61
9c494e9667b9
f5895a16fb3d
[root@alice ~]# docker rm `docker ps -a -q`
9072af8ca86b
a21628344d61
f5895a16fb3d
Error response from daemon: You cannot remove a running container 9c494e9667b9b560563d64fbbe245b881985c910ccb721dba5df906688d5280f. Stop the container before attempting removal or force remove
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c494e9667b9 mmdghh/alpine:latest "/bin/sleep 300" About a minute ago Up About a minute alpine_sleep
[root@alice ~]#
如果需要删除所有容器 包括正在进行的容器,加上-f即可(慎重)
保存镜像
[root@alice ~]# docker run -d --name alpine_sleep docker.io/mmdghh/alpine:latest /bin/sleep 300s
c3d1aa7a1bc2df47f47621f16e420883b334ef8e242349f1f734ad9cb4533968
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3d1aa7a1bc2 mmdghh/alpine:latest "/bin/sleep 300s" 5 seconds ago Up 4 seconds alpine_sleep
[root@alice ~]# docker exec -it c3d1aa7a1bc2 /bin/sh
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # echo "hello world" >1.txt
/ # ls
1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # [root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3d1aa7a1bc2 mmdghh/alpine:latest "/bin/sleep 300s" 45 seconds ago Up 44 seconds alpine_sleep
[root@alice ~]# docker run -it docker.io/mmdghh/alpine:latest /bin/sh
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ #
写入到容器的文件并不会保存在镜像里
-p
保存到执行命令这一时刻的内容 之后更新的不会报错
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3d1aa7a1bc2 mmdghh/alpine:latest "/bin/sleep 300s" 5 minutes ago Up 4 minutes alpine_sleep
[root@alice ~]# docker commit -p alpine_sleep docker.io/mmdghh/alpine:v_1.txt
sha256:11be5214792460b1e258a6d6e7dbca5dccfddce0d438c0ad9b04d78b147006e0
[root@alice ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mmdghh/alpine v_1.txt 11be52147924 8 seconds ago 5.55MB
alpine latest 389fef711851 3 weeks ago 5.58MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
[root@alice ~]# docker run -it docker.io/mmdghh/alpine:v_1.txt /bin/sh
/ # ls
1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # cat 1.txt
hello world
/ # [root@alice ~]#
导入导出镜像
导出: docker save image_name/image_id > xxx.tar
导入: docker load -i xxx.tar
或 docker load < xxx.tar
tip: 如果你导出的时候名称用了:
记得导入的时候用\
转义
[root@alice tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mmdghh/alpine v_1.txt 11be52147924 10 minutes ago 5.55MB
alpine latest 389fef711851 3 weeks ago 5.58MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
[root@alice tmp]# docker save 11be52147924 > mmdghh_alpine_v_1.txt.tar
[root@alice tmp]# ll mmdghh_alpine_v_1.txt.tar
-rw-r--r-- 1 root root 5829632 Jan 10 17:18 mmdghh_alpine_v_1.txt.tar
[root@alice tmp]# docker rmi mmdghh/alpine:v_1.txt -f
Untagged: mmdghh/alpine:v_1.txt
Deleted: sha256:11be5214792460b1e258a6d6e7dbca5dccfddce0d438c0ad9b04d78b147006e0
[root@alice tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 389fef711851 3 weeks ago 5.58MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
[root@alice tmp]# docker load < mmdghh_alpine_v_1.txt.tar
Loaded image ID: sha256:11be5214792460b1e258a6d6e7dbca5dccfddce0d438c0ad9b04d78b147006e0
[root@alice tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 11be52147924 12 minutes ago 5.55MB
alpine latest 389fef711851 3 weeks ago 5.58MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
[root@alice tmp]# docker tag 11be52147924 docker.io/mmdghh/alpine:v_1.txt
[root@alice tmp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mmdghh/alpine v_1.txt 11be52147924 12 minutes ago 5.55MB
alpine latest 389fef711851 3 weeks ago 5.58MB
hello-world latest bf756fb1ae65 12 months ago 13.3kB
mmdghh/alpine latest 965ea09ff2eb 14 months ago 5.55MB
[root@alice tmp]# docker run -it docker.io/mmdghh/alpine:v_1.txt /bin/sh
/ # ls
1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # cat 1.txt
hello world
/ #
查看容器日志
docker logs container_id/``container_name [-f]
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee20554ab3e9 mmdghh/alpine:v_1.txt "/bin/sh" 5 minutes ago Exited (130) 12 seconds ago sharp_hopper
8735b44becfe mmdghh/alpine:v_1.txt "/bin/sh" 16 minutes ago Exited (0) 16 minutes ago pensive_mclean
f2123463239b mmdghh/alpine:v_1.txt "/bin/sh" 18 minutes ago Exited (0) 18 minutes ago happy_bartik
c3d1aa7a1bc2 mmdghh/alpine:latest "/bin/sleep 300s" 25 minutes ago Exited (0) 19 minutes ago alpine_sleep
[root@alice ~]# docker logs ee20554ab3e9
查看容器的详细信息
docker inspect container_name/container_di
下面有例子
args
端口映射
-p host_port:container_port
[root@alice ~]# docker pull nginx:1.12.2
1.12.2: Pulling from library/nginx
f2aa67a397c4: Pull complete
e3eaf3d87fe0: Pull complete
38cb13c1e4c9: Pull complete
Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Status: Downloaded newer image for nginx:1.12.2
[root@alice ~]# docker images |grep nginx
nginx 1.12.2 4037a5562b03 2 years ago 108MB
[root@alice ~]# docker run -d --name nginx -p 83:80 nginx:1.12.2
6ce9e4bb303b754a576d3bf587e0aaec7e3749a3d20f1e40f43c734b28196c67
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ce9e4bb303b nginx:1.12.2 "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:83->80/tcp nginx
[root@alice ~]#
img
挂载目录
-v host_path:container_path
[root@alice ~]# docker pull nginx:1.12.2
1.12.2: Pulling from library/nginx
f2aa67a397c4: Pull complete
e3eaf3d87fe0: Pull complete
38cb13c1e4c9: Pull complete
Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Status: Downloaded newer image for nginx:1.12.2
[root@alice ~]# docker images |grep nginx
nginx 1.12.2 4037a5562b03 2 years ago 108MB
[root@alice ~]# docker run -d --name nginx -p 83:80 nginx:1.12.2
6ce9e4bb303b754a576d3bf587e0aaec7e3749a3d20f1e40f43c734b28196c67
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ce9e4bb303b nginx:1.12.2 "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:83->80/tcp nginx
[root@alice ~]#
[root@alice ~]# mkdir html
[root@alice ~]# cd html/
[root@alice html]# wget www.baidu.com -O index.html
--2021-01-10 17:54:31-- http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 220.181.38.149, 220.181.38.150
Connecting to www.baidu.com (www.baidu.com)|220.181.38.149|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: ‘index.html’
100%[===================================================================================================================>] 2,381 --.-K/s in 0s
2021-01-10 17:54:31 (264 MB/s) - ‘index.html’ saved [2381/2381]
[root@alice html]# cat index.html
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
[root@alice html]# docker run -d --name nginx_with_baidu -p 84:80 -v /root/html:/usr/share/nginx/html nginx:1.12.2
acf79798ce19fdb6e584723d0ab1cc057508082466f6b9be92acc19eca737699
[root@alice html]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
acf79798ce19 nginx:1.12.2 "nginx -g 'daemon of…" 7 seconds ago Up 5 seconds 0.0.0.0:84->80/tcp nginx_with_baidu
6ce9e4bb303b nginx:1.12.2 "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 0.0.0.0:83->80/tcp nginx
[root@alice html]# docker exec -it acf79798ce19 /bin/bash
root@acf79798ce19:/# ls /usr/share/nginx/html/
index.html
img
查看挂载的详细信息
[root@alice html]# docker inspect nginx_with_baidu |grep -A 9 'Mounts'
"Mounts": [
{
"Type": "bind",
"Source": "/root/html",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
[root@alice html]#
传递环境变量
-e ``variate_name=``variate_value
[root@alice ~]# docker run --rm -e E_OPTS=qwert docker.io/mmdghh/alpine:latest printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=62db172fe9da
E_OPTS=qwert
HOME=/root
[root@alice ~]# docker run --rm -e E_OPTS=qwert -e C_OPTS=12345 docker.io/mmdghh/alpine:latest printenv #传递多个变量
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=3ac265a1cf85
E_OPTS=qwert
C_OPTS=12345
HOME=/root
[root@alice ~]#
容器内下载软件
红帽系 yum
debian系 apt-get
alpine apt
进入容器并且下载
[root@alice ~]# docker exec -it nginx_with_baidu /bin/bash
root@acf79798ce19:/# curl
bash: curl: command not found
root@acf79798ce19:/# exit
[root@alice ~]# docker exec -it nginx_with_baidu /bin/bash
root@acf79798ce19:/# tee /etc/apt/sources.list << EOF
> deb http://mirrors.163.com/debian/ jessie main non-free contrib
> deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
> EOF
deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
root@acf79798ce19:/# cat /etc/apt/sources.list
deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
root@acf79798ce19:/# apt-get update && apt-get install curl -y
Ign:1 http://mirrors.163.com/debian jessie InRelease
Get:2 http://mirrors.163.com/debian jessie-updates InRelease [16.3 kB]
Get:3 http://mirrors.163.com/debian jessie Release [77.3 kB]
Get:4 http://mirrors.163.com/debian jessie Release.gpg [1652 B]
Get:5 http://mirrors.163.com/debian jessie/main amd64 Packages [9098 kB]
Get:6 http://mirrors.163.com/debian jessie/non-free amd64 Packages [101 kB]
Get:7 http://mirrors.163.com/debian jessie/contrib amd64 Packages [59.2 kB]
Fetched 9353 kB in 1s (4951 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
ca-certificates krb5-locales libcurl3 libffi6 libgmp10 libgnutls-deb0-28 libgssapi-krb5-2 libhogweed2 libidn11 libk5crypto3 libkeyutils1 libkrb5-3
libkrb5support0 libldap-2.4-2 libnettle4 libp11-kit0 librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libssh2-1 libssl1.0.0 libtasn1-6 openssl
Suggested packages:
gnutls-bin krb5-doc krb5-user libsasl2-modules-otp libsasl2-modules-ldap libsasl2-modules-sql libsasl2-modules-gssapi-mit
| libsasl2-modules-gssapi-heimdal
The following NEW packages will be installed:
ca-certificates curl krb5-locales libcurl3 libffi6 libgmp10 libgnutls-deb0-28 libgssapi-krb5-2 libhogweed2 libidn11 libk5crypto3 libkeyutils1 libkrb5-3
libkrb5support0 libldap-2.4-2 libnettle4 libp11-kit0 librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libssh2-1 libssl1.0.0 libtasn1-6 openssl
0 upgraded, 25 newly installed, 0 to remove and 1 not upgraded.
Need to get 7883 kB of archives.
After this operation, 15.2 MB of additional disk space will be used.
Get:1 http://mirrors.163.com/debian jessie/main amd64 libssl1.0.0 amd64 1.0.1t-1+deb8u8 [1044 kB]
Get:2 http://mirrors.163.com/debian jessie/main amd64 libgmp10 amd64 2:6.0.0+dfsg-6 [253 kB]
Get:3 http://mirrors.163.com/debian jessie/main amd64 libnettle4 amd64 2.7.1-5+deb8u2 [176 kB]
Get:4 http://mirrors.163.com/debian jessie/main amd64 libhogweed2 amd64 2.7.1-5+deb8u2 [125 kB]
Get:5 http://mirrors.163.com/debian jessie/main amd64 libffi6 amd64 3.1-2+deb8u1 [20.2 kB]
Get:6 http://mirrors.163.com/debian jessie/main amd64 libp11-kit0 amd64 0.20.7-1 [81.2 kB]
Get:7 http://mirrors.163.com/debian jessie/main amd64 libtasn1-6 amd64 4.2-3+deb8u3 [49.2 kB]
Get:8 http://mirrors.163.com/debian jessie/main amd64 libgnutls-deb0-28 amd64 3.3.8-6+deb8u7 [696 kB]
Get:9 http://mirrors.163.com/debian jessie/main amd64 libkeyutils1 amd64 1.5.9-5+b1 [12.0 kB]
Get:10 http://mirrors.163.com/debian jessie/main amd64 libkrb5support0 amd64 1.12.1+dfsg-19+deb8u4 [59.4 kB]
Get:11 http://mirrors.163.com/debian jessie/main amd64 libk5crypto3 amd64 1.12.1+dfsg-19+deb8u4 [116 kB]
Get:12 http://mirrors.163.com/debian jessie/main amd64 libkrb5-3 amd64 1.12.1+dfsg-19+deb8u4 [303 kB]
Get:13 http://mirrors.163.com/debian jessie/main amd64 libgssapi-krb5-2 amd64 1.12.1+dfsg-19+deb8u4 [152 kB]
Get:14 http://mirrors.163.com/debian jessie/main amd64 libidn11 amd64 1.29-1+deb8u2 [136 kB]
Get:15 http://mirrors.163.com/debian jessie/main amd64 libsasl2-modules-db amd64 2.1.26.dfsg1-13+deb8u1 [67.1 kB]
Get:16 http://mirrors.163.com/debian jessie/main amd64 libsasl2-2 amd64 2.1.26.dfsg1-13+deb8u1 [105 kB]
Get:17 http://mirrors.163.com/debian jessie/main amd64 libldap-2.4-2 amd64 2.4.40+dfsg-1+deb8u4 [218 kB]
Get:18 http://mirrors.163.com/debian jessie/main amd64 librtmp1 amd64 2.4+20150115.gita107cef-1+deb8u1 [60.0 kB]
Get:19 http://mirrors.163.com/debian jessie/main amd64 libssh2-1 amd64 1.4.3-4.1+deb8u1 [125 kB]
Get:20 http://mirrors.163.com/debian jessie/main amd64 libcurl3 amd64 7.38.0-4+deb8u11 [260 kB]
Get:21 http://mirrors.163.com/debian jessie/main amd64 krb5-locales all 1.12.1+dfsg-19+deb8u4 [2649 kB]
Get:22 http://mirrors.163.com/debian jessie/main amd64 openssl amd64 1.0.1t-1+deb8u8 [664 kB]
Get:23 http://mirrors.163.com/debian jessie/main amd64 ca-certificates all 20141019+deb8u3 [207 kB]
Get:24 http://mirrors.163.com/debian jessie/main amd64 curl amd64 7.38.0-4+deb8u11 [201 kB]
Get:25 http://mirrors.163.com/debian jessie/main amd64 libsasl2-modules amd64 2.1.26.dfsg1-13+deb8u1 [101 kB]
Fetched 7883 kB in 1s (4228 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libssl1.0.0:amd64.
(Reading database ... 7027 files and directories currently installed.)
Preparing to unpack .../00-libssl1.0.0_1.0.1t-1+deb8u8_amd64.deb ...
Unpacking libssl1.0.0:amd64 (1.0.1t-1+deb8u8) ...
Selecting previously unselected package libgmp10:amd64.
Preparing to unpack .../01-libgmp10_2%3a6.0.0+dfsg-6_amd64.deb ...
Unpacking libgmp10:amd64 (2:6.0.0+dfsg-6) ...
Selecting previously unselected package libnettle4:amd64.
Preparing to unpack .../02-libnettle4_2.7.1-5+deb8u2_amd64.deb ...
Unpacking libnettle4:amd64 (2.7.1-5+deb8u2) ...
Selecting previously unselected package libhogweed2:amd64.
Preparing to unpack .../03-libhogweed2_2.7.1-5+deb8u2_amd64.deb ...
Unpacking libhogweed2:amd64 (2.7.1-5+deb8u2) ...
Selecting previously unselected package libffi6:amd64.
Preparing to unpack .../04-libffi6_3.1-2+deb8u1_amd64.deb ...
Unpacking libffi6:amd64 (3.1-2+deb8u1) ...
Selecting previously unselected package libp11-kit0:amd64.
Preparing to unpack .../05-libp11-kit0_0.20.7-1_amd64.deb ...
Unpacking libp11-kit0:amd64 (0.20.7-1) ...
Selecting previously unselected package libtasn1-6:amd64.
Preparing to unpack .../06-libtasn1-6_4.2-3+deb8u3_amd64.deb ...
Unpacking libtasn1-6:amd64 (4.2-3+deb8u3) ...
Selecting previously unselected package libgnutls-deb0-28:amd64.
Preparing to unpack .../07-libgnutls-deb0-28_3.3.8-6+deb8u7_amd64.deb ...
Unpacking libgnutls-deb0-28:amd64 (3.3.8-6+deb8u7) ...
Selecting previously unselected package libkeyutils1:amd64.
Preparing to unpack .../08-libkeyutils1_1.5.9-5+b1_amd64.deb ...
Unpacking libkeyutils1:amd64 (1.5.9-5+b1) ...
Selecting previously unselected package libkrb5support0:amd64.
Preparing to unpack .../09-libkrb5support0_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libkrb5support0:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libk5crypto3:amd64.
Preparing to unpack .../10-libk5crypto3_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libk5crypto3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libkrb5-3:amd64.
Preparing to unpack .../11-libkrb5-3_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libkrb5-3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libgssapi-krb5-2:amd64.
Preparing to unpack .../12-libgssapi-krb5-2_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libgssapi-krb5-2:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libidn11:amd64.
Preparing to unpack .../13-libidn11_1.29-1+deb8u2_amd64.deb ...
Unpacking libidn11:amd64 (1.29-1+deb8u2) ...
Selecting previously unselected package libsasl2-modules-db:amd64.
Preparing to unpack .../14-libsasl2-modules-db_2.1.26.dfsg1-13+deb8u1_amd64.deb ...
Unpacking libsasl2-modules-db:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Selecting previously unselected package libsasl2-2:amd64.
Preparing to unpack .../15-libsasl2-2_2.1.26.dfsg1-13+deb8u1_amd64.deb ...
Unpacking libsasl2-2:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Selecting previously unselected package libldap-2.4-2:amd64.
Preparing to unpack .../16-libldap-2.4-2_2.4.40+dfsg-1+deb8u4_amd64.deb ...
Unpacking libldap-2.4-2:amd64 (2.4.40+dfsg-1+deb8u4) ...
Selecting previously unselected package librtmp1:amd64.
Preparing to unpack .../17-librtmp1_2.4+20150115.gita107cef-1+deb8u1_amd64.deb ...
Unpacking librtmp1:amd64 (2.4+20150115.gita107cef-1+deb8u1) ...
Selecting previously unselected package libssh2-1:amd64.
Preparing to unpack .../18-libssh2-1_1.4.3-4.1+deb8u1_amd64.deb ...
Unpacking libssh2-1:amd64 (1.4.3-4.1+deb8u1) ...
Selecting previously unselected package libcurl3:amd64.
Preparing to unpack .../19-libcurl3_7.38.0-4+deb8u11_amd64.deb ...
Unpacking libcurl3:amd64 (7.38.0-4+deb8u11) ...
Selecting previously unselected package krb5-locales.
Preparing to unpack .../20-krb5-locales_1.12.1+dfsg-19+deb8u4_all.deb ...
Unpacking krb5-locales (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package openssl.
Preparing to unpack .../21-openssl_1.0.1t-1+deb8u8_amd64.deb ...
Unpacking openssl (1.0.1t-1+deb8u8) ...
Selecting previously unselected package ca-certificates.
Preparing to unpack .../22-ca-certificates_20141019+deb8u3_all.deb ...
Unpacking ca-certificates (20141019+deb8u3) ...
Selecting previously unselected package curl.
Preparing to unpack .../23-curl_7.38.0-4+deb8u11_amd64.deb ...
Unpacking curl (7.38.0-4+deb8u11) ...
Selecting previously unselected package libsasl2-modules:amd64.
Preparing to unpack .../24-libsasl2-modules_2.1.26.dfsg1-13+deb8u1_amd64.deb ...
Unpacking libsasl2-modules:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up libssl1.0.0:amd64 (1.0.1t-1+deb8u8) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.24.1 /usr/local/share/perl/5.24.1 /usr/lib/x86_64-linux-gnu/perl5/5.24 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.24 /usr/share/perl/5.24 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Setting up libsasl2-modules-db:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up libsasl2-2:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up libtasn1-6:amd64 (4.2-3+deb8u3) ...
Setting up libgmp10:amd64 (2:6.0.0+dfsg-6) ...
Setting up libssh2-1:amd64 (1.4.3-4.1+deb8u1) ...
Setting up krb5-locales (1.12.1+dfsg-19+deb8u4) ...
Setting up libnettle4:amd64 (2.7.1-5+deb8u2) ...
Setting up openssl (1.0.1t-1+deb8u8) ...
Setting up libffi6:amd64 (3.1-2+deb8u1) ...
Setting up libkeyutils1:amd64 (1.5.9-5+b1) ...
Setting up libsasl2-modules:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up ca-certificates (20141019+deb8u3) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.24.1 /usr/local/share/perl/5.24.1 /usr/lib/x86_64-linux-gnu/perl5/5.24 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.24 /usr/share/perl/5.24 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Updating certificates in /etc/ssl/certs... 174 added, 0 removed; done.
Setting up libidn11:amd64 (1.29-1+deb8u2) ...
Setting up libhogweed2:amd64 (2.7.1-5+deb8u2) ...
Setting up libkrb5support0:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up libp11-kit0:amd64 (0.20.7-1) ...
Setting up libgnutls-deb0-28:amd64 (3.3.8-6+deb8u7) ...
Setting up libk5crypto3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up librtmp1:amd64 (2.4+20150115.gita107cef-1+deb8u1) ...
Setting up libldap-2.4-2:amd64 (2.4.40+dfsg-1+deb8u4) ...
Setting up libkrb5-3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up libgssapi-krb5-2:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up libcurl3:amd64 (7.38.0-4+deb8u11) ...
Setting up curl (7.38.0-4+deb8u11) ...
Processing triggers for ca-certificates (20141019+deb8u3) ...
Updating certificates in /etc/ssl/certs... 0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.
root@acf79798ce19:/# curl -k https://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
安装好后commit并且推送到仓库
[root@alice ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
acf79798ce19 nginx:1.12.2 "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:84->80/tcp nginx_with_baidu
6ce9e4bb303b nginx:1.12.2 "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:83->80/tcp nginx
[root@alice ~]# docker commit -p acf79798ce19 mmdghh/nginx:curl
sha256:84b7a98f5ee209f0139febe7cac04a7edaaca7254ddf1c043e8ac779504204ba
[root@alice ~]# docker push docker.io/mmdghh/nginx:curl
The push refers to repository [docker.io/mmdghh/nginx]
bbadc5b62281: Pushed
4258832b2570: Mounted from library/nginx
683a28d1d7fd: Pushed
d626a8ad97a1: Mounted from library/nginx
curl: digest: sha256:f86f97bacf0ff37e3cc09f98dfb8153c486ee1e8bb9caad5046ed6aa58c43c50 size: 1160
[root@alice ~]#
img
dockerfile
Dockerfile 是一个文本文件,其内包含了一条条的 指令(Instruction),每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。
注: 本文中大部分来自《Docker从入门到实践》如有难懂的地方请移驾原书地址
dockerfile的编写
在一个空白目录中,建立一个文本文件,并命名为 Dockerfile
:
$ mkdir mynginx
$ cd mynginx
$ touch Dockerfile
其内容为
FROM nginx
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
注意事项
由于dockerfile中每一个指令都会建立一层,每一个 RUN
的行为,会新建立一层,在其上执行这些命令,执行结束后,commit
这一层的修改,构成新的镜像。镜像是多层存储,每一层的东西并不会在下一层被删除,会一直跟随着镜像。因此镜像构建时,一定要确保每一层只添加真正需要添加的东西,任何无关的东西都应该清理掉。(安装包、缓存等)
Dockerfile 支持 Shell 类的行尾添加 \
的命令换行方式,以及行首 #
进行注释的格式。良好的格式,比如换行、缩进、注释等,会让维护、排障更为容易,这是一个比较好的习惯。
eg:
FROM debian:stretch
RUN buildDeps='gcc libc6-dev make wget' \
&& apt-get update \
&& apt-get install -y $buildDeps \
&& wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \
&& mkdir -p /usr/src/redis \
&& tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1 \
&& make -C /usr/src/redis \
&& make -C /usr/src/redis install \
&& rm -rf /var/lib/apt/lists/* \
&& rm redis.tar.gz \
&& rm -r /usr/src/redis \
&& apt-get purge -y --auto-remove $buildDeps
构建镜像
命令格式为docker build [选项] <上下文路径/URL/->
在 Dockerfile
文件所在目录执行:
[root@supercomputer]# docker build -t nginx:v3 .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM nginx
---> e43d811ce2f4
Step 2 : RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
---> Running in 9cdc27646c7b
---> 44aa4490ce2c
Removing intermediate container 9cdc27646c7b
Successfully built 44aa4490ce2c
在这里我们指定了最终镜像的名称 -t nginx:v3
上下文路径
docker build
命令最后有一个 .
。.
表示当前目录,但是这里的当前目录指的并非是dockerfile所在的路径docker build -t nginx:v3 .
中的这个 .
,实际上是在指定上下文的目录,docker build
命令会将该目录下的内容打包交给 Docker 引擎以帮助构建镜像。
一般来说,应该会将 Dockerfile
置于一个空目录下,或者项目根目录下。如果该目录下没有所需文件,那么应该把所需文件复制一份过来。如果目录下有些东西确实不希望构建时传给 Docker 引擎,那么可以用 .gitignore
一样的语法写一个 .dockerignore
,该文件是用于剔除不需要作为上下文传递给 Docker 引擎的。
Dockerfile 指令详解
FROM 指定基础镜像
FROM
就是指定 基础镜像,因此一个 Dockerfile
中 FROM
是必备的指令,并且必须是第一条指令。
除了选择现有镜像为基础镜像外,Docker 还存在一个特殊的镜像,名为 scratch
。这个镜像是虚拟的概念,并不实际存在,它表示一个空白的镜像。
FROM scratch
...
如果你以 scratch 为基础镜像的话, 意味着你不以任何镜像为基础, 接下来所写的指令将作为镜像第一层开始存在.
不以任何系统为基础, 直接将可执行文件复制进镜像的做法并不罕见, 比如 swarm, etcd. 对于 Linux 下静态编译的程序来说, 并不需要有操作系统提供运行时支持, 所需的一切库都已经在可执行文件里了, 因此直接 FROM scratch 会让镜像体积更加小巧. 使用 Go 语言 开发的应用很多会使用这种方式来制作镜像, 这也是为什么有人认为 Go 是特别适合容器微服务架构的语言的原因之一.
RUN 执行命令
shell 格式:RUN <命令>
,就像直接在命令行中输入的命令一样
注意要简化命令 避免多次使用run 并且在最后清理安装包等 详见:注意事项
COPY 复制文件
格式:
COPY [--chown=<user>:<group>] <源路径>... <目标路径>
COPY [--chown=<user>:<group>] ["<源路径1>",... "<目标路径>"]
COPY
指令将从构建上下文目录中 <源路径>
的文件/目录复制到新的一层的镜像内的 <目标路径>
位置。比如:
COPY package.json /usr/src/app/
COPY hom* /mydir/
COPY hom?.txt /mydir/
<源路径>
可以是多个,甚至可以是通配符
<目标路径>
可以是容器内的绝对路径,也可以是相对于工作目录的相对路径(工作目录可以用 WORKDIR
指令来指定)。目标路径不需要事先创建,如果目录不存在会在复制文件前先行创建缺失目录。
注: 使用 COPY
指令,源文件的各种元数据都会保留。比如读、写、执行权限、文件变更时间等。
ADD 更高级的复制文件
ADD
指令和 COPY
的格式和性质基本一致。如果 <源路径>
为一个 tar
压缩文件的话,压缩格式为 gzip
, bzip2
以及 xz
的情况下,ADD
指令将会自动解压缩这个压缩文件到 <目标路径>
去。
因此在 COPY
和 ADD
指令中选择的时候,可以遵循这样的原则,所有的文件复制均使用 COPY
指令,仅在需要自动解压缩的场合使用 ADD
。
在使用该指令的时候还可以加上 --chown=<user>:<group>
选项来改变文件的所属用户及所属组。
ADD --chown=55:mygroup files* /mydir/
ADD --chown=bin files* /mydir/
ADD --chown=1 files* /mydir/
ADD --chown=10:11 files* /mydir/
CMD 容器启动命令
CMD
指令的格式和 RUN
相似,也是两种格式:
-
shell
格式:CMD <命令>
-
exec
格式:CMD ["可执行文件", "参数1", "参数2"...]
-
参数列表格式:
CMD ["参数1", "参数2"...]
。在指定了ENTRYPOINT
指令后,用CMD
指定具体的参数。
之前介绍容器的时候曾经说过,Docker 不是虚拟机,容器就是进程。既然是进程,那么在启动容器的时候,需要指定所运行的程序及参数。CMD
指令就是用于指定默认的容器主进程的启动命令的。
在运行时可以指定新的命令来替代镜像设置中的这个默认命令,比如,ubuntu
镜像默认的 CMD
是 /bin/bash
,如果我们直接 docker run -it ubuntu
的话,会直接进入 bash
。我们也可以在运行时指定运行别的命令,如 docker run -it ubuntu cat /etc/os-release
。这就是用 cat /etc/os-release
命令替换了默认的 /bin/bash
命令了,输出了系统版本信息。
在指令格式上,一般推荐使用 exec
格式,这类格式在解析时会被解析为 JSON 数组,因此一定要使用双引号 "
,而不要使用单引号。
如果使用 shell
格式的话,实际的命令会被包装为 sh -c
的参数的形式进行执行。比如:
CMD echo $HOME
在实际执行中,会将其变更为:
CMD [ "sh", "-c", "echo $HOME" ]
这就是为什么我们可以使用环境变量的原因,因为这些环境变量会被 shell 进行解析处理。
提到 CMD
就不得不提容器中应用在前台执行和后台执行的问题。这是初学者常出现的一个混淆。
Docker 不是虚拟机,容器中的应用都应该以前台执行,而不是像虚拟机、物理机里面那样,用 systemd
去启动后台服务,容器内没有后台服务的概念。
一些初学者将 CMD
写为:
CMD service nginx start
然后发现容器执行后就立即退出了。甚至在容器内去使用 systemctl
命令结果却发现根本执行不了。这就是因为没有搞明白前台、后台的概念,没有区分容器和虚拟机的差异,依旧在以传统虚拟机的角度去理解容器。
对于容器而言,其启动程序就是容器应用进程,容器就是为了主进程而存在的,主进程退出,容器就失去了存在的意义,从而退出,其它辅助进程不是它需要关心的东西。
而使用 service nginx start
命令,则是希望 upstart 来以后台守护进程形式启动 nginx
服务。而刚才说了 CMD service nginx start
会被理解为 CMD [ "sh", "-c", "service nginx start"]
,因此主进程实际上是 sh
。那么当 service nginx start
命令结束后,sh
也就结束了,sh
作为主进程退出了,自然就会令容器退出。
正确的做法是直接执行 nginx
可执行文件,并且要求以前台形式运行。比如:
CMD ["nginx", "-g", "daemon off;"]
ENTRYPOINT 入口点
ENTRYPOINT
的格式和 RUN
指令格式一样,分为 exec
格式和 shell
格式。
ENTRYPOINT
的目的和 CMD
一样,都是在指定容器启动程序及参数。ENTRYPOINT
在运行时也可以替代,不过比 CMD
要略显繁琐,需要通过 docker run
的参数 --entrypoint
来指定。
当指定了 ENTRYPOINT
后,CMD
的含义就发生了改变,不再是直接的运行其命令,而是将 CMD
的内容作为参数传给 ENTRYPOINT
指令
ENTRYPOINT
的两种用法
ENTRYPOINT [ "curl", "-s", "https://ip.cn" ]
docker run myip -i # 相当于在后面加了参数 不会改变原来的命令
ENTRYPOINT ["docker-entrypoint.sh"] #此脚本要add进去并且添加执行权限
CMD [ "redis-server" ]
执行时候就是相当于执行docker-entrypoint.sh redis-server
相当于带参数的脚本 比如 mysql 类的数据库,可能需要一些数据库配置、初始化的工作,这些工作要在最终的 mysql 服务器运行之前解决。
参考链接:ENTRYPOINT入口点
ENV 设置环境变量
格式有两种:
ENV <key> <value>
ENV <key1>=<value1> <key2>=<value2>...
这个指令很简单,就是设置环境变量而已,无论是后面的其它指令,如 RUN
,还是运行时的应用,都可以直接使用这里定义的环境变量。
ENV VERSION=1.0 DEBUG=on \
NAME="Happy Feet" #有空格用引号
ARG 构建参数
VOLUME 定义匿名卷
EXPOSE 暴露端口
格式为 EXPOSE <端口1> [<端口2>...]
。
EXPOSE
指令是声明运行时容器提供服务端口,这只是一个声明,在运行时并不会因为这个声明应用就会开启这个端口的服务。在 Dockerfile 中写入这样的声明有两个好处,一个是帮助镜像使用者理解这个镜像服务的守护端口,以方便配置映射;另一个用处则是在运行时使用随机端口映射时,也就是 docker run -P
时,会自动随机映射 EXPOSE
的端口。
WORKDIR 指定工作目录
格式为 WORKDIR <工作目录路径>
。
使用 WORKDIR
指令可以来指定工作目录(或者称为当前目录),以后各层的当前目录就被改为指定的目录,如该目录不存在,WORKDIR
会帮你建立目录。
USER 指定当前用户
格式:USER <用户名>[:<用户组>]
USER
指令和 WORKDIR
相似,都是改变环境状态并影响以后的层。WORKDIR
是改变工作目录,USER
则是改变之后层的执行 RUN
, CMD
以及 ENTRYPOINT
这类命令的身份。
当然,和 WORKDIR
一样,USER
只是帮助你切换到指定用户而已,这个用户必须是事先建立好的,否则无法切换。
RUN groupadd -r redis && useradd -r -g redis redis
USER redis
RUN [ "redis-server" ]
如果以 root
执行的脚本,在执行期间希望改变身份,比如希望以某个已经建立好的用户来运行某个服务进程,不要使用 su
或者 sudo
,这些都需要比较麻烦的配置,而且在 TTY 缺失的环境下经常出错。建议使用 gosu
。
# 建立 redis 用户,并使用 gosu 换另一个用户执行命令
RUN groupadd -r redis && useradd -r -g redis redis
# 下载 gosu
RUN wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/1.7/gosu-amd64" \
&& chmod +x /usr/local/bin/gosu \
&& gosu nobody true
# 设置 CMD,并以另外的用户执行
CMD [ "exec", "gosu", "redis", "redis-server" ]
为什么要用gosu?
- gosu启动命令时只有一个进程,所以docker容器启动时使用gosu,那么该进程可以做到PID等于1;
- sudo启动命令时先创建sudo进程,然后该进程作为父进程去创建子进程,1号PID被sudo进程占据;
参考文章:docker与gosu
HEALTHCHECK 健康检查
ONBUILD 为他人作嫁衣裳
镜像优化
此处笔记为简单记录 详情请参阅附件
📎Docker镜像优化:从1.16GB到22.4MB.html
第一步优化:使用轻量化基础镜像
相较于基于其他 Linux 发行版(例如 Ubuntu)的镜像,基于 Alpine 或 BusyBox 的镜像非常小。这是因为 Alpine 镜像和类似的其他镜像都经过了优化,其中仅包含最少的必须的软件包。
第二步优化:多阶段构建
通过多阶段构建,我们可以在 Dockerfile 中使用多个基础镜像,并将编译成品、配置文件等从一个阶段复制到另一个阶段,这样我们就可以丢弃不需要的东西。
imgDocker的四种网络模型
NAT(默认)
None
不使用网络
使用方法
--net=none
多用于不对外提供网络接口的服务
host
和宿主机共享一个网络空间
使用方法
--net=host
联合网络
和另一个容器共享网络空间
使用方法
--net=container:container_id
网友评论