Post

Guide to Using Command Line for Docker Management

Docker is a powerful containerization platform that simplifies the management of applications within isolated environments. With Docker, you can quickly install, run, and manage various application components, such as webservers and databases, in separate containers.

This article will walk you through the essential command line instructions for managing Docker, particularly in the context of developing a website with a webserver and MariaDB running in separate containers.

1. Installing Docker

Before diving in, ensure that Docker is installed on your operating system.

Linux (Ubuntu/Debian)

1
2
3
4
sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker

macOS & Windows

Download and install Docker Desktop from Docker Hub. Once the installation is complete, ensure Docker is running on your system.

Verifying the Installation

After installation, verify that Docker is correctly installed:

1
docker --version

If successful, you’ll see the installed Docker version displayed.

2. Running Docker Containers

With Docker installed, you can start running containers for your webserver and database.

Running a Webserver (Nginx)

1
docker run -d --name webserver -p 80:80 nginx
  • -d → Runs the container in the background.
  • --name webserver → Names the container “webserver”.
  • -p 80:80 → Maps port 80 on the host to port 80 on the container.
  • nginx → Uses the official Nginx image from Docker Hub.

Running MariaDB

1
docker run -d --name database -e MYSQL_ROOT_PASSWORD=strongpassword -p 3306:3306 mariadb
  • -e MYSQL_ROOT_PASSWORD=strongpassword → Sets the root password for MariaDB.
  • -p 3306:3306 → Maps port 3306 on the host to the container.
  • mariadb → Uses the official MariaDB image from Docker Hub.

3. Managing Docker Containers

Viewing Running Containers

1
docker ps

Viewing All Containers (Including Stopped Ones)

1
docker ps -a

Stopping Containers

1
docker stop webserver database

Removing Containers

1
docker rm webserver database

Viewing Container Logs

1
docker logs webserver

4. Connecting Webserver with Database

To enable the webserver to connect to the database, it’s advisable to use Docker Network.

Creating a Docker Network

1
docker network create my-network

Running Webserver & Database in the Same Network

1
2
docker run -d --name webserver --network my-network -p 80:80 nginx
docker run -d --name database --network my-network -e MYSQL_ROOT_PASSWORD=strongpassword mariadb

5. Using Docker for a Website

Copying Website Files to the Webserver

1
docker cp /path/to/website webserver:/usr/share/nginx/html

Accessing the Database from the Host

Use a MySQL client to connect to MariaDB:

1
mysql -h 127.0.0.1 -P 3306 -u root -p

If using Docker Network, replace 127.0.0.1 with database (the container name):

1
mysql -h database -P 3306 -u root -p

6. Using Docker Compose for Multi-Container Management

To simplify managing multiple containers, use Docker Compose.

Creating a docker-compose.yml File

Create a docker-compose.yml file with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
version: '3.8'
services:
  webserver:
    image: nginx
    container_name: webserver
    ports:
      - "80:80"
    volumes:
      - ./website:/usr/share/nginx/html
    networks:
      - my-network

  database:
    image: mariadb
    container_name: database
    environment:
      MYSQL_ROOT_PASSWORD: strongpassword
    ports:
      - "3306:3306"
    networks:
      - my-network

networks:
  my-network:

Running Docker Compose

1
docker-compose up -d
  • This command will run the webserver and database within the same network.
  • The website files in the ./website folder will automatically be loaded into the Nginx container.

Stopping and Removing Containers

1
docker-compose down

Conclusion

Docker offers a flexible solution for managing container-based applications. With a few command line instructions, you can easily install, run, and manage web servers and databases within isolated environments.

Using Docker Network enables communication between containers, and Docker Compose simplifies managing multiple containers through a single configuration file.

With this guide, you’re well-equipped to start building and managing Docker-based applications more efficiently. 🚀

If you have any questions, feel free to discuss or visit the official Docker documentation for further information.

This post is licensed under CC BY 4.0 by the author.