Closed6
Dockerにnginxコンテナ作るよ
docker composeのインストール
ubuntu
sudo apt-get update
sudo apt-get install docker-compose-plugin
バージョン確認
ubuntu
docker compose version
nginxコンテナ起動
ubuntu
vi compose.yaml
compose.yaml
services:
nginx:
image: nginx:1.27.0
ports:
- 8080:80
memo: rootlessモードの場合80番ポートはうまくいかない
memo:latestはつかわないubuntu
$ docker compose up -d
[+] Running 1/1
✔ Container ubuntu-nginx-1 Started
localhostにアクセス
ubuntu
$ curl http://localhost:8080
<!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>
動いてるネ
コンテナ内にあるconfファイルをのぞき見
ubuntu
$ docker compose exec nginx cat /etc/nginx/nginx.conf
/etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
ubuntu
$ docker compose exec nginx ls /etc/nginx/conf.d
default.conf
ubuntu
$ docker compose exec nginx cat /etc/nginx/conf.d/default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
コンテナ内にあるconfをローカルにコピーする
先にローカル保存先のディレクトリを作っておく
ubuntu
mkdir -p nginx/conf.d
起動しているnginxコンテナのIDを調べる
ubuntu
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a5a91d56a5c nginx:1.27.0 "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:8080->80/tcp, :::8080->80/tcp ubuntu-nginx-1
コンテナ内の/etc/nginx/nginx.confをローカルにコピー
ubuntu
$ docker cp 3a5a91d56a5c:/etc/nginx/nginx.conf ./nginx/nginx.conf
Successfully copied 2.56kB to /home/ubuntu/nginx/nginx.conf
コンテナ内の/etc/nginx/conf.d/default.confをローカルにコピー
ubuntu
$ docker cp 3a5a91d56a5c:/etc/nginx/conf.d/default.conf ./nginx/conf.d/default.conf
Successfully copied 3.07kB to /home/ubuntu/nginx/conf.d/default.conf
ローカルのconfファイルを参照してnginxコンテナを起動する
compose.yamlを編集
ubuntu
vi compose.yaml
volumes項目を追加
compose.yaml
services:
nginx:
image: nginx:1.27.0
ports:
- 8080:80
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
volumesとは
コンテナ再起動ubuntu
docker compose restart
動作確認
ubuntu
~$ curl http://localhost:8080
<!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>
動いてるネ
ポート番号とタイムゾーンを変数にする
ubuntu
vi .env
.env
WEB_PUBLISHED_PORT=8080
TZ=Asia/Tokyo
ubuntu
vi compose.yaml
compose.yaml
services:
nginx:
image: nginx:1.27.0
ports:
- ${WEB_PUBLISHED_PORT:-80}:80
environment:
TZ: ${TZ}
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
このスクラップは4ヶ月前にクローズされました