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
Docker ENTRYPOINT In Dockerfiles, an ENTRYPOINT instruction is used to set executables that will always run when the container is initiated. Unlike CMD commands, ENTRYPOINT commands cannot be ignored or overridden—even when the container runs with command line arguments stated.
All About Docker Compose Override Entrypoint Entrypoint helps use set the command and parameters that executes first when a container is run. In fact, the command line arguments in the following command become a part of the entrypoint command, thereby overriding all elements mentioned via CMD.
When we have specified the ENTRYPOINT instruction in the executable form, it allows us to set or define some additional arguments/parameters using the CMD instruction in Dockerfile. If we have used it in the shell form, it will ignore any of the CMD parameters or even any CLI arguments.
Example of using CMD and ENTRYPOINT togetherIf both ENTRYPOINT and CMD are present, what is written in CMD is executed as an option of the command written in ENTRYPOINT. If an argument is added at the time of docker run , the contents of CMD will be overwritten and the ENTRYPOINT command will be executed.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With