Step 1: Install Docker Desktop on macOS
1.1 Install Homebrew (If you don’t have it already)
First, you’ll need Homebrew if you don’t have it installed. Open your Terminal and run the following command to install it:
1.2 Install Docker using Homebrew
Once Homebrew is installed, you can install Docker by running:
This command installs Docker Desktop, which includes both Docker Engine and Docker Compose.
1.3 Start Docker Desktop
After installation, open Docker Desktop from your Applications folder. The application will launch, and you’ll see a Docker icon in the menu bar at the top of the screen.
Docker will start automatically, and you’ll be able to access Docker from the command line.
Step 2: Verify Docker Installation
2.1 Check Docker Version
Once Docker Desktop is running, open Terminal again and check if Docker was installed successfully by running:
This should return something like:
2.2 Check Docker Compose Version
If you also want to check if Docker Compose is installed, run:
You should see something like:
Step 3: Run a Simple Test Container
3.1 Run hello-world
Test
To verify that Docker is working, run the following command to download and run a hello-world container:
This command will download the hello-world image from Docker Hub (if you don’t already) and run it in a container. You should see a message that says:
Step 4: Running Applications in Docker
Now that Docker is installed and working, let’s run an actual application inside a Docker container. We'll run a MySQL database in a Docker container for this example.
4.1 Pull MySQL Docker Image
First, let’s pull the official MySQL image from Docker Hub. In your terminal, run:
This will download the MySQL Docker image to your local system.
4.2 Run a MySQL Container
Now, run the MySQL container with the following command:
Let’s break down the command:
-
--name mysql-container
gives the container a name (mysql-container
). -
-e MYSQL_ROOT_PASSWORD=root
sets the root password for the MySQL instance. -
-d
runs the container in detached mode (in the background). -
mysql
is the name of the Docker image.
You should now have a MySQL container running in the background.
4.3 Verify MySQL Container is Running
You can verify the container is running by using:
This will list all the running containers. You should see mysql-container
listed in the output.
Step 5: Access the Running MySQL Container
Now, let’s connect to the MySQL container to interact with the database.
5.1 Access MySQL Command Line in the Container
Run the following command to access the MySQL shell inside the container:
This command does the following:
-
exec -it
allows you to run commands inside the container interactively. -
mysql-container
is the name of the container. -
mysql -u root -p
runs the MySQL client with the root user and prompts for the password.
Enter the password (root
in our case) when prompted.
5.2 Run MySQL Queries
Once inside the MySQL shell, you can start running queries. For example, you can create a database like this:
And show the databases with:
Step 6: Stop and Remove the Container
6.1 Stop the Running Container
When you're done, you can stop the running container using:
6.2 Remove the Container
If you no longer need the container, you can remove it with:
Step 7: Running Other Applications (Example: Nginx)
To run another application, like Nginx, you can follow similar steps.
7.1 Pull Nginx Docker Image
Run this command to pull the official Nginx image:
7.2 Run the Nginx Container
Now, run the Nginx container:
-
--name nginx-container
gives the container a name (nginx-container
). -
-p 8080:80
maps port 80 in the container to port 8080 on your local machine. -
-d
runs the container in detached mode (in the background). -
nginx
is the name of the image.
7.3 Access the Nginx Server
You can now open your browser and go to http://localhost:8080
. You should see the default Nginx welcome page.
Step 8: Docker Compose (Optional)
If you want to run multi-container applications, you can use Docker Compose. This allows you to define multiple containers in a single docker-compose.yml
file.
8.1 Create a docker-compose.yml
File
Here’s an example docker-compose.yml
file for running a MySQL and Nginx application together:
8.2 Start the Application with Docker Compose
Run the following command to start both containers:
This will start both the MySQL and Nginx containers as defined in the docker-compose.yml
file.