Step 1: In this exercise, we will cover basic operations and capabilities of Docker-Compose. Are you ready to get started?
Step 2: Our sandbox environment already has Docker and Docker compose installed. And, you can install it on your machine by following the provided documentation link.
Step 3: Let's move on to creating our Docker application setup using Docker Compose. We will create setup for a Flask app. Clone the repository by running `git clone https://github.com/web3coda/docker-basics` in your terminal.
Step 4: The link to the repository is also provided below. You can use it outside of sandbox environment as well.
Step 5: Open VS Code by clicking on the button. We will use VS Code to check the Docker Compose, Dockerfile and Flask app files.
Step 6: Check 'dockerfile-compose-tutorial' folder inside the cloned 'docker-basics' repository. This is the folder we will use for this exercise.
Step 7: Open Dockerfile by clicking on the file name in the left sidebar. This is the Dockerfile we will use for our Flask app. It is well documented so you can understand what each line does. Also, check app.py , requirements.txt and docker-compose.yml files. These files are used to define app, to list packages or dependencies and to define services for our Flask app.
Step 8: In the sandbox environment, you can use either 'docker compose' or 'docker-compose' command as per your wish.
Step 9: Important Note: docker compose commands only work when there is a docker-compose.yaml file present in the current working directoy. So, ensure you are running all commands from 'dockerfile-compose-tutorial' directory. Also, if the Docker compose file is not named docker compose then in that case, we have to specify the file with '-f file-name'.
Step 10: With everything set up, it's time to build and run our Docker-Compose file. Run 'docker-compose up' command to run the docker-compose.yaml file. It will first build the image and then create the network with redis. The complete setup will container one network and two containers (app container and redis).
Step 11: Check the page hosted by this webapp by clicking 'localhost:8000' button. By clicking the 'Go' button, you will see the count increasing.
Step 12: Switch back to 'terminal' tab and stop the running command with 'Ctrl + C'. Also, run 'docker compose down' to ensure every component of the setup is removed.
Step 13: In similar way, we can run Docker-Compose services in detached mode by appending '-d' argument to it.
Step 14: Check running docker conatiners. You will observe 2 containers, one for flask app and another one for redis.
Step 15: Check docker networks. You will be able to see 'docker-compose-tutorial_default' bridge created to connect these two containers.
Step 16: List the running docker-compose projects. As we are only running one project, you will only see that.
Step 17: List the running docker containers that are created by Docker Compose. Unlike 'docker ps' it won't show all containers but only the ones created with Docker Compose.
Step 18: In detached mode, Docker compose logs are not shown on terminal by default but we can check those with 'docker compose logs' command.
Step 19: Just like tail command, we can continuously follow the logs using '-f' argument with this command. And, stop it by using 'Ctrl + C'
Step 20: We can also view the logs for a specific service by running 'docker-compose logs [service-name]'. For example, in our case, we can check for 'web'service
Step 21: Stop the Docker-Compose services running in detached mode.
Step 22: If you want to start a specific service in detached mode. You can find the service name in the 'docker-compose.yml' file or use 'docker-compose ps' to view the service names.
Step 23: Like before, stop the Docker-Compose services running in detached mode.
Step 24: We can also mount files/directory using docker-compose. This allows us to persist the data across multiple container runs and change the application code without rebuilding the Docker image.
Step 24: Edit the 'docker-compose.yaml' file to add a bind mount using VS Code. Copy the code provided below and paste it in the docker-compose.yaml file
Step 25: Run Docker-Compose to apply the change
Step 26: Switch to VS Code tab and edit the 'app.py' file by replacing `Hello World` with `Happy Dockering`.
Step 27: Switch to Localhost tab and click 'Go', you should see the change.
Step 28: Docker compose also allow us to scale up and down the services as per need. In simple words, Scaling is the process of increasing or decreasing the number of containers running for a service assuming more containers can help in serving more clients. Lets scale our 'web' services to run 2 containers instead of 1.
Step 29: Verify the number of containers by running the container listing.
Step 30: Switch to localhost tab and press 'Go' button a few times, you should observe different hostname and counts. This is because now there are two containers serving our requests.
Step 31: Let's scale the service down.
Step 32: Great! You've successfully scaled your application. Now, stop the Docker-Compose services.
Step 33: Congratulations! You've successfully completed the lab and learned basics of Docker Compose.