Docker Compose Example
This is a step-by-step tutorial to understand the uses of Docker compose. In this tutorial, I will create two Docker containers using Docker compose. One docker container will have a MySQL database instance and another Docker container have an Apache webserver 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 web app is our web application directory. Also, create a index.html in the web app directory for testing.
Step 2 – Create Dockerfile for Webapp
Now create a Dockerfile in the web app directory to create a customized image for your application including the Apache webserver.
add following content
Step 3 – Create Docker Compose File
Finally, create a docker-compose configuration file (docker-compose.yml) file in the current directory. This will define all the containers that will be used in your current setup.
add the following content.
The above docker-compose file has settings for two containers. The first container is for the MySQL database server and the second is for the web server. The web container will run our application on the Apache server. As this is customized we have defined the build directory to the web app.
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 the web app directory.
read the below output of the above command. I have skipped some part of the output which is not required. The first line of the below output shows that it skipped building for the 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 the docker host is the 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 follows.
Now use the following commands to rebuild the web app container and relaunch using docker-compose.
Check the output of the command.
You can see that the mysql_db container is showing unchanged as nothing changed there. The only apache_web container has been recreated due to a new build found for the image used for that.
Again access your web application on port 8080 of the docker host machine. You will see the updated content here.
0 Comments
CAN FEEDBACK
Emoji