Check out this Docker Compose tutorial for beginners
Docker Compose is a tool to initiate and configure multiple containers at the same time. Intended for local testing environments and proof-of-concept projects, Docker Compose can also run services in production, but Kubernetes is the more popular tool for that task.
In this Docker Compose tutorial video, we create an Azure Voting App -- a prebuilt and open source application meant for learning purposes, with basic functionality for users to vote between two options. The app uses Redis -- an open source data structures server, which can operate as a key-value store -- as its back end. We will verify the app's success via web browser, and finally, delete the entire project.
The Docker Compose binary, docker-compose, is typically installed alongside some versions of Docker, such as the Docker community edition or Docker desktop versions. If your Docker download doesn't include Docker Compose, download the binary from GitHub and follow these installation instructions.
In Docker Compose, each container is called a service. IT admins can define configurations for multiple services in a single YAML file, docker-compose.yaml.
In the Docker Compose tutorial video above, the Azure Voting App uses the services in the docker-compose.yaml file to set up a Flask application front end that connects to a Redis back end. The Dockerfile located in this repository builds the front end and pulls a standard Redis container from docker-hub.
To start a docker-compose application, navigate to the folder that contains the docker-compose.yaml file and run the docker-compose up -d command. This command starts all services defined in the docker-compose.yaml file -- which you can confirm with the command docker ps after the docker-compose command completes to check any containers running at time of test. Docker Compose uses an equivalent command, docker-compose ps, but the output is limited to the containers defined in the docker-compose.yaml file.
Once you have finished with the application, you can stop it with the docker-compose down command from the same directory. Run the docker-compose logs command while the application is up -- this command returns logs from the currently running Docker Compose application.
For more information on any specific Docker Compose command, run docker-compose help <command>. For example, docker-compose help up will return the options and usage for the docker-compose up command.
Welcome to 'How to get started with Docker Compose.'
So, to start out here, I'm on an Ubuntu Linux machine, and I need to have Docker installed already. So let's go ahead and run a docker version to make sure that I have it installed here.
Great, that's confirmed. Now, next I want to install the Docker Compose binary -- and the instructions on how to do this are available at Docs.Docker.com/compose/install, so I'm not going to go over them too much here. But what I'm doing is downloading the binary from GitHub, and then making it runnable. So to check that that installed correctly, let's run a docker-compose --version. You'll notice that docker and compose has a hyphen in between there; that can get pretty confusing -- especially if you've worked with Docker a lot. So this might take some getting used to, but it is worth learning about.
So next, I'm in a directory here already that I've set up for this demo. And that's got a few files in here, including a Dockerfile under azure-vote, and the Docker Compose file at the root of the directory. And I'll make sure you all have a link to this as well. It's open source -- it's on GitHub. But let's go ahead and take a look at those files.
If I take a look at the Dockerfile, for example, I see it's really using a Python container, installing Redis and then copying over main.py and the config files under the azure-vote directory. Now if I take another step back and look at the Docker Compose file, this one's a little bit more interesting.
So Docker Compose: Every Docker Compose file is going to be a YAML that just defines how many Docker containers you want to spin up and what their configuration is. And each of these is going to be called a service. So at the top, on line one, you'll see version three, which is the version of the Docker Compose YAML. And each of these is associated to a different Docker engine -- so try and always use the latest if you can. And then I have two services, the azure-vote-back and azure-vote-front. This is just the front end and the back end for an application. So you see from my azure-vote-back, I'm using a Redis container. And that is -- if I just run a docker run redis, that's going to pull that container, and then I'm giving it a name and exposing port. So the Redis port is 6379. So if I run a docker run command with Redis, it's just going to do the same thing, but only for the one container.
For my azure-vote-front, since I have the build flag there, it's actually going to trigger a build in the azure-vote directory, which -- if I take a look at the directory I'm in -- it's going to build that Dockerfile there, which we covered already. So then I'm giving that azure-vote-front service its own name, container name and port that I want to expose. But I'm also giving it an environment variable, which is the redis variable, and the value I'm giving that is azure-vote-back. So, I'm basically telling that front end to look for the service I've set up under the container named azure-vote-back for the back end for the service.
So now that we've gone over what the Docker Compose file looks like, we can go ahead and start it by running a docker-compose up -d command. And you see it's run both of these now. It's created both of these services and spun them up; they should be running if we have a docker ps. We see that both of these services are running. Let's go ahead and go over to a web browser and navigate to it.
So I'm here in Firefox, and if you recall, the port in the Docker Compose file that was going to be exposed on the host was port 8000. So if I go to localhost:8000, this is the page I'm greeted with: It's the Azure Voting App with the Redis back end. I can click around and each of these gets a vote or gets reset. And that's all defined in main.py.
Let's go back to the terminal and we can shut this app down. So we're back here in the terminal, my containers are still running. And let's clear the screen to make it a little bit easier to read. Now if I want to shut down both services that I'm using, I'm going to run a docker-compose down command. And all of my services have been stopped and removed. Let's run docker ps one more time just to make sure.
Great -- so my containers have been stopped. If I go back to my web browser, my application's not going to load this time around. And this is a great way to get quick proof of concepts out the door -- when you need multiple containers to run a service. It can also be great for troubleshooting or being able to spin up a stack locally for testing or development.
That's it for this video. Thank you for watching.