Apache on a Mac Using Docker

In this short article I’m going to show you how to setup Apache on macOS using Docker.

Why use Docker? macOS includes a build in Apache server, but whereas in days’ past you could easily enable it with a simple checkbox, nowadays you need a bit of Apache knowledge to activate it.

I’m not sure why Apple discontinued official support of it. Perhaps the version included in macOS isn’t secure enough, but there are other options, one option being XAMPP if you like to avoid the command line.

But, if you’re reasonably comfortable with the command line then the Docker approach may be for you. I’ve tried to write this guide assuming little familiarity with the concepts of Docker, especially as I’m incredibly new to it myself.

  1. Install Docker for MacThankfully, installing Docker on a Mac is straight forward. Head here to download and install it. Once installed it’ll sit happily as an icon in your menubar.
  2. Start Docker MachineThe Docker Machine is a virtual machine which will run your Apache container. Open up a command line and run the following:
    docker-machine start && eval $(docker-machine env)
    

    All being well, you should see something like this:

    Starting "default"...
    (default) Check network to re-create if needed...
    (default) Waiting for an IP...
    Machine "default" was started.
    Waiting for SSH to be available...
    Detecting the provisioner...
    Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.
    
  3. Pull Down and Start linode/lampAssuming your website is in ~/Sites, run the following which will pull down an image for a full LAMP stack and start it:
    sudo docker run -p 80:80 -v ~/Sites:/var/www/example.com/public_html -t -i linode/lamp /bin/bash
    

    You’ll need to supply your user password.

    Again, if everything goes according to plan, you’ll see a new prompt like this:

    root@d3e5bfcbb42f:/#
    
  4. Start ApacheFinal task, start Apache:
    root@d3e5bfcbb42f:/# service apache2 start
    * Starting web server apache2
    *
    

Now open your web browser and type localhost in as the URL. Hopefully you’ll see your website as contained within ~/Sites!Mac Homepage

This is a very basic guide. If you’re like me, you probably have subdomains linking to different directories, and to get that working we’ll need to modify the image to our needs, and that’s a challenge for another time.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: