docker建立nginx

晓谪發表於2024-03-28

最後修改時間2024-03-28

作業系統
Linux version 5.15.0-86-generic
(buildd@lcy02-amd64-086)
(gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, GNU ld (GNU Binutils for Ubuntu) 2.38)
#96-Ubuntu SMP Wed Sep 20 08:23:49 UTC 2023

查詢nginx1.24

sudo docker search nginx:1.24

拉nginx1.24

sudo docker pull nginx:1.24

檢視docker映象,檢查是否已經拉取nginx映象

sudo docker images

建立nginx相關資料夾

mkdir /home/ubuntu/nginx
mkdir /home/ubuntu/nginx/{logs,html,conf}
mkdir /home/ubuntu/nginx/conf/conf.d

建立nginx容器

sudo docker create \
--name testNginx \
-p 80:80 \
7f0fd59e0094
sudo docker create \
--name nginx_test \
-p 80:80 \
-v /home/ubuntu/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /home/ubuntu/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /home/ubuntu/nginx/logs:/var/log/nginx \
-v /home/ubuntu/nginx/html:/usr/share/nginx/html \
-v /etc/localtime:/etc/localtime:ro \
7f0fd59e0094

建立可後臺執行的nginx容器

sudo docker run \
--name nginx_test \
-p 80:80 \
-d 7f0fd59e0094
sudo docker run \
-p 80:80 \
--name nginx_test \
-v /home/ubuntu/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /home/ubuntu/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /home/ubuntu/nginx/logs:/var/log/nginx \
-v /home/ubuntu/nginx/html:/usr/share/nginx/html \
-v /etc/localtime:/etc/localtime:ro \
-d 7f0fd59e0094

後臺執行建立的nginx容器

sudo docker start 容器id &

進入nginx容器

sudo docker exec -it 容器id /bin/bash

複製nginx配置檔案

sudo docker cp 容器id:/etc/nginx/nginx.conf /home/ubuntu/nginx
sudo docker cp 容器id:/usr/share/nginx/html /home/ubuntu/nginx/html
sudo docker cp 容器id:/etc/nginx/conf.d /home/ubuntu/nginx/conf/conf.d

修改nginx配置檔案使用者組、使用者許可權

sudo chown -R ubuntu:ubuntu /home/ubuntu/nginx
sudo chmod -R 775 /home/ubuntu/nginx

相關文章