Owncloud on Docker

Migrating ownCloud to Docker

Migrating software is always a challenge in some respect. It may be that software needs to go from one server to another, one datacenter to another, or even from on premise to cloud hosting. Containerizing software too faces the same sorts of challenges. One such software suite is ownCloud – ownCloud is a PHP based application that supplies a self-hosted, Dropbox like experience for its user. It has for this reason become hugely popular with enthusiasts and companies alike who don’t want to pay for SaaS or prefer to keep things more private rather than shared. regardless of the motivation, ownCloud is increasingly being containerized.

Fortunately, most of the heavy lifting for ownCloud is already done. Docker provides a pre-built image that already has all the dependencies need to run ownCloud. All one needs to do is build on the image with one’s existing ownCloud instance and one is is up and running. Likewise, if one plans on migrating the database too, Docker has a pre-built MySQL image that provides all the software and dependencies and it is ready to use. Because the heavy lifting is done, moving to Docker is actually pretty simple.

The major difference between traditional means of hosting ownCloud and using containers has to do with the way persistent data is stored. Containers by nature are intended to be disposable, meaning that in anything that goes into the container can be discarded at any point without loss. The implication is that everything that cannot stand be lost should be stored outside the container. Docker provides a means of doing this through Docker Volumes. In both the cases of MySQL and ownCloud, there is persistent data that should be stored on volumes: the database and the actual files respectively.

Owncloud on Docker
Owncloud on Docker

Note: The instructions here are designed to be a general purpose guide and will lack specifics in certain areas because every ownCloud setup is unique in some way. You’ll need to fill in the blanks where necessary.

The source files for this post can be downloaded here:

MySQL

Docker Hub provides many versions of MySQL that are ready to run. These images have the ability to import a database when a container starts for the first time. This feature is useful for migrating data to a containerized instance of MySQL. This step is only necessary if you intend to use a container for the database as well. Many services running in containers do not necessarily use containerized databases, albeit it is possible and acceptable, particularly for smaller installations.

  1. Backup the source database.
    • If you have access to the console on the machine running MySQL, you can use mysqldump.
     mysqldump --databases owncloud-database -u username -p > owncloud.sql
    

    This will prompt for a password for the username with permissions to dump the database. Enter the password and it will create a .sql file. Replace ownCcoud-database with the name of the ownCloud database and username with the user that can dump the database. Download the file to your local computer once you’re finished.

    • If you don’t have console access, odds are you have access through phpMyAdmin. This portal allows you to do all kinds of things with MySQL databases.
      1. Log in to the phpMyAdmin
      2. Click the Export tab
      3. From the database drop-down (left navigation), select the ownCloud database.
      4. Choose Quick export.
      5. For the format select SQL.
      6. Click the Go button.
      7. Save the file – This will download a .sql file with the name of the database.
  2. Move the .sql to the same folder where the Dockerfile for the MySQL image is located.
  3. Edit the Dockerfile. Change the second line of the file so that the first parameter (yourdatabase.sql) in the ADD statement matches the name of the database from from the backup. After editing, save he Dockerfile.
     ADD yourdatabase.sql /docker-entrypoint-initdb.d/owncloud.sql
    
  4. Build the image. With the Docker client connected and the current directory set to the directory with the Dockerfile, run Docker build. This will create a container image in a local image registry from the generic MySQL image on Docker Hub.
     docker build --no-cache --tag oc-mysql .
    
  5. Create a folder on the Docker host for the MySQL data path. This will be used for the data persistence.
     mkdir /var/some/path/
    
  6. Create a Docker network. This network will be used by ownCloud and MySQL internally on Docker.
     docker network create oc-net
    
  7. Run the image as a container. Notice the -v parameter maps the created folder to the mysql folder in the container. The container will now write persistent data to the host’s file system outside of the container. Also, note the MYSQL_ROOT_PASSWORD. Set this password to whatever you use for the root on MySQL. Note the –net parameter that specifies the created network .
      docker run -dit --name oc-mysql -v /var/some/path:/var/lib/mysql --net oc-net -h oc-mysql -e MYSQL_ROOT_PASSWORD=yourpassword oc-mysql
    

