Using dockerfile to extend base docker-compose installation

I am running a series of docker containers on an Intel NUC7CJYHN:

$ uname -a
Linux [server name] 5.10.0-10-amd64 #1 SMP Debian 5.10.84-1 (2021-12-08) x86_64 GNU/Linux

My nextcloud folder contains the following files:

docker-compose.yml
dockerfile
.env

The docker-compose file is a simple set up with a mysql database:

version: "3"

services:
  nextcloud:
    image: nextcloud:latest
    restart: unless-stopped
    ports:
      - 8088:80
    environment:
      - MYSQL_HOST=mysql
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=${PASSWORD}
    volumes:
      - ${APPDATA}/html:/var/www/html

  mysql:
    image: mysql:8.0
    restart: unless-stopped
    environment:
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=${PASSWORD}
      - MYSQL_ROOT_PASSWORD=${PASSWORD}
    volumes:
      - ${APPDATA}/mysql:/var/lib/mysql

volumes:
  mysql:
  nextcloud:

There is also a dockerfile with the following:

FROM nextcloud:latest

RUN apt-get update && apt-get install -y procps smbclient && rm -rf /var/lib/apt/lists/*

Nextcloud starts successfully, and I am able to access the web interface and perform initial set up. I have also been able to access the front end via a Nginx Proxy Manager defined subdomain.

What I want to happen is to issue the command docker-compose up --detach and the image to be pulled and installed and then the dockerfile to be executed to install various addons. I can’t find any documentation on how to do this.

There are some dockerfile examples on the official github page but there are no examples of how to implement these. There are no dockerfile results when searching in the official documents, either.

I’m looking for something that I can turn into configuration as code to ensure this is repeatable. Any guidance or suggestions?

I have been able to access the container shell:

$ sudo docker exec -it nextcloud_nextcloud_1 /bin/bash

From there I can run these commands:

# apt-get update && apt-get install -y procps smbclient && rm -rf /var/lib/apt/lists/*

However, on upgrade or recreation of the container, these changes will disappear. I have confirmed they can be applied, though.

this snippet of docker-compose.yml file will build a service nextcloud from the Dockerfile inside of subdirectory ./nextcloud located in $PWD once you run docker-compose up

version: '3'
services:
  nextcloud:
    build:
      context: ./nextcloud
      dockerfile: Dockerfile
    container_name: nextcloud

you should not do this as changes within existing container are not persistent and lost if you rebuild it. You should rather use Dockerfile as in the sample above using official examples provided on docker hub…

I have confirmed that the following command works:

$ sudo docker build -t nextcloud .
Sending build context to Docker daemon  687.1MB
Step 1/2 : FROM nextcloud:latest
 ---> 453a63c1def9
Step 2/2 : RUN apt-get update && apt-get install -y procps smbclient && rm -rf /var/lib/apt/lists/*
 ---> Running in 8321750abbfa
[...]
Successfully built b459f388c1ee
Successfully tagged nextcloud:latest

Next step is to add the config specified by @wwe to my docker-compose and test this.

First, here’s the docker-compose.yml file:

version: '3'

services:
  nextcloud:
    build:
      context: ${APPDATA}
      dockerfile: Dockerfile
    container_name: nextcloud
    restart: unless-stopped
    ports:
      - 8088:80
    environment:
      - MYSQL_HOST=mysql
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=${PASSWORD}
    volumes:
      - ${APPDATA}/html:/var/www/html

  mysql:
    image: mysql:8.0
    restart: unless-stopped
    container_name: nextcloud_mysql
    environment:
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=${PASSWORD}
      - MYSQL_ROOT_PASSWORD=${PASSWORD}
    volumes:
      - ${APPDATA}/mysql:/var/lib/mysql

volumes:
  mysql:
  nextcloud:

I have a .env file with the following name/value pairs:

APPDATA=/srv/nextcloud
PASSWORD=[password]

I have a Dockerfile with the following contents:

FROM nextcloud:latest

RUN apt-get update && apt-get install -y procps smbclient && rm -rf /var/lib/apt/lists/*

The following commands are issued:

$ sudo docker-compose up --detach
Creating network "nextcloud_default" with the default driver
Building nextcloud
Sending build context to Docker daemon  687.1MB
Step 1/2 : FROM nextcloud:latest
[...]
Step 2/2 : RUN apt-get update && apt-get install -y procps smbclient && rm -rf /var/lib/apt/lists/*
[...]
WARNING: Image for service nextcloud was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating nextcloud         ... done
Creating nextcloud_mysql ... done

Now when I access my Nextcloud host, I can see that the smbclient has been installed. This works, and will support upgrades!

1 Like