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)
sudo apt-get updatesudo apt-get install -y docker.iosudo systemctl start dockersudo systemctl enable dockermacOS & 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:
docker --versionIf 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)
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
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
docker psViewing All Containers (Including Stopped Ones)
docker ps -aStopping Containers
docker stop webserver databaseRemoving Containers
docker rm webserver databaseViewing Container Logs
docker logs webserver4. Connecting Webserver with Database
To enable the webserver to connect to the database, it’s advisable to use Docker Network.
Creating a Docker Network
docker network create my-networkRunning Webserver & Database in the Same Network
docker run -d --name webserver --network my-network -p 80:80 nginxdocker run -d --name database --network my-network -e MYSQL_ROOT_PASSWORD=strongpassword mariadb5. Using Docker for a Website
Copying Website Files to the Webserver
docker cp /path/to/website webserver:/usr/share/nginx/htmlAccessing the Database from the Host
Use a MySQL client to connect to MariaDB:
mysql -h 127.0.0.1 -P 3306 -u root -pIf using Docker Network, replace 127.0.0.1 with database (the container name):
mysql -h database -P 3306 -u root -p6. 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:
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
docker-compose up -d- This command will run the webserver and database within the same network.
- The website files in the
./websitefolder will automatically be loaded into the Nginx container.
Stopping and Removing Containers
docker-compose downConclusion
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.