Docker Installation

Docker Installation

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:

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

1.2 Install Docker using Homebrew

Once Homebrew is installed, you can install Docker by running:

brew install --cask docker

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:

docker --version

This should return something like:

Docker version 20.10.7, build f0df350

2.2 Check Docker Compose Version

If you also want to check if Docker Compose is installed, run:

docker-compose --version

You should see something like:

docker-compose version 1.29.2, build 5becea4c

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:

docker run hello-world

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:

Hello from Docker! This message shows that your installation appears to be working correctly.

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:

docker pull mysql

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:

docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=root -d mysql

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:

docker ps

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:

docker exec -it mysql-container mysql -u root -p

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:

CREATE DATABASE my_database;

And show the databases with:

SHOW DATABASES;

Step 6: Stop and Remove the Container

6.1 Stop the Running Container

When you're done, you can stop the running container using:

docker stop mysql-container

6.2 Remove the Container

If you no longer need the container, you can remove it with:

docker rm mysql-container

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:

docker pull nginx

7.2 Run the Nginx Container

Now, run the Nginx container:

docker run --name nginx-container -p 8080:80 -d nginx
  • --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:

version: '3' services: mysql: image: mysql environment: MYSQL_ROOT_PASSWORD: root ports: - "3306:3306" nginx: image: nginx ports: - "8080:80" depends_on: - mysql

8.2 Start the Application with Docker Compose

Run the following command to start both containers:

docker-compose up

This will start both the MySQL and Nginx containers as defined in the docker-compose.yml file.

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close