Docker Compose Example

Docker Compose Example

 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.

$ vim  webapp/Dockerfile

add following content

FROM test/ubuntu-ssh:16.04

RUN apt-get update \
   && apt-get install -y apache2

COPY index.html /var/www/html/
WORKDIR /var/www/html
CMD ["apachectl", "-D", "FOREGROUND"]
EXPOSE 80

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.

$ vim  docker-compose.yml

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.

$ docker-compose build

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.

db uses an image, skipping
Building web
Step 1/6 : FROM test/ubuntu-ssh:16.04
16.04: Pulling from test/ubuntu-ssh
b3e1c725a85f: Pull complete
4daad8bdde31: Pull complete
63fe8c0068a8: Pull complete
4a70713c436f: Pull complete
bd842a2105a8: Pull complete
c41407f48fa7: Pull complete
1fcfeb9b5ef4: Pull complete
13195a7d2240: Pull complete
b86be64bbda8: Pull complete
8c951fe917dc: Pull complete
f74bc80103b6: Pull complete
Digest: sha256:523d6fbc97954e9f77231bf54bfcfbbdd4805349887477fbac4a63dc735d777d
Status: Downloaded newer image for tecadmin/ubuntu-ssh:16.04
 ---> bb63b492da01
Step 2/6 : RUN apt-get update    && apt-get install -y apache2
 ---> Running in 00be0dd717ce
[[[Removed long output from here]]]
 ---> 41c731590234
Removing intermediate container 00be0dd717ce
Step 3/6 : COPY index.html /var/www/html/
 ---> 42f84d4c2243
Removing intermediate container 945aaee6cbde
Step 4/6 : WORKDIR /var/www/html
 ---> 40bebd21e352
Removing intermediate container e13f5f412906
Step 5/6 : CMD apachectl -D FOREGROUND
 ---> Running in ab0db1ef1c6e
 ---> 587bf2323289
Removing intermediate container ab0db1ef1c6e
Step 6/6 : EXPOSE 80
 ---> Running in 7bcbef52d585
 ---> 8f03d4135394
Removing intermediate container 7bcbef52d585
Successfully built 8f03d4135394
Successfully tagged apache:latest

Step 5 – Launch Docker Containers

Finally, launch your containers using docker-compose up command. Use -d switch to run them in daemon mode.

$ docker-compose up -d

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.

$ echo "Welcome to Docker Compose Tutorial" >> webapp/index.html

Now use the following commands to rebuild the web app container and relaunch using docker-compose.

$ docker-compose build
$ docker-compose up -d

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.

http://dockerhost:8080/ 

Reactions

Post a Comment

0 Comments

close