Streamlining Web system development using docker-compose and Git

Introduction

I posted an article with the title [Efficient Web system development using containers and Git], but after that I also tried using docker-compose I tried it, so I will write down the contents at that time as a memo.

Execution environment

[Docker installation environment] ・ Ubuntu 20.04 LTS (on GCP) ・ Docker 19.03.13 ・ Docker-compose 1.25.0

[Container environment] ・ Image Ubuntu: 20.04 -Apache 2.4.41 * Since the version is not specified, the latest version at the time of installation

To give it a try

In the following flow, try starting / stopping the Web service using docker-compose.

    1. Environmental preparation
    1. Create a repository on Github
    1. Files prepared on the host side
  1. Operation confirmation using docker-compose

1. 1. Preparing the environment

** Create a VM instance on GCP **

** Package management tool update **

$ sudo apt update

** Install git **

python


$ sudo apt install -y git

** Install docker **

** Install docker-compose **

python


$ sudo apt install -y docker-compose 

Check if the installation is successful

python


$ ddocker-compose --version
docker-compose version 1.25.0, build unknown

2. 2. Create a repository on Github

Do the following on Github:

** ■ Create a repository for Public **

** ■ Create an "App" folder directly under the created repository **

** ■ Create index.html in the "App" folder **

** ■ Cut a development branch **

3. 3. Files prepared on the host side

** Prepare files with the following directory structure **

./docker-compose.yml ./Dockerfile ./site_config/   - demo_site.conf
./mount/   - main/ --Clone the main branch on Github

- dev/ --Clone the dev branch on Github

Directory creation


#################################################
#Directory for storing site config configuration files#
#################################################
$ sudo mkdir ./site_config


################################
#Create a directory for mounting#
################################
$ sudo mkdir ./mount

#Source code storage location
$ sudo mkdir ./mount/main
$ sudo mkdir ./mount/dev

Clone the source on Github


$ sudo git clone --depth 1 -b main https://github.com/Smiler5617/test_websys.git ./mount/main
$ sudo git clone --depth 1 -b dev https://github.com/Smiler5617/test_websys.git ./mount/dev

** Create each file as below **

demo_site.conf


<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/App/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Dockerfile


#Get base image
FROM ubuntu:20.04

#Installation of required packages
RUN apt update
RUN apt install -y tzdata
RUN apt install -y apache2

#Loading site settings
COPY ./site_config/demo_site.conf /etc/apache2/sites-available/
RUN a2dissite 000-default
RUN a2ensite demo_site

#Create a directory for mounting
RUN mkdir /var/www/html/App

#Port open
EXPOSE 80

CMD ["apachectl", "-D", "FOREGROUND"]

docker-compose.yml


version: '3.7'

services:
  web_main:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./mount/main/App:/var/www/html/App
    ports:
      - 8080:80

  web_dev:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./mount/dev/App:/var/www/html/App
    ports:
      - 8081:80

4. Operation confirmation using docker-compose

** Launch all services **

python


$ docker-compose up

If you can access the following environment, you can start it for the time being. http: // [VM external IP (global IP address)]: 8080 / http: // [VM external IP (global IP address)]: 8081 /

** Start / Stop ** Ctrl + C

** Run in background **

python


$ docker-compose start
Starting web_main ... done
Starting web_dev  ... done

Stop

python


$ docker-compose stop
Stopping [Google account name]_web_dev_1  ... 
Stopping [Google account name]_web_main_1 ... 

** You can also specify to start or stop the service **

python


$ docker-compose start web_main
Starting web_main ... done

$ docker-compose stop web_main
Stopping [Google account name]_web_main_1 ... 

** Service Suspension & Removal **

python


$ docker-compose down

Recommended Posts

Streamlining Web system development using docker-compose and Git
Team development using Git (eclipse edition)
Prepare Nuxt (web) + Laravel (API) development environment in the same repository using docker-compose
Django development environment construction using Docker-compose (personal memorandum)
Team development using Git (eclipse edition)
Switched property files for development and production using JMockit
[Java] Development with multiple files using package and import