安装docker
----epel源
yum install -y epel-release
----安装yum集成的实用程序集合
yum install -y yum-utils
----获得docker-ce的yum仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
----列出docker版本信息
yum list docker-ce --show-duplicates
----默认下载是最新的docker-ce
yum install -y docker-ce
----设置开机自启
systemctl enable docker
----对docker引擎做一些配置
{
vi /etc/docker/daemon.json
"graph": "/data/docker",
"storage-driver": "overlay2",
"insecure-registries": ["registry.access.redhat.com","quay.io","harbor.od.com"],
"registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"],
"bip": "172.7.1.1/24",
"exec-opts": ["native.cgroupdriver=systemd"],
"live-restore": true
}
----启动docker
systemctl start docker
----查看有返回值说明docker引擎正常
docker info
docker version
启动第一个docker容器
docker run hello-world
docker镜像结构:
注册一个hub.docker的用户
~]# 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: yhd2021
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
~]#
查看用户认证的密码
cat /root/.docker/config.json
搜索镜像
docker search alpine
下载镜像(默认是最新的)
~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
596ba82af5aa: Pull complete
Digest: sha256:d9a7354e3845ea8466bb00b22224d9116b183e594527fb5b6c3d30bc01a20378
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
下载指定镜像
~]# docker pull alpine:3.10.1
3.10.1: Pulling from library/alpine
050382585609: Pull complete
Digest: sha256:6a92cd1fcdc8d8cdec60f33dda4db2cb1fcdcacf3410a8e05b3741f44a9b5998
Status: Downloaded newer image for alpine:3.10.1
docker.io/library/alpine:3.10.1
查看镜像
~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 4 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
alpine 3.10.1 b7b28af77ffe 19 months ago 5.58MB
~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 4 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
alpine 3.10.1 b7b28af77ffe 19 months ago 5.58MB
给镜像打标签
~]# docker tag 7731472c3f2a docker.io/yhd2021/alpine:v3.13.1
~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 4 weeks ago 5.61MB
yhd2021/alpine v3.13.1 7731472c3f2a 4 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
alpine 3.10.1 b7b28af77ffe 19 months ago 5.58MB
推送镜像
~]# docker push docker.io/yhd2021/alpine:v3.13.1
The push refers to repository [docker.io/yhd2021/alpine]
c04d1437198b: Mounted from library/alpine
v3.13.1: digest: sha256:d0710affa17fad5f466a70159cc458227bd25d4afb39514ef662ead3e6c99515 size: 528
删除镜像标签
~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 4 weeks ago 5.61MB
yhd2021/alpine v3.13.1 7731472c3f2a 4 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
alpine 3.10.1 b7b28af77ffe 19 months ago 5.58MB
yhd2021/latest latest b7b28af77ffe 19 months ago 5.58MB
~]# docker rmi docker.io/yhd2021/latest
Untagged: yhd2021/latest:latest
Untagged: yhd2021/latest@sha256:57334c50959f26ce1ee025d08f136c2292c128f84e7b229d1b0da5dac89e9866
~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 7731472c3f2a 4 weeks ago 5.61MB
yhd2021/alpine v3.13.1 7731472c3f2a 4 weeks ago 5.61MB
hello-world latest bf756fb1ae65 13 months ago 13.3kB
alpine 3.10.1 b7b28af77ffe 19 months ago 5.58MB
~]#
指定images id彻底删除
~]# docker rmi -f 7731472c3f2a docker.io/yhd2021/latest
Untagged: alpine:latest
Untagged: alpine@sha256:d9a7354e3845ea8466bb00b22224d9116b183e594527fb5b6c3d30bc01a20378
Untagged: yhd2021/alpine:v3.13.1
Untagged: yhd2021/alpine@sha256:d0710affa17fad5f466a70159cc458227bd25d4afb39514ef662ead3e6c99515
Deleted: sha256:7731472c3f2a25edbb9c085c78f42ec71259f2b83485aa60648276d408865839
Deleted: sha256:c04d1437198bc178b49fdb9a90a9c0e362ffbd63c4be5d19fec406d1c9d6a03c
Error: No such image: docker.io/yhd2021/latest
~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 13 months ago 13.3kB
alpine 3.10.1 b7b28af77ffe 19 months ago 5.58MB
容器的操作
查看本地的容器进程
~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e5fd19194939 hello-world "/hello" About an hour ago Exited (0) About an hour ago quizzical_panini
启动容器并查看ip地址以及发行版本
~]# docker run -it docker.io/yhd2021/alpine:v3.13.1 /bin/sh
/ # ip add
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
12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:07:01:02 brd ff:ff:ff:ff:ff:ff
inet 172.7.1.2/24 brd 172.7.1.255 scope global eth0
valid_lft forever preferred_lft forever
/ # cat /etc/issue
Welcome to Alpine Linux 3.13
Kernel \r on an \m (\l)
/ #
用非交互式的方式启动容器
~]# docker run --rm docker.io/yhd2021/alpine:v3.13.1 /bin/echo hello
hello
非交互式启动一个后台程序
~]# docker run -d --name myalpine docker.io/yhd2021/alpine:v3.13.1
b1d6343a3ccd295dfe03334aebe9d0180c85f227b71e672ddccb2b2cfe423cc4
~]# docker run -d --name myalpine1 docker.io/yhd2021/alpine:v3.13.1 /bin/sleep 300
0177b5ff84b556f09dc04f8348e03543ce72cf6cebe0357a4c896ea014114dfd
~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0177b5ff84b5 yhd2021/alpine:v3.13.1 "/bin/sleep 300" 6 seconds ago Up 5 seconds myalpine1
b1d6343a3ccd yhd2021/alpine:v3.13.1 "/bin/sh" About a minute ago Exited (0) About a minute ago myalpine
打包导出备份镜像
~]# docker save 7731472c3f2a > alpine:v3.13.3.tar
导入镜像并打标签
~]# docker load < alpine\:v3.13.3.tar
~]# docker tag 7731472c3f2a yhd2021/alpine:v3.13.1
查看容器日志(docker logs -f dc28fa64bef9)
~]# docker run hello-world 2>&1 >>/dev/null
[root@salve001 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dc28fa64bef9 hello-world "/hello" 7 seconds ago Exited (0) 6 seconds ago great_shaw
[root@salve001 ~]# docker logs dc28fa64bef9
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/
映射端口
下载nginx
~]# 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
打标签启动
~]# docker tag 4037a5562b03 yhd2021/nginx:v.1.12.2
~]# docker images~]# docker run --rm --name mynginx -d -p81:80 yhd2021/nginx:v.1.12.2
6ff91c59ac4b507217051473b19fabf51424cbfecb9bf8fd8dbb336c2c40948f
[root@salve001 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ff91c59ac4b yhd2021/nginx:v.1.12.2 "nginx -g 'daemon of?? 14 seconds ago Up 13 seconds 0.0.0.0:81->80/tcp mynginx
查看验证端口
[root@salve001 ~]# netstat -anlpt|grep 81
tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 13212/docker-proxy
tcp 0 0 127.0.0.1:49058 127.0.0.1:81 TIME_WAIT -
[root@salve001 ~]# 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>
挂载数据卷
~]# mkdir html
~]# cd html
html]# wget www.baidu.com -O index.html
html]# docker run -d --rm --name nginx_with_baidu -d -p82:80 -v/root/html:/usr/share/nginx/html yhd2021/nginx:v.1.12.2
65d987b30c33d83c8e7cb6861308f285a5ae7cc8d87e8b3b46e54ec8d74ba685
html]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65d987b30c33 yhd2021/nginx:v.1.12.2 "nginx -g 'daemon of?? 14 seconds ago Up 12 seconds 0.0.0.0:82->80/tcp nginx_with_baidu
html]# docker exec -it nginx_with_baidu /bin/sh
# cd /usr/share/nginx/html
# ls
index.html
--检查容器的一些细节
html]# docker inspect 65d987b30c33|grep share
"/root/html:/usr/share/nginx/html"
"Destination": "/usr/share/nginx/html"
传递环境变量
~]# docker run --rm -e E_OPTS=abcd yhd2021/alpine:v3.13.1 printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=13362bf870fe
E_OPTS=abcd
HOME=/root
容器内安装工具
~]# docker exec -it nginx_with_baidu /bin/bash
root@65d987b30c33:/# curl
bash: curl: command not found
root@65d987b30c33:/# tee /etc/apt/sources.list << EOF
deb http://mirrors.163.com/debian/ jessie main non-ffree contrib
deb http://mirrirs.163.com/dobian/ jessie-updates main non-free contrib
EOF
root@65d987b30c33:/# apt-get update && apt-get install curl -y
~]# docker commit -p 65d987b30c33 yhd2021/nginx:curl
~]# docker push yhd2021/nginx:curl
Dockerfile的简单操作
USER/WORK_DIR
dockerfile]# vim /data/dockerfile/Dockerfile
FROM yhd2021/nginx:v.1.12.2
USER nginx
WORKDIR /usr/share/nginx/html
dockerfile]# docker build . -t docker.io/yhd2021/nginx:v.1.12.2_with_user_workdir
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM yhd2021/nginx:v.1.12.2
---> 4037a5562b03
Step 2/3 : USER nginx
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 254bccc87dc6
Removing intermediate container 254bccc87dc6
---> 3aa7dde90cca
Step 3/3 : WORKDIR /usr/share/nginx/html
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 94adb3483fda
Removing intermediate container 94adb3483fda
---> 3a46d40cc184
Successfully built 3a46d40cc184
Successfully tagged yhd2021/nginx:v.1.12.2_with_user_workdir
dockerfile]# docker run --rm -ti --name nginx123 yhd2021/nginx:v.1.12.2_with_user_workdir /bin/bash
nginx@282ecdd15a0c:/usr/share/nginx/html$ pwd
/usr/share/nginx/html
nginx@282ecdd15a0c:/usr/share/nginx/html$
ADD / EXPOSE
dockerfile]# vim /data/dockerfile/Dockerfile
FROM yhd2021/nginx:v.1.12.2
ADD index.html /usr/share/nginx/html/index.html
EXPOSE 80
dockerfile]# docker build . -t docker.io/yhd2021/nginx:v.1.12.2_with_index_expose
Sending build context to Docker daemon 5.12kB
Step 1/3 : FROM yhd2021/nginx:v.1.12.2
---> 4037a5562b03
Step 2/3 : ADD index.html /usr/share/nginx/html/index.html
---> 0df699ff4235
Step 3/3 : EXPOSE 80
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 89f04f4ef9bd
Removing intermediate container 89f04f4ef9bd
---> dc552a3788b2
Successfully built dc552a3788b2
Successfully tagged yhd2021/nginx:v.1.12.2_with_index_expose
dockerfile]# docker run --rm -ti --name nginx_expose -P yhd2021/nginx:v.1.12.2_with_index_expose /bin/bash
root@173c187a9232:/# whoami
root
root@173c187a9232:/# pwd
/
root@173c187a9232:/# nginx -g "daemon off;"
RUN/ENV
dockerfile]# vim Dockerfile
FROM centos:7
ENV VER 9.11.4
RUN yum install bind-$VER -y
------查看版本信息
yum list bind --show-duplicates
dockerfile]# docker build . -t yhd2021/bind:v9.11.4_with_env_run
CMD指定
dockerfile]# vim Dockerfile
FROM centos:7
RUN yum install httpd -y
CMD ["httpd","-D","FOREGROUND"]
dockerfile]# docker build . -t yhd2021/httpd:test
entrypoint
dockerfile]# vim Dockerfile
FROM centos:7
ADD entrypoint.sh /entrypoint.sh
RUN yum install epel-release -q -y && yum install nginx -y
ENTRYPOINT /entrypoint.sh
dockerfile]# vi entrypoint.sh
#!/bin/bash
/sbin/nginx -g "daemon off;"
dockerfile]# docker build . -t yhd2021/nginx:mynginx2020