Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19977

Docker - how do i restart nginx to apply custom config?

I am trying to configure a LEMP dev environment with docker and am having trouble with nginx because I can't seem to restart nginx once it has it's new configuration.

docker-compose.yml:

version: '3'
services:
  nginx:
    image: nginx
    ports:
      - '8080:80'
    volumes:
      - ./nginx/log:/var/log/nginx
      - ./nginx/config/default:/etc/nginx/sites-available/default
      - ../wordpress:/var/www/wordpress
  php:
    image: php:fpm
    ports:
      - 9000:9000
  mysql:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - ./mysql/data:/var/lib/mysql

I have a custom nginx config that replaces /etc/nginx/sites-available/default, and in a normal Ubuntu environment, I would run service nginx restart to pull in the new config.

However, if I try to do that this Docker environment, the nginx container exits with code 1.

docker-compose exec nginx sh
service nginx restart

-exit with code 1-

How would I be able use nginx with a custom /etc/nginx/sites-available/default file?

Upvotes: 7

Views: 12345

Answers (3)

Tim Krins
Tim Krins

Reputation: 3819

To reload nginx with docker-compose specifically (rather than restart the whole container, causing downtime):

docker-compose exec nginx nginx -s reload

Upvotes: 5

H Aßdøµ
H Aßdøµ

Reputation: 3055

Basically you can reload nginx configuration by invoking this command:

docker exec <nginx-container-name-or-id> nginx -s reload

Upvotes: 14

BMitch
BMitch

Reputation: 263916

Docker containers should be running a single application in the foreground. When that process it launches as pid 1 inside the container exits, so does the container (similar to how killing pid 1 on a linux server will shutdown that machine). This process isn't managed by the OS service command.

The normal way to reload a configuration in a container is to restart the container. Since you're using docker-compose, that would be docker-compose restart nginx. Note that if this config was part of your image, you would need to rebuild and redeploy a new container, but since you're using a volume, that isn't necessary.

Upvotes: 1

Related Questions