Mastering Docker on Mac: No Desktop Required!

Published on

Mastering Docker on Mac: No Desktop Required!

Docker has transformed the way we develop, ship, and run applications by providing a consistent environment across various stages of deployment. While the Docker Desktop application makes it easy for many developers, Mac users can also harness the power of Docker without relying on the GUI-based Docker Desktop. In this blog post, we will explore how you can set up Docker on a Mac without using Docker Desktop, as well as some essential commands for getting started.

Why Use Docker Without Docker Desktop?

  1. Resource Efficiency: Docker Desktop can consume considerable system resources. Running Docker through a terminal can optimize your Mac’s performance by using only what's necessary.

  2. Flexibility: Not using Docker Desktop can provide more control over your Docker installation and its features. It allows for custom configurations that may be limited in the GUI.

  3. Learning Experience: Command line skills are invaluable for DevOps professionals. Working with Docker directly in the terminal deepens your understanding of the container technology.

Prerequisites

Before we begin, you’ll need the following:

  • A Mac running macOS version 10.12 or later.
  • Homebrew installed.
  • Basic knowledge of the command line.

Setting Up Docker in Your Terminal

Step 1: Install Docker CLI

Docker's command-line interface (CLI) is sufficient for building and running containers. To install it, you can use Homebrew. Open your terminal and run:

brew install docker

This command does not install the Docker engine, but it gives you the Docker CLI tools. To leverage Docker entirely, you will need to connect to a Docker daemon.

Step 2: Install Docker in a VM

Since Docker requires a Linux kernel, we can use a lightweight VM called "Docker Machine." This tool allows you to create and manage virtual machines for Docker.

To install Docker Machine, run:

brew install docker-machine

Now, let's create a virtual machine to run your Docker containers:

docker-machine create --driver virtualbox default

By using VirtualBox, Docker Machine will create a Linux VM named "default." You can replace 'default' with any name you wish to assign to the VM.

Step 3: Start the Docker Machine

To start the Docker machine you just created, run:

docker-machine start default

Step 4: Configure the Environment

Once your Docker machine is running, configure your shell to connect to it:

eval $(docker-machine env default)

This command sets environment variables so that Docker commands use the specified machine.

Step 5: Verify the Installation

To confirm that Docker is set up correctly, run:

docker run hello-world

If Docker is configured correctly, you should see a message indicating that your installation appears to be working.

Getting Started with Docker Commands

Let’s look at some essential Docker CLI commands that will help you manage your containers effectively.

Pulling an Image

To work with Docker, you'll often need Docker images. You can pull an image from Docker Hub using:

docker pull nginx

This command downloads the latest Nginx image. But why Nginx? It's a popular web server that you can use for testing purposes.

Running a Container

To run a Docker container, use:

docker run -d -p 8080:80 nginx
  • -d: Run container in detached mode (background).
  • -p: Map port 80 in the container to port 8080 on your Mac, allowing you to access the web server via http://localhost:8080.

Stopping a Container

If you wish to stop the container, first find its ID or name:

docker ps

Then, stop it using:

docker stop <container_id_or_name>

Removing a Container

To remove the stopped container, run:

docker rm <container_id_or_name>

This command is useful for cleaning up resources.

Managing Volumes

Volumes are crucial for maintaining data persistence in Docker. You can create a volume with:

docker volume create my-volume

Then, you can use it when running a container:

docker run -d -p 8080:80 -v my-volume:/usr/share/nginx/html nginx

In this example, we are attaching the volume 'my-volume' to the Nginx container, allowing you to persist data.

Networking in Docker

Networking allows communication between containers and your host. For instance, you can create a bridge network as follows:

docker network create my-network

You can then run containers connected to this network:

docker run -d --network my-network --name my_nginx nginx

This provides a way to group containers logically.

Advanced Docker Usage

Docker-Compose Installation

When your applications grow in complexity, managing multiple Docker containers becomes a challenge. Docker Compose serves as a powerful tool to define and manage multi-container Docker applications.

To install Docker Compose, run:

brew install docker-compose

Creating a docker-compose.yml File

Here’s an example of a simple docker-compose.yml file to run a WordPress application with MySQL:

version: '3.1'
services:
  wordpress:
    image: wordpress:latest
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: user
      WORDPRESS_DB_PASSWORD: password
      WORDPRESS_DB_NAME: mydatabase
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: user
      MYSQL_PASSWORD: password

To start the services defined in docker-compose.yml, use:

docker-compose up -d

Closing the Chapter

Using Docker on a Mac without the Desktop app is not just possible but can be rewarding. You gain resource efficiency, flexibility, and solid command-line skills. Whether you are a seasoned DevOps engineer or a novice, mastering this setup offers you enhanced control over your applications in a containerized environment.

As you delve deeper into Docker, remember to explore the extensive documentation available here. And if you’re interested in understanding more about Docker networking and its intricacies, check out this valuable resource Docker Networking.

Happy Docking!