Docker实战笔记 一 Nginx镜像

1.创建一个文件夹映射容器内文件

[email protected]:/home#mkdir demo
[email protected]:/home#ll

2.拉取nginx镜像

[email protected]:/home/demo#docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
1f7ce2fa46ab: Already exists 
9b16c94bb686: Pull complete 
9a59d19f9c5b: Pull complete 
9ea27b074f71: Pull complete 
c6edf33e2524: Pull complete 
84b1ff10387b: Pull complete 
517357831967: Pull complete 
Digest: sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

3.查看镜像

[email protected]:/home/demo#docker images
REPOSITORY             TAG                IMAGE ID       CREATED         SIZE
nginx                  latest             a6bd71f48f68   9 days ago      187MB
postgres               12                 0d56839ac0df   3 weeks ago     412MB
redis                  latest             7c4b517da47d   2 months ago    153MB
openzipkin/zipkin      latest             7fb470c13693   4 months ago    165MB
postgres               latest             1921dda0e2c5   5 months ago    412MB
redis                  6.2.10-alpine      a470c0525301   9 months ago    27.1MB
sonarqube              8.9.10-community   4009139ebd19   11 months ago   490MB
seataio/seata-server   1.5.2              f6a5368b6720   16 months ago   186MB
nacos/nacos-server     2.0.3              433eb51fef8d   2 years ago     1.05GB
seataio/seata-server   1.4.2              4909c4c024dc   2 years ago     156MB

4.启动nginx容器

[email protected]:/home/demo#docker run -p 6886:80 --name nginx -d nginx:latest
5bbd677a0cdd067ae4c2c23f6db19e7a02770ccb49e840facf963a8d24317857
[email protected]:/home/demo#docker ps 
CONTAINER ID   IMAGE                        COMMAND                   CREATED          STATUS          PORTS                                                                                  NAMES
5bbd677a0cdd   nginx:latest                 "/docker-entrypoint.…"   17 seconds ago   Up 15 seconds   0.0.0.0:6886->80/tcp, :::6886->80/tcp                                                  nginx
be0f46c5450a   seataio/seata-server:1.4.2   "java -Djava.securit…"   4 months ago     Up 2 weeks      0.0.0.0:8091->8091/tcp, :::8091->8091/tcp                                              seata-server
e5e9b616802c   nacos/nacos-server:2.0.3     "bin/docker-startup.…"   5 months ago     Up 2 weeks      0.0.0.0:8848->8848/tcp, :::8848->8848/tcp, 0.0.0.0:9848->9848/tcp, :::9848->9848/tcp   nacos203
85c7e7acf404   redis:6.2.10-alpine          "docker-entrypoint.s…"   9 months ago     Up 2 weeks      0.0.0.0:56379->6379/tcp, :::56379->6379/tcp   

5.进入容器内部

[email protected]:/home/demo#docker exec -it nginx bash
root@5bbd677a0cdd:/# ls
bin   docker-entrypoint.d   home   lib64   mnt   root  srv  usr
boot  docker-entrypoint.sh  lib    libx32  opt   run   sys  var
dev   etc                   lib32  media   proc  sbin  tmp

6.查看容器内部文件位置

root@5bbd677a0cdd:/etc/nginx# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params

7.拷贝容器内文件到外部路径

[email protected]:/home/demo#docker cp nginx:/etc/nginx ./
Preparing to copy...
Copying from container - 16.9kB
Successfully copied 16.9kB to /home/demo/./
[email protected]:/home/demo#ll
总用量 0
drwxr-xr-x 3 root root 132 11月 21 17:05 nginx
[email protected]:/home/demo#cd nginx
[email protected]:/home/demo/nginx#ll
总用量 24
drwxr-xr-x 2 root root   26 12月  1 10:29 conf.d
-rw-r--r-- 1 root root 1007 10月 24 21:46 fastcgi_params
-rw-r--r-- 1 root root 5349 10月 24 21:46 mime.types
lrwxrwxrwx 1 root root   22 10月 25 00:10 modules -> /usr/lib/nginx/modules
-rw-r--r-- 1 root root  648 10月 25 00:10 nginx.conf
-rw-r--r-- 1 root root  636 10月 24 21:46 scgi_params
-rw-r--r-- 1 root root  664 10月 24 21:46 uwsgi_params

8.查看nginx的html和logs路径位置

