How To Create A Multi Container Application Using Docker Compose
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Dockerfile.
https://cloudaffaire.com/how-to-create-a-docker-image-using-dockerfile/
In this blog post, we will discuss how to create a multi-container application using docker compose. We will also create a multi-container application using docker-compose.
Prerequisite for this demo:
- One AWS EC2 instance with Linux 2 AMI and internet access.
- Docker
What is Docker Compose?
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. Docker-compose operates in below three steps
- Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
- Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
- Run docker-compose up and Compose starts and runs your entire app.
To get more details on Docker Compose, please refer below documentation
https://docs.docker.com/compose/overview/
Next, we are going to create a multi-container application using docker-compose. The application will use the apache web server and phpMyAdmin as frontend and MySQL database as backend running in three different containers and managed by Docker Compose.
How To Create A Multi Container Application Using Docker Compose Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
##----------------------- ## Docker: Docker Compose ##----------------------- ## docker-compose [COMMAND] ################################### ## Docker compose installation ## ################################### ## Run this command to download the current stable release of Docker Compose: ## please refer https://docs.docker.com/compose/install/ for additional details sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose ## Apply executable permissions to the binary: sudo chmod +x /usr/local/bin/docker-compose ## Test the installation. docker-compose --version ########################### ## Directory structure ## ########################### # myPROJECT/ # ├── apache # │ └── index.php # ├── docker-compose.yml # ├── Dockerfile # └── mysql # └── data.sql ## Create directories mkdir myPROJECT && cd myPROJECT mkdir mysql apache ######################################## ## Create a docker-compose.yml file ## ######################################## vi docker-compose.yml ---------------- version: '3' services: web: build: . image: cloudaffaire/myapache:latest container_name: apache_container links: - "db:db" ports: - "8002:80" volumes: - ./apache:/var/www/html/ networks: - default db: image: mysql:5.7.13 command: --default-authentication-plugin=mysql_native_password container_name: mysql_container environment: - MYSQL_DATABASE=mysqldb - MYSQL_USER=admin - MYSQL_PASSWORD=admin - MYSQL_ROOT_PASSWORD=admin ports: - "8000:3306" volumes: - ./mysql:/docker-entrypoint-initdb.d phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin_container environment: PMA_ARBITRARY: 1 MYSQL_USER: admin MYSQL_PASSWORD: admin MYSQL_ROOT_PASSWORD: admin ports: - "8001:80" links: - "db:db" --------------- :wq ########################### ## Create a Dockerfile ## ########################### vi Dockerfile --------------- FROM php:7.0.30-apache RUN docker-php-ext-install mysqli --------------- :wq ########################################## ## Create other files used by the app ## ########################################## ## Create index.php file under apache directory vi apache/index.php --------------- Hello!!.. Welcome to docker compose demo"; ?> $conn = mysqli_connect('db', 'admin', 'admin', "mysqldb"); $query = 'SELECT * From myproduct'; $result = mysqli_query($conn, $query); echo '
$result->close(); mysqli_close($conn); ?> -------------- :wq ## Create data.sql file under mysql directory vi mysql/data.sql -------------- SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; USE mysqldb; CREATE TABLE `myproduct` ( `id` int(10) NOT NULL, `name` varchar(20) NOT NULL, `price` int(10) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `myproduct` (`id`, `name`, `price`) VALUES (1, 'apple', 120), (2, 'banana', 80), (3, 'mango', 180), (4, 'grape', 150); -------------- :wq ################################# ## Docker Compose Management ## ################################# ## Validate and view the Compose file. docker-compose config -q docker-compose config --services ## Build the service docker-compose build ## Start the service docker-compose up -d ## Test the application ## Replace your ec2 public DNS or localhost if you are using your own system ec2-13-234-163-168.ap-south-1.compute.amazonaws.com:8002 #test web, db ec2-13-234-163-168.ap-south-1.compute.amazonaws.com:8001 #test phpmyadmin |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
## List images used by the created containers docker-compose images ## Print the public port for a port binding. docker-compose port web 80 docker-compose port db 3306 docker-compose port phpmyadmin 80 ## List service containers docker-compose ps ## Receive real time events from containers docker-compose events ## Display the running processes docker-compose top web ## View output from containers docker-compose logs web ## Pause services docker-compose pause web ## Unpause services docker-compose unpause web ## Execute a command in a running container docker-compose exec web bash exit ## Run a one-off command docker-compose exec db bash ## Stop services docker-compose stop web ## Start services docker-compose start web ## Restart services docker-compose restart web ## Remove the service docker-compose down ## Generate a Docker bundle from the Compose file docker-compose bundle --push-images ## Push services (make sure you are logged into docker hub) docker-compose up -d docker-compose push web ## Pull service images docker-compose pull web ## Kill containers docker-compose kill web docker-compose kill db docker-compose kill phpmyadmin ## Remove stopped containers docker-compose rm web docker-compose rm db docker-compose rm phpmyadmin |
Hope you have enjoyed this article. In the next blog post, we will discuss docker swarm.
To get more details on docker, please refer below docker documentation
please leave the files ro repository please