Docker基础
1.Docker介绍
docker三剑客:仓库、镜像、容器
2.Docker-ce安装
#1.基础环境安装
#安装base、epel源
[root@master ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@master ~]# yum install epel-release -y
[root@master ~]# yum install -y yum-utils
#安装docker-ce源
[root@master ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#查看docker-ce的版本
[root@master ~]# yum list docker-ce --show-duplicates
#默认安装最新版本
[root@master ~]# yum install docker-ce -y
#配置阿里云镜像加速
[root@master ~]# vi /etc/docker/daemon.json
{
"graph": "/data/docker",
"storage-driver": "overlay2",
"insecure-registries": ["registry.access.redhat.com","quay.io"],
"registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"],
"bip": "172.10.69.1/24",
"exec-opts": ["native.cgroupdriver=systemd"],
"live-restore": true
}
#bip:依据自己虚拟机主机ip的最后两位,如我宿主机(192.168.10.69),此处bip(172.10.69.1/24)
#启动docker
[root@master ~]# systemctl enable docker
[root@master ~]# systemctl start docker
3.启动第一个容器
[root@master ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
Status: Downloaded newer image for hello-world:latest
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.
#查看镜像
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 5 months ago 13.3kB
#查看容器
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9702a2f7cd4f hello-world "/hello" 4 minutes ago Exited (0) 4 minutes ago fervent_curie
4.Docker镜像的管理
4.1 注册docker hub仓库
注册地址:http://dockerhub.com/ --> http://hub.docker.com
#命令行登录docker hub仓库
[root@master ~]# docker login docker.io
#查看登录信息
[root@master ~]# cat /root/.docker/config.json
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "***************NTM6Y2Fvd2VpcWlhbmdAMTIz"
}
}
}
#可以将密码反编译出来
[root@master ~]# echo "****************TM6Y2Fvd2VpcWlhbmdAMTIz"|base64 -d
*************:************
4.2 搜索一个镜像
#alpine是一个非常小的linux系统发行版
[root@master ~]# docker search alpine
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
alpine A minimal Docker image based on Alpine Linux… 7784 [OK]
mhart/alpine-node Minimal Node.js built on Alpine Linux 483
anapsix/alpine-java Oracle Java 8 (and 7) with GLIBC 2.28 over A… 472 [OK]
frolvlad/alpine-glibc Alpine Docker image with glibc (~12MB) 267 [OK]
alpine/git A simple git container running in alpine li… 189 [OK]
yobasystems/alpine-mariadb MariaDB running on Alpine Linux [docker] [am… 95 [OK]
alpine/socat Run socat command in alpine container 75 [OK]
kiasaki/alpine-postgres PostgreSQL docker image based on Alpine Linux 44 [OK]
davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 with… 44 [OK]
jfloff/alpine-python A small, more complete, Python Docker image … 41 [OK]
byrnedo/alpine-curl Alpine linux with curl installed and set as … 34 [OK]
hermsi/alpine-sshd Dockerize your OpenSSH-server with rsync and… 34 [OK]
zenika/alpine-chrome Chrome running in headless mode in a tiny Al… 34 [OK]
etopian/alpine-php-wordpress Alpine WordPress Nginx PHP-FPM WP-CLI 25 [OK]
hermsi/alpine-fpm-php FPM-PHP 7.0 to 8.0, shipped along with tons … 25 [OK]
bashell/alpine-bash Alpine Linux with /bin/bash as a default she… 18 [OK]
roribio16/alpine-sqs Dockerized ElasticMQ server + web UI over Al… 15 [OK]
davidcaste/alpine-java-unlimited-jce Oracle Java 8 (and 7) with GLIBC 2.21 over A… 13 [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]
dwdraju/alpine-curl-jq Alpine Docker Image with curl, jq, bash 1 [OK]
bushrangers/alpine-caddy Alpine Linux Docker Container running Caddys… 1 [OK]
goodguykoi/alpine-curl-internal simple alpine image with curl installed no C… 1 [OK]
apteno/alpine-jq Weekly build of alpine image with curl, wget… 1
ellerbrock/alpine-mysql-client MySQL Client based on Alpine Linux 1 [OK]
4.3 下载镜像
[root@master ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
29291e31a76a: Pull complete
Digest: sha256:eb3e4e175ba6d212ba1d6e04fc0782916c08e1c9d7b45892e9796141b1d379ae
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
#registory :仓库 :镜像 : 标签
4.4 下载指定tag
#只有docker hub可以这样写,如果是私有仓库,得写全,
#例:docker pull docker.io/library/alpine:3.14.1
[root@master ~]# docker pull alpine:3.14.1
3.14.1: Pulling from library/alpine
Digest: sha256:eb3e4e175ba6d212ba1d6e04fc0782916c08e1c9d7b45892e9796141b1d379ae
Status: Downloaded newer image for alpine:3.14.1
docker.io/library/alpine:3.14.1
#镜像的结构:registry_name/repository_name/image_name:tag_name
#例如:docker.io/library/alpine:3.14.1
4.5 查看本地镜像命令
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.14.1 021b3423115f 2 weeks ago 5.6MB
alpine latest 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
4.6 给镜像打标签
[root@master ~]# docker tag 021b3423115f docker.io/oldtest/alpine:v3.14.1
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.14.1 021b3423115f 2 weeks ago 5.6MB
alpine latest 021b3423115f 2 weeks ago 5.6MB
oldtest/alpine v3.14.1 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
4.7 推送镜像
[root@master ~]# docker push docker.io/oldtest/alpine:v3.14.1
The push refers to repository [docker.io/oldtest/alpine]
bc276c40b172: Mounted from library/alpine
v3.14.1: digest: sha256:be9bdc0ef8e96dbc428dc189b31e2e3b05523d96d12ed627c37aa2936653258c size: 528
#给镜像在打个标签
[root@master ~]# docker tag 021b3423115f docker.io/oldtest/alpine:latest
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldtest/alpine latest 021b3423115f 2 weeks ago 5.6MB
oldtest/alpine v3.14.1 021b3423115f 2 weeks ago 5.6MB
17361575253/alpine v3.14.1 021b3423115f 2 weeks ago 5.6MB
alpine 3.14.1 021b3423115f 2 weeks ago 5.6MB
alpine latest 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
#再次推送镜像到仓库
[root@master ~]# docker push docker.io/oldtest/alpine:latest
The push refers to repository [docker.io/oldtest/alpine]
bc276c40b172: Layer already exists
latest: digest: sha256:be9bdc0ef8e96dbc428dc189b31e2e3b05523d96d12ed627c37aa2936653258c size: 528
4.8 删除标签
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldtest/alpine latest 021b3423115f 2 weeks ago 5.6MB
oldtest/alpine v3.14.1 021b3423115f 2 weeks ago 5.6MB
alpine 3.14.1 021b3423115f 2 weeks ago 5.6MB
alpine latest 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
[root@master ~]# docker rmi docker.io/oldtest/alpine:latest
Untagged: oldtest/alpine:latest
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine 3.14.1 021b3423115f 2 weeks ago 5.6MB
alpine latest 021b3423115f 2 weeks ago 5.6MB
oldtest/alpine v3.14.1 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
#强制删除镜像
[root@master ~]# docker rmi -f 021b3423115f
Untagged: 17361575253/alpine:v3.14.1
Untagged: alpine:3.14.1
Untagged: alpine:latest
Untagged: alpine@sha256:eb3e4e175ba6d212ba1d6e04fc0782916c08e1c9d7b45892e9796141b1d379ae
Untagged: oldtest/alpine:v3.14.1
Untagged: oldtest/alpine@sha256:be9bdc0ef8e96dbc428dc189b31e2e3b05523d96d12ed627c37aa2936653258c
Deleted: sha256:021b3423115ff662225e83d7e2606475217de7b55fde83ce3447a54019a77aa2
Deleted: sha256:bc276c40b172b1c5467277d36db5308a203a48262d5f278766cf083947d05466
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 5 months ago 13.3kB
#从自己docker hub仓库在拉取镜像
[root@master ~]# docker pull oldtest/alpine
Using default tag: latest
latest: Pulling from oldtest/alpine
29291e31a76a: Pull complete
Digest: sha256:be9bdc0ef8e96dbc428dc189b31e2e3b05523d96d12ed627c37aa2936653258c
Status: Downloaded newer image for oldtest/alpine:latest
docker.io/oldtest/alpine:latest
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldtest/alpine latest 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
4.9 镜像管理命令总结
#登录registry
[root@master ~]# docker login docker.io
#查找镜像
[root@master ~]# docker search alpine
#拉取镜像
[root@master ~]# docker pull alpine
#拉取指定版本镜像
[root@master ~]# docker pull alpine:3.14.1
#列出本地镜像
[root@master ~]# docker images | docker images ls
#给镜像打tag
[root@master ~]# docker tag ${image_id} ${tag}
#推送镜像
[root@master ~]# docker push docker.io/oldtest/alpine:v3.14.1
#删除标签
[root@master ~]# docker rmi docker.io/oldtest/alpine:latest
#删除镜像
[root@master ~]# docker rmi -f 021b3423115f
4.10 Docker镜像的特性
5. Docker容器的基本操作
5.1 查看本地的容器进程
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9702a2f7cd4f hello-world "/hello" 4 hours ago Exited (0) 4 hours ago fervent_curie
5.2 启动容器(运行镜像)
docker run是日常用的最频繁用的命令之一, 同样也是较为复杂的命令之一
命令格式: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS :选项
-i:表示启动一个可交互的容器, 并持续打开标准输入
-t:表示使用终端关联到容器的标准输入输出上
-d:表示将容器放置后台运行
--rm:退出后即删除容器
--name :表示定义容器唯-名称
IMAGE :表示要运行的镜像
COMMAND :表示启动容器时要运行的命令*
5.3 交互式启动容器
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldtest/alpine latest 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
[root@master ~]# docker run -it oldtest/alpine:latest /bin/sh
/ # ip a
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
27: eth0@if28: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:0a:45:02 brd ff:ff:ff:ff:ff:ff
inet 172.10.69.2/24 brd 172.10.69.255 scope global eth0
valid_lft forever preferred_lft forever
#这里ip:(172.10.69.2/24)是我们在安装docker-ce时设置的( "bip": "172.10.69.1/24")
/ # cat /etc/issue
Welcome to Alpine Linux 3.14
Kernel \r on an \m (\l)
#在另一个终端看
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fec63a9039cd oldtest/alpine:latest "/bin/sh" 8 seconds ago Up 7 seconds strange_tesla
9702a2f7cd4f hello-world "/hello" 4 hours ago Exited (0) 4 hours ago fervent_curie
5.4 非交互式启动容器
[root@master ~]# docker run --rm oldtest/alpine:latest /bin/echo hello
hello
#这时执行完自己退出,也查不到这个容器
5.5 非交互式启动一个后台容器
[root@master ~]# docker run -d --name myalpine oldtest/alpine:latest
ec48a31c3b1848d0ac2ebbd6966c135eba17d53c5ea268e110f9834be7f8e9ff
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ec48a31c3b18 oldtest/alpine:latest "/bin/sh" 7 seconds ago Exited (0) 6 seconds ago myalpine
#发现启动的容器直接退出了,大家都知道docker容器,需要有命令让内部夯住,才会一直运行
#让容器睡300s
[root@master ~]# docker run -d --name myalpine1 oldtest/alpine:latest /bin/sleep 300
033cd95052c1d6a9a3817254b69d146c54fb2c7469d430d9f6847a5ce0117f30
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
033cd95052c1 oldtest/alpine:latest "/bin/sleep 300" 3 seconds ago Up 2 seconds myalpine1
ec48a31c3b18 oldtest/alpine:latest "/bin/sh" 3 minutes ago Exited (0) 3 minutes ago myalpine
5.6 查看宿主机进程
[root@master ~]# ps -aux | grep sleep | grep -v grep
root 2315 0.0 0.0 1572 252 ? Ss 15:47 0:00 /bin/sleep 300
5.7 进入容器
[root@master ~]# docker exec -it 033cd95052c1 /bin/sh
/ # ps
PID USER TIME COMMAND
1 root 0:00 /bin/sleep 300
7 root 0:00 /bin/sh
14 root 0:00 ps
5.8 停止|启动|重启|删除容器
#启动|停止|重启是用容器的名字和container id都可以
#停止容器
[root@master ~]# docker stop 033cd95052c1
033cd95052c1
#启动容器
[root@master ~]# docker start 033cd95052c1
033cd95052c1
#重启容器
[root@master ~]# docker restart 033cd95052c1
033cd95052c1
#删除容器
[root@master ~]# docker rm myalpine
myalpine
#删除所有容器(危险操作)
[root@master ~]# docker rm -f $(docker ps -a)
#用for循环删除已经退出的容器
[root@master ~]# for i in `docker ps -a | grep -i exit|awk '{print $1}'` ; do docker rm -f $i;done
5.9 提交/修改容器
#启动一个容器
[root@master ~]# docker run -d --name myapline oldtest/alpine:latest /bin/sleep 300
1ca73b91d78ab23b582a0d482b64a079f43a6a378c6776f4e9c3bce640a0919d
[root@master ~]# docker exec -it myapline /bin/sh
/ # pwd
/
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # echo hello >1.txt
/ # ls
1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # cat 1.txt
hello
/ # exit
#将有1.txt文件的容器提交为新的镜像
[root@master ~]# docker commit -p myapline oldtest/myapline:v3.14.1_1.txt
sha256:d4050381244768a77238f128b247c11b29540e9df83b6714aa80c4df2d651b5e
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldtest/myapline v3.14.1_1.txt d40503812447 3 seconds ago 5.6MB
oldtest/alpine latest 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
#用有1.txt文件的镜像在运行容器
[root@master ~]# docker run -it oldtest/alpine:v3.14.1_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
5.10 导入|导出镜像
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldtest/myapline v3.14.1_1.txt d40503812447 About a minute ago 5.6MB
oldtest/alpine latest 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
#导出镜像
[root@master ~]# docker save d40503812447 > myalpine-v3.14.1_1.txt.tar
[root@master ~]# ls
alpine:v3.14.1_1.txt.tar
#删除镜像
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldtest/myapline v3.14.1_1.txt d40503812447 About a minute ago 5.6MB
oldtest/alpine latest 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
#导出镜像
[root@master ~]# docker rmi -f d40503812447
Untagged: oldtest/myapline:v3.14.1_1.txt
Deleted: sha256:d4050381244768a77238f128b247c11b29540e9df83b6714aa80c4df2d651b5e
Deleted: sha256:f3536d71cb517b457f275991dcfbdc34eb5fb9d8988a57ecb3ad5cd2381f5908
#导入镜像
[root@master ~]# docker load <myalpine-v3.14.1_1.txt.tar
d88f2aeb571d: Loading layer [==================================================>] 3.584kB/3.584kB
Loaded image ID: sha256:d4050381244768a77238f128b247c11b29540e9df83b6714aa80c4df2d651b5e
#有镜像,但没有标签
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> d40503812447 12 minutes ago 5.6MB
oldtest/alpine latest 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
#重新打个标签
[root@master ~]# docker tag d40503812447 oldtest/myalpine:v3.14.1_1.txt
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldtest/myalpine v3.14.1_1.txt d40503812447 13 minutes ago 5.6MB
oldtest/alpine latest 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
#v3.14.1_1.txt镜像启动个容器
[root@master ~]# docker run -it --rm --name myalpine_v3.14.1_1.txt oldtest/myalpine:v3.14.1_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
/ #
5.11 查看容器的日志
#启动容器,将标准错误输出重定向到标准输出,在输出到/dev/null
[root@master ~]# docker run hello-world 2>&1 >>/dev/null
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e5dc8ff8b763 hello-world "/hello" 5 minutes ago Exited (0) 5 minutes ago trusting_noether
[root@master ~]# docker logs -f e5dc8ff8b763
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://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@master ~]# docker ps -a
[root@master ~]# docker run
●docker run是日常用的最频繁用的命令之一,同样也是较为复杂的
命令之一
命令格式: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
OPTIONS:选项
-i:表示启动一个可交互的容器,并持续打开标准输入
-t:表示使用终端关联到容器的标准输入输出上
-d:表示将容器放置后台运行
--rm;退出后即删除容器
--nate: 表示定义容器唯一名称.
IMAGE:表示要运行的镜像
COMMAND:表示启动容器时要运行的命令*
[root@master ~]# docker start/stop/restart
[root@master ~]# docker rm (-f)
[root@master ~]# docker exec/commit
[root@master ~]# docker load/save
[root@master ~]# docker logs (-f)
6.Docker容器的高级操作
●映射端口
●docker run -p容器外端口:容器内端口
●挂载数据卷
● docker run -v容器外目录:容器内目录
●传递环境变量
●docker run -e环境变量key:环境变量value
●容器内安装软件(工具)
● yum/apt get/apt等I
6.1 下载nginx
[root@master ~]# 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
docker.io/library/nginx:1.12.2
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
oldtest/myalpine v3.14.1_1.txt d40503812447 55 minutes ago 5.6MB
oldtest/alpine latest 021b3423115f 2 weeks ago 5.6MB
hello-world latest d1165f221234 5 months ago 13.3kB
nginx 1.12.2 4037a5562b03 3 years ago 108MB
#打个标签
[root@master ~]# docker tag 4037a5562b03 oldtest/nginx:v1.12.2
6.2 映射端口
[root@master ~]# docker run -d --rm --name mynginx -p81:80 oldtest/nginx:v1.12.2
f20a8094a2b52363ff4bf091961d7a09037f9ff18b045f9a2dcb31f87e7bcc33
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f20a8094a2b5 oldtest/nginx:v1.12.2 "nginx -g 'daemon of…" 4 seconds ago Up 2 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp mynginx
[root@master ~]# netstat -lntp |grep 81
tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 11465/docker-proxy
tcp6 0 0 :::81 :::* LISTEN 11472/docker-proxy
[root@master ~]# curl 127.0.0.1:81
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
6.3 数据卷挂载
[root@master ~]# mkdir html
[root@master ~]# cd html/
[root@master html]# wget www.baidu.com -O index.html
--2021-08-25 19:21:21-- http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 36.152.44.96, 36.152.44.95
Connecting to www.baidu.com (www.baidu.com)|36.152.44.96|: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-08-25 19:21:21 (131 MB/s) - ‘index.html’ saved [2381/2381]
[root@master ~]# docker run -d --rm --name nginx_with_html -p82:80 -v /root/html/:/usr/share/nginx/html oldtest/nginx:v1.12.2
0ee772c9ae049d059ea60c0c562bc37752a50e4a7bd238fe8bab6c2fe69952e2
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ee772c9ae04 oldtest/nginx:v1.12.2 "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 0.0.0.0:82->80/tcp, :::82->80/tcp nginx_with_html
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ee772c9ae04 oldtest/nginx:v1.12.2 "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 0.0.0.0:82->80/tcp, :::82->80/tcp nginx_with_html
[root@master ~]# docker exec -it 0ee772c9ae04 /bin/bash
root@0ee772c9ae04:/# cd /usr/share/nginx/html/
root@0ee772c9ae04:/usr/share/nginx/html# ls
index.html
#查看挂载信息
[root@master ~]# docker inspect 0ee772c9ae04 |grep usr
"/root/html/:/usr/share/nginx/html"
"Destination": "/usr/share/nginx/html",
6.4 传递环境变量
#定义单个环境变量
[root@master ~]# docker run --rm -e E_OPTS=abcd oldtest/alpine:latest printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=3b9d1ae2c2e3
E_OPTS=abcd
HOME=/root
#定义多个环境变量(写多个-e就可以)
[root@master ~]# docker run --rm -e E_OPTS=abcd -e C_OPTS=1234 oldtest/alpine:latest printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=a505c8494fc7
E_OPTS=abcd
C_OPTS=1234
HOME=/root
6.5 容器内安装软件
[root@master ~]# docker exec -it 0ee772c9ae04 /bin/bash
root@0ee772c9ae04:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@0ee772c9ae04:/# 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@0ee772c9ae04:/# apt-get update && apt-get install curl -y
root@0ee772c9ae04:/# curl https://www.baidu.com
#将次容器打个标签,推送到docker hub仓库
[root@master ~]# docker commit -p 0ee772c9ae04 oldtest/nginx:curl
sha256:c796ef7d70c5d7bc36084dcbe68d3a3ffe76359ec23b115bc0de733699759f5e
[root@master ~]# docker push oldtest/nginx:curl
The push refers to repository [docker.io/oldtest/nginx]
6f606826d206: Layer already exists
4258832b2570: Layer already exists
683a28d1d7fd: Layer already exists
d626a8ad97a1: Layer already exists
curl: digest: sha256:cdd1e9395337a8078097ed16aca5f8d37820fdaabd8e57eb93f01718c67914ef size: 1160
6.7 容器的生命周期
容器的生命周期
●检查本地是否存在镜像,如果不存在即从远端仓库检索
●利用镜像启动容器
●分配一个文件系统,并在只读的镜像层外挂载一层可读写层
●从宿主机配置的网桥接口中桥接一个虚拟接口到容器
●从地址池配置一个ip地址给容器
●执行用户指定的指令
●执行完毕后容器终止