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?

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.

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"]

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'
     image: mysql
     container_name: mysql_db
     restart: always
        - MYSQL_ROOT_PASSWORD="secret"
    image: apache
    build: ./webapp
       - db
    container_name: apache_web
    restart: always
      - "8080:80"

4.  Run these commands:  

echo "You just used Docker Compose via" >> 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 on the back-end).

6.  You should see these:

"It Worked\!
You just used Docker Compose via"

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 The directions above were tested in February of 2022.

Leave a comment

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