Once started, the container is ready to use.

ownCloud

The easiest way to migrate ownCloud is to move the entire ownCloud folder (without the data) from the source to a container.

  1. Download the ownCloud folder – everything except the /data folder. This can be done through an SFTP client, SCP, or some other utility. The location of the application will vary depending on the host, but most of the time it is found in /var/www/html. The html folder is usually the “root” folder for the application. It includes a number of other folders including apps, config, and data. Download all the files and folders except the data folder.
  2. Migrate the ownCloud data. By moving the data, we’re talking about all the files that are tracked by ownCloud meta-data that goes along with it. This is typically found in the /var/www/html/data folder. This data will need to be move to a place that is accessible by the Docker host machine. There are any number of ways to do this, including a straight copy, mounting a remote share, copying to an external drive and mounting the drive on the Docker host, among many other methods. Additionally, Docker provides a number of volume drivers that can be used to access a variety of different kinds of data. Moving the data may not even be necessary of the data is on the same host already.The bottom line is that regardless of how the data is moved and accessed by the host, there needs to be a path to the data, such as /var/oc-data or some other path. This path will be mounted in the ownCloud container as a volume.
  3. Create a folder in the same folder where the Dockerfile for ownCloud resides called owncloud.
  4. Move the downloaded files and folder downloaded from the source to the owncloud folder that was just created.
  5. Edit the Dockerfile. Set the FROM directive to be the closest match version for your installation of ownCloud. You can find the available images here. Find the closest one with -apache. For instance, ownCloud 8.2.9 would use 8-apache. Copy this tag and paste it in the Dockerfile. After editing, save he Dockerfile.
     FROM owncloud:8-apache
    
  6. Open the config.php in the ownCloud/config folder in a text editor. Edit the database parameters to match those used for the MySQL container or your MySQL database. If you did not move the database, then these can probably be used as is. Otherwise, set dbhost to oc-mysql, the dbuser to root, and dbpassword to the password used for the MYSQL_ROOT_PASSWORD parameter on the MySQL container. Save the file when completed.
     'dbhost' => 'oc-mysql',
     'dbuser' => 'root',
     'dbpassword' => 'yourpassword',
    
  7. Also, make note of the path of the datadirectory parameter. This points to the place where ownCloud will expect the data files for the application to reside. In most common cases, this is /var/www/html/data, however it may vary depending on the installation.
  8. Build the image with Docker build. This will create a container image in a local registry from the generic image on Docker Hub.
     docker build --no-cache --tag oc-app .
    
  9. Run the image as a container. Note the -v container mounts a volume in the container that points to folder in the datadirectory parameter in the config.php. The –net specifies the network to use. If you didn’t create this network in the previous step and aren’t using a containerized database, remove –net oc-net. -p is the port mapping for the app. ownCloud can run on any port, but the standard HTTP port is 80.
      docker run -dit --name oc-app --net oc-net -v /path/to/data:/var/www/html/data -h oc-app -p 80:80 oc-app
    
  10. ownCloud uses a trusted_domain setting in the config.php file that allows connections on a paticular domain. This may or may not be enable. Update your DNS records to reflect the IP of the host so that ownCloud clients can find it.
  11. Once ownCloud is up and running, you should be able to point your browser to the host’s IP anddress and load ownCloud.
     http://aaa.bbb.ccc.ddd
    

Conclusion

Migrating ownCloud itself is usually not a difficult task, but if can be time consuming. The real advantages to running in containers through are realized through application portability and scalability. ownCloud on a container can be setup to scale with network demands with more instances of the application, and in more complex setups, it can take advantage of the advanced networking features in Docker to create high availability and cross-zone configurations without having to build many of the features manually.

1 comment on “Migrating ownCloud to DockerAdd yours →

Leave a Reply

Your email address will not be published. Required fields are marked *