How Do You Use docker-compose?

Problem scenario
You want to create an example of a docker compose file.  How do you use docker-compose?

Prerequisites
i.  This assumes you have Docker installed and running.  If you need assistance, see this posting.
ii.  This assumes that you have installed docker-compose.  If you need assistance, see this posting.
iii.  This assumes the Docker host has access to the internet.
iv.  Port 8080 is not being used (e.g., any containers that use this port are turned off).  If you need to keep port 8080 open for another purpose, change the "8080" in the directions below.

Procedures
1.  Run these three commands:
mkdir dockercompose && cd dockercompose
mkdir webapp
echo "<h2>It worked\!</h2>" > webapp/index.html

2.a.  Create a Dockerfile in the webapp directory. (You may want to run vi webapp/Dockerfile.)
2.b.   This is the content for the Dockerfile:

FROM tecadmin/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

3.a.  Create a docker-compose.yml file.  (You may want to run vi docker-compose.yml.)
3.b.  This is the content of the docker-compose.yml file:

version: '3'
services:
  db:
     image: mysql
     container_name: mysql_db
     restart: always
     environment:
        - MYSQL_ROOT_PASSWORD="secret"
  web:
    image: apache
    build: ./webapp
    depends_on:
       - db
    container_name: apache_web
    restart: always
    ports:
      - "8080:80"

4.  Run these commands:  

echo "Welcome to Docker Compose Tutorial" >> webapp/index.html
docker-compose build
docker-compose up -d

5.  Open a web browser.  Go to x.x.x.x:8080 where x.x.x.x is the external IP address of the server (or the result of "curl icanhazip.com on the back-end).

6.  You should see these:

"It Worked\!
Welcome to Docker Compose Tutorial "

You are now done.  (Remember that modifications to the index.html command will require you to use "docker-compose build" once again.  New containers may conflict on port 8080 if you do not change the docker-compose.yml file or stop the containers from running.)

The above directions were adapted (but mostly copied) from Tecadmin.net.

Leave a comment

Your email address will not be published. Required fields are marked *