一、进入容器内部

[root@AH296 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c691ecccfe2b nginx "/docker-entrypoint.鈥 17 hours ago Up 17 hours 0.0.0.0:87->80/tcp, :::87->80/tcp mynginxtest
[root@AH296 ~]# docker exec -it c691ecccfe2b /bin/bash
root@c691ecccfe2b:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@c691ecccfe2b:/# cd /usr/share/nginx/html/
root@c691ecccfe2b:/usr/share/nginx/html# ls -l
total 8
-rw-r--r-- 1 root root 497 Dec 28 2021 50x.html
-rw-r--r-- 1 root root 615 Dec 28 2021 index.html
root@c691ecccfe2b:/usr/share/nginx/html# echo "<h1>Welcome to AH296</h1>" > index.html
root@c691ecccfe2b:/usr/share/nginx/html#

#在宿主机访问容器端口
[root@AH296 ~]# curl 127.0.0.1:87
<h1>Welcome to AH296</h1>
[root@AH296 ~]#

#退出容器
root@c691ecccfe2b:/usr/share/nginx/html# exit
exit
[root@AH296 ~]#

二、将改变后的镜像提交后制作成新的镜像

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
# -a:作者是谁
# -m:变更的描述信息

[root@AH296 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a53b4e544c3 nginx "/docker-entrypoint.鈥 15 hours ago Up 15 hours 0.0.0.0:89->80/tcp, :::89->80/tcp mynginx
[root@AH296 ~]# docker commit -a "ah296" -m "index change" 4a53b4e544c3 ah296:v1.0
sha256:77a523846190bd76afcec9e3c701ef5963ed03dcaa151078a2af4392eabd5e7f
[root@AH296 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ah296 v1.0 77a523846190 4 seconds ago 141MB ###我们提交后创建的镜像
nginx latest 605c77e624dd 11 months ago 141MB
redis latest 7614ae9453d1 11 months ago 113MB
redis 6.2.4 9dae5b22eb39 16 months ago 105MB
[root@AH296 ~]#
  1. 将nginx镜像首页改为我们自己的首页
[root@AH296 ~]# docker ps 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a53b4e544c3 nginx "/docker-entrypoint.鈥 15 hours ago Up 15 hours 0.0.0.0:89->80/tcp, :::89->80/tcp mynginx
root@4a53b4e544c3:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@4a53b4e544c3:/# cd /usr/share/nginx/
root@4a53b4e544c3:/usr/share/nginx# cd html/
root@4a53b4e544c3:/usr/share/nginx/html# ls
50x.html index.html
root@4a53b4e544c3:/usr/share/nginx/html# cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
root@4a53b4e544c3:/usr/share/nginx/html# echo "<h1>Welcome to ah296</h1>" > index.html
root@4a53b4e544c3:/usr/share/nginx/html# cat index.html
<h1>Welcome to ah296</h1>
root@4a53b4e544c3:/usr/share/nginx/html#
  1. 访问页面

docker基础命令(二)_docker

  1. 提交镜像
[root@AH296 ~]# docker commit -a "ah296" -m "change index" 4a53b4e544c3 ah296:v1.1
sha256:443f9a85d562417e2a505cd93209b00bc2ae991f1fca98709972be9974a26b15
[root@AH296 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ah296 v1.1 443f9a85d562 5 seconds ago 141MB
ah296 v1.0 77a523846190 26 hours ago 141MB
nginx latest 605c77e624dd 11 months ago 141MB
redis latest 7614ae9453d1 11 months ago 113MB
redis 6.2.4 9dae5b22eb39 17 months ago 105MB
  1. 使用我们提交的镜像起容器
[root@AH296 ~]# docker run -d --name=mynginx --restart=always -p 89:80 ah296:v1.1
4c5f50279d2e3535b135e623eeeb1abfe7b9a9804ff008c21fbcc1ffd95473d2
[root@AH296 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c5f50279d2e ah296:v1.1 "/docker-entrypoint.鈥 4 seconds ago Up 3 seconds 0.0.0.0:89->80/tcp, :::89->80/tcp mynginx
  1. 通过公网访问,确认是OK的

docker基础命令(二)_html_02

三、镜像保存

  1. 将我们自己的镜像保存为tar文件
[root@AH296 ~]# docker save -o ah296.tar ah296:v1.1
[root@AH296 ~]# ll
total 142512
-rw------- 1 root root 145928704 Dec 15 12:40 ah296.tar
#因为只有一台云主机,接下来的操作也是在同一台主机上进行的
  1. 将镜像解压缩
[root@AH296 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ah296 v1.1 443f9a85d562 12 minutes ago 141MB
ah296 v1.0 77a523846190 26 hours ago 141MB
nginx latest 605c77e624dd 11 months ago 141MB
redis latest 7614ae9453d1 11 months ago 113MB
redis 6.2.4 9dae5b22eb39 17 months ago 105MB
[root@AH296 ~]# docker rmi ah296:v1.1
Untagged: ah296:v1.1
Deleted: sha256:443f9a85d562417e2a505cd93209b00bc2ae991f1fca98709972be9974a26b15
Deleted: sha256:9c920233ff34c3512fab0d80cbc01031dd83932e858868e91b65a9a39808032c
[root@AH296 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ah296 v1.0 77a523846190 26 hours ago 141MB
nginx latest 605c77e624dd 11 months ago 141MB
redis latest 7614ae9453d1 11 months ago 113MB
redis 6.2.4 9dae5b22eb39 17 months ago 105MB
[root@AH296 ~]# ls
ah296.tar
[root@AH296 ~]# docker load -i ah296.tar
94fe8e1e323d: Loading layer [==================================================>] 19.97kB/19.97kB
Loaded image: ah296:v1.1
[root@AH296 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ah296 v1.1 443f9a85d562 13 minutes ago 141MB
ah296 v1.0 77a523846190 26 hours ago 141MB
nginx latest 605c77e624dd 11 months ago 141MB
redis latest 7614ae9453d1 11 months ago 113MB
redis 6.2.4 9dae5b22eb39 17 months ago 105MB
[root@AH296 ~]# docker run -d --name=mynginx --restart=always -p 89:80 ah296:v1.1
d4a065e11069f4509b9d6478a2b827ce817601dd82032728c6c61f6bf9e88e15
[root@AH296 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4a065e11069 ah296:v1.1 "/docker-entrypoint.鈥 About a minute ago Up About a minute 0.0.0.0:89->80/tcp, :::89->80/tcp mynginx
  1. 通过公网访问,确认是OK的

docker基础命令(二)_docker_03

四、镜像推送到docker官网

  1. 给镜像加个标签
[root@AH296 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4a065e11069 ah296:v1.1 "/docker-entrypoint.鈥 10 minutes ago Up 10 minutes 0.0.0.0:89->80/tcp, :::89->80/tcp mynginx
[root@AH296 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ah296 v1.1 443f9a85d562 24 minutes ago 141MB
ah296 v1.0 77a523846190 26 hours ago 141MB
nginx latest 605c77e624dd 11 months ago 141MB
redis latest 7614ae9453d1 11 months ago 113MB
redis 6.2.4 9dae5b22eb39 17 months ago 105MB
[root@AH296 ~]# docker tag ah296:v1.1 daming221/ah296:v1.1
[root@AH296 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
daming221/ah296 v1.1 443f9a85d562 25 minutes ago 141MB
ah296 v1.1 443f9a85d562 25 minutes ago 141MB
ah296 v1.0 77a523846190 26 hours ago 141MB
nginx latest 605c77e624dd 11 months ago 141MB
redis latest 7614ae9453d1 11 months ago 113MB
redis 6.2.4 9dae5b22eb39 17 months ago 105MB
  1. 登录docker官网
[root@AH296 ~]# docker login
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: ah296@outlook.com
Password:
Error response from daemon: Get "https://registry-1.docker.io/v2/": unauthorized: incorrect username or password
[root@AH296 ~]# docker login
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: ah296@outlook.com
Password:
Error response from daemon: Get "https://registry-1.docker.io/v2/": unauthorized: incorrect username or password
[root@AH296 ~]# docker login
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: ah296
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
#注意,登录名不是你的邮箱,你的用户名是登录后docker页面右上角的用户名
  1. 将我们的镜像上传到docker官网
[root@AH296 ~]# docker push daming221/ah296:v1.1
The push refers to repository [docker.io/daming221/ah296]
An image does not exist locally with the tag: daming221/ah296 #这个是因为没有登录docker官网导致的
[root@AH296 ~]# docker push daming221/ah296:v1.1
The push refers to repository [docker.io/daming221/ah296]
94fe8e1e323d: Preparing
d874fd2bc83b: Preparing
32ce5f6a5106: Preparing
f1db227348d0: Preparing
b8d6e692a25e: Preparing
e379e8aedd4d: Waiting
2edcec3590a4: Waiting
denied: requested access to the resource is denied #这个是因为没有登录docker官网导致的
==============================================================================================================
[root@AH296 ~]# docker push daming221/ah296:v1.1
The push refers to repository [docker.io/daming221/ah296]
94fe8e1e323d: Pushed
d874fd2bc83b: Mounted from library/nginx
32ce5f6a5106: Mounted from library/nginx
f1db227348d0: Mounted from library/nginx
b8d6e692a25e: Mounted from library/nginx
e379e8aedd4d: Mounted from library/nginx
2edcec3590a4: Mounted from library/redis
v1.1: digest: sha256:b14644f4c67fb83b024bb0960d003d5b4a2e72dc01ca473f2f823eec30c3fa5a size: 1778
[root@AH296 ~]#

docker基础命令(二)_html_04

  1. 在别的机器上下载我们的镜像,然后运行
[root@AH296 ~]# docker push daming221/ah296:v1.1
[root@AH296 ~]# docker run -d --name=mynginx --restart=always -p 89:80 daming221/ah296:v1.1

五、挂载

注意:挂载数据是无法提交改变的

  1. 删掉我们之前起的容器,然后添加挂载后重新启动
[root@AH296 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4a065e11069 ah296:v1.1 "/docker-entrypoint.鈥 7 hours ago Up 7 hours 0.0.0.0:89->80/tcp, :::89->80/tcp mynginx
[root@AH296 ~]# docker stop d4a065e11069
d4a065e11069
[root@AH296 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
daming221/ah296 v1.1 443f9a85d562 7 hours ago 141MB
ah296 v1.1 443f9a85d562 7 hours ago 141MB
ah296 v1.0 77a523846190 32 hours ago 141MB
nginx latest 605c77e624dd 11 months ago 141MB
redis latest 7614ae9453d1 11 months ago 113MB
redis 6.2.4 9dae5b22eb39 17 months ago 105MB
[root@AH296 ~]# docker run --name=mynginx2 --restart=always -d -p 89:80 -v /data/html:/usr/share/nginx/html:ro nginx
18430de7b0988f65dacbf7f0371a8bcb59932baefd02c91bf79aff99344f9ca8
[root@AH296 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18430de7b098 nginx "/docker-entrypoint.鈥 6 seconds ago Up 5 seconds 0.0.0.0:89->80/tcp, :::89->80/tcp mynginx2
[root@AH296 ~]#
  1. 访问

docker基础命令(二)_docker_05

  1. 进入到主机上的配置文件目录,添加index.html文件
[root@AH296 ~]# cd /data/html/
[root@AH296 html]# ll
total 0
[root@AH296 html]# echo "Welcome to ah296" > index.html
  1. 访问

docker基础命令(二)_docker_06

六、补充

  1. docker日志查看
[root@AH296 html]# docker logs mynginx2
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/12/15 11:21:31 [notice] 1#1: using the "epoll" event method
2022/12/15 11:21:31 [notice] 1#1: nginx/1.21.5
2022/12/15 11:21:31 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/12/15 11:21:31 [notice] 1#1: OS: Linux 3.10.0-1127.19.1.el7.x86_64
2022/12/15 11:21:31 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/12/15 11:21:31 [notice] 1#1: start worker processes
2022/12/15 11:21:31 [notice] 1#1: start worker process 31
2022/12/15 11:21:41 [error] 31#31: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 183.17.231.105, server: localhost, request: "GET / HTTP/1.1", host: "120.24.204.221:89"
183.17.231.105 - - [15/Dec/2022:11:21:41 +0000] "GET / HTTP/1.1" 403 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46" "-"
2022/12/15 11:21:43 [error] 31#31: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 183.17.231.105, server: localhost, request: "GET / HTTP/1.1", host: "120.24.204.221:89"
183.17.231.105 - - [15/Dec/2022:11:21:43 +0000] "GET / HTTP/1.1" 403 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46" "-"
2022/12/15 11:21:45 [error] 31#31: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 183.17.231.105, server: localhost, request: "GET / HTTP/1.1", host: "120.24.204.221:89"
183.17.231.105 - - [15/Dec/2022:11:21:45 +0000] "GET / HTTP/1.1" 403 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46" "-"
183.17.231.105 - - [15/Dec/2022:11:25:52 +0000] "GET / HTTP/1.1" 200 17 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46" "-"
[root@AH296 html]# docker logs mynginx2
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/12/15 11:21:31 [notice] 1#1: using the "epoll" event method
2022/12/15 11:21:31 [notice] 1#1: nginx/1.21.5
2022/12/15 11:21:31 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/12/15 11:21:31 [notice] 1#1: OS: Linux 3.10.0-1127.19.1.el7.x86_64
2022/12/15 11:21:31 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/12/15 11:21:31 [notice] 1#1: start worker processes
2022/12/15 11:21:31 [notice] 1#1: start worker process 31
2022/12/15 11:21:41 [error] 31#31: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 183.17.231.105, server: localhost, request: "GET / HTTP/1.1", host: "120.24.204.221:89"
183.17.231.105 - - [15/Dec/2022:11:21:41 +0000] "GET / HTTP/1.1" 403 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46" "-"
2022/12/15 11:21:43 [error] 31#31: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 183.17.231.105, server: localhost, request: "GET / HTTP/1.1", host: "120.24.204.221:89"
183.17.231.105 - - [15/Dec/2022:11:21:43 +0000] "GET / HTTP/1.1" 403 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46" "-"
2022/12/15 11:21:45 [error] 31#31: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 183.17.231.105, server: localhost, request: "GET / HTTP/1.1", host: "120.24.204.221:89"
183.17.231.105 - - [15/Dec/2022:11:21:45 +0000] "GET / HTTP/1.1" 403 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46" "-"
183.17.231.105 - - [15/Dec/2022:11:25:52 +0000] "GET / HTTP/1.1" 200 17 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46" "-"
183.17.231.105 - - [15/Dec/2022:11:28:03 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46" "-"
[root@AH296 html]#
  1. 把容器指定位置的东西复制出来
docker cp 18430de7b098:/etc/nginx/nginx.conf  /data/conf/nginx.conf
  1. 把主机文件复制到容器中
docker cp /data/conf/nginx.conf 18430de7b098:/etc/nginx/nginx.conf