This is the step by step tutorial to understand uses of Docker compose. In this tutorial, I will create two Docker containers using Docker compose. One docker container will have MySQL database instance and another Docker container have Apache web server with our dummy application file.
Let’s follow step by step tutorial and watch the things happening there.
Step 1 – Create Directory Structure
First of all, create a directory structure. Here webapp is our web application directory. Also, create a index.html in webapp directory for testing.
By default, the docker-compose command will look for a file named.env in the project directory (parent folder of your Compose file). By passing the file as an argument, you can store it anywhere and name it appropriately, for example.env.ci.env.dev.env.prod. Passing the file path is done using the -env-file option. Both the PHP and Apache containers have access to a “volume” that we define in the docker-compose.yml file which maps the publichtml folder of our repository to the respective services for them to access. When we do this, we map a folder on the host filesystem (outside of the container context) to inside of the running containers. Docker Compose - Hashicorp's Vault and Consul Part B (EaaS, dynamic secrets, leases, and revocation) Docker Compose - Hashicorp's Vault and Consul Part C (Consul) Docker Compose with two containers - Flask REST API service container and an Apache server container Docker compose: Nginx reverse proxy with multiple containers. A typical minimal configuration consists of an Apache http Server and multiple Tomcat instances hosting different applications. After I had played around with Docker images and containers I digged into docker-compose to define and link multiple docker containers. Build your docker images. Command to install Docker Engine: How To Start Apache Web Server i.
Step 2 – Create Dockerfile for Webapp
Now create a Dockerfile in webapp directory to create a customized image for your application including Apache web server.
add following content
Step 3 – Create Docker Compose File
Finally create a docker compose configuration file (docker-compose.yml) file in current directory. This will define all the containers will be used in your current setup.
add following content.
Above docker compose file has settings for two containers. The first container is for mysql database server and the second is for web server. The web container will run our application on Apache server. As this is customized we have defined build directory to webapp.
Step 4 – Build Webapp Image
Now, build an image using the following command. This will create an image named apache using Dockerfile and contents from webapp directory.
read the below output of above command. I have skipped some part of output which is not required. The first line of below output shows that it skipped building for db container due to no build defined. For web container it uses webapp/Dockerfile to build an image.
Step 5 – Launch Docker Containers
Finally launch your containers using docker-compose up command. Use -d switch to run them in daemon mode.
You can access your web application running on the apache_web container by accessing your docker host on port 8080. For example, http://dockerhost:8080/ where dockerhost is IP or hostname of your Docker host machine.
Step 6 – Update Content in Web Application
Let’s make a change in your web application. I have added some more content to webapp/index.html file as following.
Now use the following commands to rebuild webapp container and relaunch using docker-compose.
Check the output of the command.
You can see that mysql_db container is showing unchanged as nothing changed there. The only apache_web container has been recreated due to new build found for the image used for that.
Again access your web application on port 8080 of docker host machine. You will see the updated content here.
Jquery markdown. With the Docker Compose utility we can create our own environment for web development.
We will be using two Dockerfiles created on previous posts:
For the web server: Apache and PHP
Docker Container: Centos 7, Apache, and PHP 7.3
For the database server: MySQL
Docker Container: MySQL 8
First, we create a file named docker-compose.yml.
On that file we will define two services 'web' and 'db'
- The 'web' service will use the Dockerfile for our Apache/PHP container.
- The 'db' service will use the Dockerfile for our MySQL container.
- For the root user we are using the password 'root'. You may want to use a more secure password.
- We also use the port 3306 to access the MySQL server from the host machine. If you already have another instance of MySQL running, you may need to use a different port. (i.e. '4001:3306')
Dockerfile for Web Server
This is the Dockerfile for the web server.
This is the Dockerfile for the database server.
You can download the above files, as well as additional files as a zip file here.
Uncompress the zipped file, and you will have the following folder structure and files:
Now we create the containers.
Open a terminal window, navigate to the folder with the docker-compose.yml file, and type the following command.
You will see an output similar to this one (excerpts):
After the containers have finished building, we can check the logs with the following command:
Test the Containers
To test that our web and database servers are working correctly there is included a test.php file that will try to make a connection to the database.
- As 'host' variable we can use the name of our 'db' service. We don't need to specify its IP address.
- If you have specified a different password for the root user on the docker-compose.yml file. You would have to change it on this file as well.
Open your web browser and go to the http://localhost:4000/test.php url.
You should see the following text:
If the connection couldn't be made, an error message, indicating the reason, will be displayed instead.
After you have finished working with your containers you can stop them:
To re-start them, you would execute the docker-compose up -d specified above.
Remove the Containers
If you don't need the containers any more you can remove them:
Docker Compose Tutorialapache-php7-mysql8.zip
Docker Compose Command Line
Check the docker-compose.yml and Dockerfiles on Github here.