Docker-Compose Entrypoint/Command

Viewed 35396

I'm trying add packages (nano) to a container by using my existing docker.compose.yaml file. I can get it to run the script after it deploys but for some reason it keeps looping the script. Is this the intended purpose? TBH i'm pretty new to the whole docker-compose.yaml setup so I've no idea if this is what I'm supposed to be doing!

I've tried changing command: for entrypoint: but I get the same issue.

version: '3.4'

services:
  nextcloud:
    image: nextcloud
    container_name: "nextcloud"
    restart: always
    ports:
      - 8080:80
    volumes:
      - /dconfig/nextcloud/data:/var/www/html/data
      - /dconfig/nextcloud/config:/var/www/html/config
    network_mode: "bridge"
    environment:
      - TZ=Europe/London
      - PGID=1000
      - PUID=1000
    command: /var/www/html/config/install.sh

install.sh

#!/bin/sh
apt-get update
apt-get install -y smbclient nano
1 Answers

A Docker container runs exactly one command, and when that command is done, the container exits. If the container has no entrypoint, it's the command: from docker-compose.yml, any arguments after the image name in a docker run command, or the CMD from the Dockerfile. If it does have an entrypoint (entrypoint:, docker run --entrypoint ..., ENTRYPOINT), it's the entrypoint, which gets passed the command as arguments.

In short: if you pass an entrypoint or command in docker-compose.yml, it gets run instead of the server the container would normally run. There's no plain-Docker way to run a "hook" before or after the main process.

In your particular case, installing software into a container at startup time is an anti-pattern: it has to be repeated every time the container starts up, and startup could fail if the external package repository is down. You can write a very simple Dockerfile, in the same directory as the docker-compose.yml:

FROM nextcloud
RUN apt-get update \
 && apt-get install smbclient

In the docker-compose.yml, change the image: line to say

services:
 nextcloud:
   build: .  # instead of image:

(As a matter of style, I wouldn't set container_name: or network_mode: explicitly, the Docker Compose defaults here are fine. I also wouldn't install nano or any other text editor in a container since any changes you make locally will be lost as soon as you restart the container. While you're still debugging the image I'd also hold off on a restart: always specification, though it's reasonable once you're more convinced everything is working.)

Related