Docker on Mac with Homebrew: A Step-by-Step Tutorial

Docker on Mac with Homebrew: A Step-by-Step Tutorial

Docker on Mac with Homebrew

Docker has revolutionized how developers work by providing an easy way to move code across machines without worrying about dependencies or server versions. It simplifies the operational aspects of running applications. In this post, we’ll show you how to install Docker for macOS using Homebrew.

What Are Homebrew and Homebrew Cask?

If you’re not familiar with Homebrew, it’s a package manager for macOS that simplifies the installation of software via the command line. Homebrew is incredibly popular because it makes it easy to install software packages with a simple command:

brew install [package_name]

However, when it comes to installing GUI software like Docker, you need to use Homebrew Cask.

Why not use brew install docker? By default, Homebrew installs command-line tools. GUI applications, such as Docker, require a different installation method, which is where Homebrew Cask comes in. Instead of manually downloading and dragging the package to your Applications folder, you can install it using a simple command:

brew cask install docker

This makes installing Docker much simpler.

How to Install Homebrew

If you don’t have Homebrew installed yet, you can easily install it by running the following command in your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

This script will explain the steps it will take and pause before proceeding with the installation.

Once Homebrew is installed, you're ready to install Docker.

Installing Docker on Mac with Homebrew Cask

Now that Homebrew is installed, use the following command to install Docker:

brew install --cask docker

Homebrew will download the Docker package and run the installer. After installation, simply follow the Docker GUI to complete the initial configuration.

Verifying the Installation

To ensure Docker is installed correctly, check its version by running:

docker --version

If the version is displayed, Docker is installed successfully. If the command doesn’t return anything, you may need to start the Docker daemon. You can do this by searching for Docker in Spotlight or by finding it in the Applications folder and running it.

Once Docker is running, you should see its icon in the status bar.

Customizing Docker Preferences

After installation, you can customize Docker to suit your needs. Click on the Docker icon in the status bar and select Preferences. In the General tab, you can choose to start Docker automatically when your system starts.

  • Automatic Startup: If you plan to use Docker frequently, it’s a good idea to enable this option so Docker starts automatically when you boot your Mac.

  • Resources: In the Resources section, you can adjust how much CPU, RAM, disk space, and swap space Docker should use. If you're only using Docker occasionally, you can leave these settings lower. If you plan on running multiple containers, you may want to allocate more resources.

Using Docker on Mac with Homebrew

Once Docker is up and running, you can start using it to run containers. To run a container, use the following command:

docker run [image_name]

If the image isn’t already available locally, Docker will try to pull it from Docker Hub, the official Docker registry. If the image exists there, Docker will download it and run the container.

For example, to test the installation, run:

docker run hello-world

This command pulls the hello-world image and runs it. The output should confirm that Docker is working correctly.

Running an Example Container

Let’s run a more detailed example:

docker run -it ubuntu bash

This command runs an interactive Ubuntu container, giving you access to a Bash shell inside the container. You can execute commands as if you were using Ubuntu directly. For instance, to check the Ubuntu version, run:

cat /etc/os-release

The output will display information about the Ubuntu version running in the container.

Port Forwarding in Docker

Port forwarding is another useful feature in Docker, allowing you to expose applications running inside containers to your host machine. For example, if you run an NGINX container and want to access it via your browser on port 8080, use this command:

docker run -d -p 8080:80 nginx

Now, you can access the NGINX server by going to http://localhost:8080 in your browser.

Summary

In this tutorial, we’ve shown you how to install Docker on macOS using Homebrew and Homebrew Cask. After installation, you can run containers, customize Docker settings, and use port forwarding to make applications inside containers accessible from your local machine.

Docker is a powerful tool that simplifies working with containers, and now you have the basics to get started. For more advanced features and usage, check out Docker's official documentation.

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close