[email protected]:/home/demo/nginx#docker exec -it nginx bash
root@5bbd677a0cdd:/# cd usr
root@5bbd677a0cdd:/usr# ls
bin  games  include  lib  lib32  lib64  libexec  libx32  local  sbin  share  src
root@5bbd677a0cdd:/usr# cd share
root@5bbd677a0cdd:/usr/share# ls
X11              common-licenses  dpkg        java         man         pam-configs  util-linux
base-files       debconf          fontconfig  keyrings     maven-repo  perl5        xml
base-passwd      debianutils      fonts       libc-bin     menu        pixmaps      zoneinfo
bash-completion  dict             gcc         libgcrypt20  misc        polkit-1     zsh
bug              doc              gdb         lintian      nginx       tabset
ca-certificates  doc-base         info        locale       pam         terminfo
root@5bbd677a0cdd:/usr/share# cd nginx
root@5bbd677a0cdd:/usr/share/nginx# ls
html

root@5bbd677a0cdd:/usr/share# cd nginx
root@5bbd677a0cdd:/usr/share/nginx# ls
html
root@5bbd677a0cdd:/usr/share/nginx# cd /var/log
root@5bbd677a0cdd:/var/log# cd nginx
root@5bbd677a0cdd:/var/log/nginx# ls
access.log  error.log

[email protected]:/home/demo#mkdir logs
[email protected]:/home/demo#mkdir html
[email protected]:/home/demo#ll
总用量 0
drwxr-xr-x 3 root root 132 11月 21 17:05 conf
drwxr-xr-x 2 root root   6 12月  1 10:50 html
drwxr-xr-x 2 root root   6 12月  1 10:49 logs

9.删除现有nginx容器,准备启动正式容器

[email protected]:/home/demo#docker ps 
CONTAINER ID   IMAGE                        COMMAND                   CREATED          STATUS          PORTS                                                                                  NAMES
5bbd677a0cdd   nginx:latest                 "/docker-entrypoint.…"   22 minutes ago   Up 22 minutes   0.0.0.0:6886->80/tcp, :::6886->80/tcp                                                  nginx
be0f46c5450a   seataio/seata-server:1.4.2   "java -Djava.securit…"   4 months ago     Up 2 weeks      0.0.0.0:8091->8091/tcp, :::8091->8091/tcp                                              seata-server
e5e9b616802c   nacos/nacos-server:2.0.3     "bin/docker-startup.…"   5 months ago     Up 2 weeks      0.0.0.0:8848->8848/tcp, :::8848->8848/tcp, 0.0.0.0:9848->9848/tcp, :::9848->9848/tcp   nacos203
85c7e7acf404   redis:6.2.10-alpine          "docker-entrypoint.s…"   9 months ago     Up 2 weeks      0.0.0.0:56379->6379/tcp, :::56379->6379/tcp                                            redis6.2
[email protected]:/home/demo#docker stop nginx
nginx

[email protected]:/home/demo#docker remove nginx
nginx
[email protected]:/home/demo#docker ps 
CONTAINER ID   IMAGE                        COMMAND                   CREATED        STATUS       PORTS                                                                                  NAMES
be0f46c5450a   seataio/seata-server:1.4.2   "java -Djava.securit…"   4 months ago   Up 2 weeks   0.0.0.0:8091->8091/tcp, :::8091->8091/tcp                                              seata-server
e5e9b616802c   nacos/nacos-server:2.0.3     "bin/docker-startup.…"   5 months ago   Up 2 weeks   0.0.0.0:8848->8848/tcp, :::8848->8848/tcp, 0.0.0.0:9848->9848/tcp, :::9848->9848/tcp   nacos203
85c7e7acf404   redis:6.2.10-alpine          "docker-entrypoint.s…"   9 months ago   Up 2 weeks   0.0.0.0:56379->6379/tcp, :::56379->6379/tcp   

10.启动正式nginx容器,挂载相关html,logs,conf目录

[email protected]:/home/demo#docker run -p 6886:80 --name nginx -v /home/demo/conf:/etc/nginx -v /home/demo/html:/usr/share/nginx/html -v /home/demo/logs:/var/log/nginx -d nginx:latest
93f19b37815179019f436d6a60073fb56314297a4b60752b84a96f780436199d
[email protected]:/home/demo#docker ps
CONTAINER ID   IMAGE                        COMMAND                   CREATED          STATUS          PORTS                                                                                  NAMES
93f19b378151   nginx:latest                 "/docker-entrypoint.…"   25 seconds ago   Up 25 seconds   0.0.0.0:6886->80/tcp, :::6886->80/tcp                                                  nginx
be0f46c5450a   seataio/seata-server:1.4.2   "java -Djava.securit…"   4 months ago     Up 2 weeks      0.0.0.0:8091->8091/tcp, :::8091->8091/tcp                                              seata-server
e5e9b616802c   nacos/nacos-server:2.0.3     "bin/docker-startup.…"   5 months ago     Up 2 weeks      0.0.0.0:8848->8848/tcp, :::8848->8848/tcp, 0.0.0.0:9848->9848/tcp, :::9848->9848/tcp   nacos203
85c7e7acf404   redis:6.2.10-alpine          "docker-entrypoint.s…"   9 months ago     Up 2 weeks      0.0.0.0:56379->6379/tcp, :::56379->6379/tcp         

11.查看浏览器访问服务http://ip:6886,会报错,因为html文件夹里面没有index.html

[email protected]:/home/demo#cd logs
[email protected]:/home/demo/logs#ll
总用量 4
-rw-r--r-- 1 root root   0 12月  1 10:54 access.log
-rw-r--r-- 1 root root 855 12月  1 10:54 error.log
[email protected]:/home/demo/logs#tailf access.log 
10.60.1.117 - - [01/Dec/2023:02:56:11 +0000] "GET / HTTP/1.1" 403 153 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0" "-"
10.60.1.117 - - [01/Dec/2023:02:56:12 +0000] "GET / HTTP/1.1" 403 153 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0" "-"
^C
[email protected]:/home/demo/logs#ll
总用量 8
-rw-r--r-- 1 root root  322 12月  1 10:56 access.log
-rw-r--r-- 1 root root 1233 12月  1 10:56 error.log

12.查看错误日志

[email protected]:/home/demo/logs#tailf error.log 
2023/12/01 02:54:50 [notice] 1#1: start worker process 22
2023/12/01 02:54:50 [notice] 1#1: start worker process 23
2023/12/01 02:54:50 [notice] 1#1: start worker process 24
2023/12/01 02:54:50 [notice] 1#1: start worker process 25
2023/12/01 02:54:50 [notice] 1#1: start worker process 26
2023/12/01 02:54:50 [notice] 1#1: start worker process 27
2023/12/01 02:54:50 [notice] 1#1: start worker process 28
2023/12/01 02:54:50 [notice] 1#1: start worker process 29
2023/12/01 02:56:11 [error] 22#22: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 10.60.1.117, server: localhost, request: "GET / HTTP/1.1", host: "10.6.132.166:6886"
2023/12/01 02:56:12 [error] 22#22: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 10.60.1.117, server: localhost, request: "GET / HTTP/1.1", host: "10.6.132.166:6886"
2023/12/01 02:56:56 [error] 22#22: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 10.60.1.117, server: localhost, request: "GET / HTTP/1.1", host: "10.6.132.166:6886"
2023/12/01 02:56:57 [error] 22#22: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 10.60.1.117, server: localhost, request: "GET / HTTP/1.1", host: "10.6.132.166:6886"
^C
[email protected]:/home/demo/logs#cd ../
[email protected]:/home/demo#ll
总用量 0
drwxr-xr-x 3 root root 132 11月 21 17:05 conf
drwxr-xr-x 2 root root   6 12月  1 10:50 html
drwxr-xr-x 2 root root  41 12月  1 10:54 logs
[email protected]:/home/demo#cd html
[email protected]:/home/demo/html#ll
总用量 0

13.创建一个index.html

[email protected]:/home/demo/html#vi index.html



hello docker nginx!!!

14.刷新网页 即可

15.设置开机启动

[email protected]:/home/demo/html#systemctl enable docker

[email protected]:/home/demo/html#docker update --restart=always nginx

--restart具体参数值详细信息:

no: 默认策略,容器退出时不重启容器;

on-failure: 在容器非正常退出时(退出状态非0)才重新启动容器;

on-failure:3  在容器非正常退出时重启容器,最多重启3次;

always: 无论退出状态是如何,都重启容器;

unless-stopped: 在容器退出时总是重启容器,
但是不考虑在 Docker 守护进程启动时就已经停止了的容器。

Docker实战笔记 二 Springboot Idea 插件打包-CSDN博客

你可能感兴趣的:(Docker,docker,笔记,容器)