DEV Community

Cover image for Modern Web Development with Docker and Docker Compose
naskovic
naskovic

Posted on

Modern Web Development with Docker and Docker Compose

Docker and Docker Compose have become essential tools for modern web development. These tools allow developers to easily create and manage containerized environments for their projects, making it easier to develop and deploy applications.

In this blog post, we will look at two simple examples of using Docker Compose for web development: one for a PHP application using a MySQL database and PHPMyAdmin, and another for a Node-React development environment.

PHP Application with MySQL and PHPMyAdmin

To get started with a PHP application using Docker Compose, you will need to create a docker-compose.yml file in the root of your project. In this file, you can define the services that make up your application, as well as any volumes or networks that they may need.

Here is an example docker-compose.yml file for a PHP application using a MySQL database and PHPMyAdmin:

version: '3'
services:
  web:
    build: .
    ports:
      - 80:80
    volumes:
      - .:/var/www/html
    depends_on:
      - db
  db:
    image: mysql:5.7
    volumes:
      - db-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: user
      MYSQL_PASSWORD: password
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
      - 8080:80
    environment:
      PMA_HOST: db
      PMA_USER: root
      PMA_PASSWORD: password
volumes:
  db-data:
Enter fullscreen mode Exit fullscreen mode

In this example, we have defined three services: web, db, and phpmyadmin. The web service is built from the current directory and exposes port 80 on the host machine. The db service is based on the MySQL image and creates a volume for the MySQL data. The phpmyadmin service is based on the PHPMyAdmin image and exposes port 8080 on the host machine.

To start these services, you can simply run the following command:

docker-compose up
Enter fullscreen mode Exit fullscreen mode

This will build and start all of the services defined in the docker-compose.yml file. You can then access your PHP application at http://localhost, and PHPMyAdmin at http://localhost:8080.

Node-React Development Environment

To set up a Node-React development environment with Docker Compose, you can use a similar approach to the PHP example above. Here is an example docker-compose.yml file for a Node-React development environment:

version: '3'
services:
  web:
    build: .
    ports:
      - 3000:3000
    volumes:
      - .:/app
  db:
    image: mongo
    volumes:
      - db-data:/data/db
volumes:
  db-data:

Enter fullscreen mode Exit fullscreen mode

In this example, we have defined two services: web and db. The web service is built from the current directory and exposes port 3000 on the host machine. The db service

Working with Docker Compose

Once you have your docker-compose.yml file set up, you can use the following commands to manage your services:

  • docker-compose up: Build and start all services defined in the docker-compose.yml file.
  • docker-compose up -d: Build and start all services in the background.
  • docker-compose down : Stop and remove all services and networks created by docker-compose up.
  • docker-compose exec <service> <command>: Run a command in the context of a service. For example, docker-compose exec web bash will open a bash terminal in the web service.

Using Docker Compose can greatly simplify the process of setting up and managing development environments, especially when working on larger projects with multiple dependencies. It is a powerful tool that every modern web developer should be familiar with.

I hope this text has been helpful in introducing you to Docker and Docker Compose for web development. With these tools, you can easily set up and manage containerized environments for your projects, making it easier to develop and deploy applications.

Top comments (0)