Search code examples
macosdockerdocker-composedocker-for-mac

What's the simplest mac docker setup for archival purposes?


I've got a bunch of old sites (wordpress/mysql/php, ruby 1.x/sqlite, etc) on my computer that I'd like to ensure easy access to in the future without having to muck up my environment.

Docker seems like the perfect candidate for this task, but I have tried to wrap my head around it many times and have realized it's time to ask for professional help (which brings me here).

I have wasted so much time messing with this thing, and have been slightly overwhelmed with depth. At first (in the case of the wp/mysql problem), I was trying to create two different images (a wordpress/php one, and a mysql one) and link them together, which appeals to my programmer-mentality of Doing It The Right Way™.

But my UX mentality has won out and I'm abandoning the Right Way™ in favor of getting this thing working in the simplest way so that future me (who will forget all this docker knowledge as soon as I complete this task) might be able to figure it out again.

So here's what I want: A docker setup that I can put in a folder along with with an exported mysql database and a wordpress site, so when I start up that badboy—boom—I'm browsing some old site that made a lot of sense in 2005 and makes no sense now.

Know what I'm saying? What's the simplest, future-proof way I can get this done? Is it possible to keep the data/files outside of the containers in the case I wanted to edit them? I'm using Docker For Mac.


Solution

  • It sounds like you want something really generic that's going to work for all the sites you have. That might be tricky because Docker is inherently not generic. You don't typically have a docker image with all the tools (PHP, Ruby, etc) to run everything. You typically build only what you need into an image.

    With that being said, it might still be possible to do something like what you're asking for, and I think I can get you pointed in the right direction. The official Wordpress Docker image should be able to run your Wordpress sites. You were actually on the right track with a separate MySQL image, and this is easy to achieve with docker-compose.

    Your docker-compose.yml file would look something like this:

    version: '3'
    
    services:
        wordpress:
            image: wordpress:4-php5.6-apache
            ports:
                - "8080:80"
            volumes:
                - ./:/var/www/html
            environment:
                WORDPRESS_DB_HOST: mysql
                WORDPRESS_DB_USER: root
                WORDPRESS_DB_PASSWORD: password
                WORDPRESS_DB_NAME: wordpress
    
        mysql:
            image: mariadb:10.1
            ports:
                - "3306:3306"
            environment:
                MYSQL_ROOT_PASSWORD: password
                MYSQL_DATABASE: wordpress
    

    A brief summary of what that does:

    • Creates 2 Docker containers, one for Wordpress and one for MySQL.
    • Forwards requests from 127.0.0.1:8080 to the Wordpress container on port 80.
    • Mounts ./ to /var/www/html in the wordpress container. Meaning it will serve Wordpress from your current directory.
    • Configures environment variables so it knows how to connect to the database.
    • Prepares a MySQL Docker container.
    • Forwards 127.0.0.1:3306 to 3306 on the container. So you can do mysql -u root -ppassword -h 127.0.0.1.

    Now, if you create a file called docker-compose.yml similar to the one above in the Wordpress directory you want to serve, you can simply run docker-compose up in that directory and you'll be running Wordpress in a container. If you need to restore a database dump, you can cat dump.sql | mysql -u root -ppassword -h 127.0.0.1 WORDPRESS. And you can access the site at localhost:8080. By putting a docker-compose.yml file like this in your projects, it would be pretty quick to spin up a container for them.

    Now, because the scope of a StackOverflow answer like this is pretty limited, you'll probably run into a couple snags here and there that I didn't cover in this answer. But if you're willing to invest a little time learning more about Docker (it's really not that complex), this could be a great solution for you.