‫We previously talked about the Docker Compose YAML file and in this lecture, we're going to be talking
‫about the compose command line. Requirements for this lecture are like the rest for this section where
‫you really need to know images and containers, and how to use them and the concepts around them from
‫previous sections, so that you can use all of those inside the compose command and the YAML file. The
‫Docker Compose command line tool is actually separate from the Docker tool.
‫It's actually a separate binary. If you're on Docker for Windows or Docker for Mac,
‫it actually comes bundled with that.
‫If you also use the toolbox on Windows 7, it bundles with that.
‫But if you're on Linux, you have to download it separately and you can get that at GitHub.com/
‫docker/compose, but you can also just search for it on Google, and there'll be a link in the references
‫for this section.
‫But I should state really quick that it's not designed to be a production-grade tool.
‫It's super ideal for local development and testing things really quickly that might otherwise be complex
‫to type in a bunch of commands from the command line. Two common commands that we use will be docker compose
‫up, which is by far probably what you're going to be typing 90% of the time.
‫It's a one stop shop.
‫It's basically a single command to do everything in the compose file, including creating volumes, creating
‫networks, starting all the containers with all their configuration options. Then to clean up when you're
‫done,
‫the docker compose down, which a lot of people actually forget to do, will actually clean up after
‫you. It'll get rid of the containers, remove the networks and volumes that aren't needed anymore.
‫It allows you, with these two commands, to quickly jump in and out of different environments for development.
‫This is where I really get the attention of developers that are used to using vagrant up and other
‫tools to customize complicated environments locally they need for development.
‫Once I show them Docker Compose and the compose file and how easy it is to use along with a Dockerfile,
‫it really starts to click for them that this could be a better way and a more reliable way to set
‫up their environments.
‫And it doesn't require the time to set up different VMs
‫or the complexity of managing a virtual machine environment.
‫Just to give you a little taste, if you had a project where you had the typical new developer onboarding
‫process that was all about learning how to get the right tools for their system, and the right virtual
‫machines downloaded, and the right add-ons, and all the things that normally they would just have to build
‫a replicated environment for, with Docker Compose,
‫it's as easy as cloning the environment that has your compose file from some repo somewhere and then
‫running the docker compose up command.
‫It can really be that easy.
‫Let's jump in and play around. I'm in the compose sample 2 directory. The compose sample 1 directory
‫was simply some visual examples that we showed in the last lecture.
‫But in this one, we'll be able to actually run this one and get a quick output.
‫I'm just going to do a docker compose up.
‫Well, actually before we do that, let me just show you what it is.
‫This has got two services, or two containers, and the first one is an Nginx server which is configured
‫as a proxy in this case.
‫And that's going to be listening on my port 80 on my local machine.
‫You'll notice there's a volume command there where I've actually done a bind mount to a file in this
‫directory called an nginx config file, and it's really a very simple little file.
‫It's not necessarily important that we know how that file works yet. It just shows how, in this case, I'm
‫actually taking a file on my local environment. Let's say that this directory is a Git repository that
‫stores these config files for me. It allows me to map that file in to the container and I actually, at
‫the end, you'll see it say, 'ro,' that means read only, meaning I can't change it in the container.
‫That's not necessarily required here. I just wanted to show you that that's how you would do that
‫in this case. That config file is actually telling Nginx,
‫'instead of being a web server,
‫I would like you to be a reverse proxy, and sit in front of another server,' which in this case we're calling
‫our web server, with its service name, and it's running the Apache 2 server, which is the image httpd from
‫the official image repository on Docker Hub.
‫You can also see, at the top, I've specified a version here. That's how I would do that.
‫Inside the image line.
‫So, what should happen is when I type this docker compose up, it should start up both containers. It
‫should create a private network for the two of them.
‫It will automatically bind mount that file.
‫It'll open up the port, and it will start dumping logs out to my screen.
‫You'll see how there, it said, 'creating these two containers.'
‫It's actually creating the network.
‫You'll notice in my file, I didn't specify a networks area, or a volumes area because they're not actually
‫required.
‫They're only necessary if I need to do some custom things in the network, like maybe change the default
‫IP addresses or change the network driver that's used.
‫Or in the volumes case, I can create named volumes, or use a different volume driver, and we'll get into
‫all of that later in advanced sections.
‫Just so you know, that's how simple these files can be.
‫You don't normally need all that stuff.
‫Those features are there if you need them, but usually these very simple, little commands like this will
‫get you going.
‫What have I done? This is now my Apache server and my Nginx server.
‫You'll notice the logs are actually colored here.
‫That's a neat feature of Compose that you don't necessarily get out-of-the-box with Docker
‫is that it will log all the containers that it's running on the screen in the foreground.
‫I can always do the same thing as I do with Docker and run docker compose up -d, which will run
‫it in the background. But it's nice to see when I'm developing or testing things locally, that I've got
‫my logs right on the screen.
‫If I jump over to my browser, and then go to localhost, you'll see that it says, 'It works.'
‫That's actually the Apache server replying, not the Nginx server.
‫The traffic is actually going through the Nginx reverse proxy.
‫It's repeating the traffic over to the Apache server. The Apache server is responding with its default
‫basic HTML file because we didn't change anything.
‫Then the Nginx is repeating it back to me.
‫So, it's your basic web server, web proxy configuration.
‫You can tell that the traffic hit both of them. They actually work through the proxy because
‫the proxy is actually a different color
‫here. The logs are pumping out here with a random color for each container in my setup so that I can
‫easily see what's going on.
‫If I hit refresh several times, you'd see the traffic showing up. Then it's hitting first the proxy and
‫then the backend web server.
‫If I wanted to stop this here, I'd hit Control c to stop the compose in my screen.
‫I could actually run it again with the -d to run it in the background.
‫You can see here that I can refresh it and it's still there. I can do a docker compose logs to see
‫the same output of logs that I saw a while ago.
‫A lot of the commands that you might be used to in Docker are also in Docker Compose. Really what
‫Compose is just doing is it's talking to the Docker API in the background
‫on behalf of the Docker CLI. It's kind of like a replacement for the Docker CLI still talking
‫to the Docker server API in the backend.
‫Of course, the great help here, docker compose help, shows me all the commands I can run.
‫You'll see a lot of these look really familiar because they're the equivalent of the docker command
‫just using the context of the configuration of the compose file.
‫I want to clean all this up because they're still running. I can do a docker compose ps here, which
‫will show me that both of my containers are running.
‫I can do a docker compose top to actually list all of the services running inside of them in a nice
‫formatted output.
‫That's pretty cool.
‫Then finally, I can do a docker compose down to stop and clean up my stuff that I just started.
‫You'll see that it's telling me what it's done there. Cleaned it all up.
‫So, great.
‫This is just a little taste.
‫We're going to be using Docker Compose a lot throughout the rest of this course for the local development
‫and testing stuff. Then we'll keep using the Docker Compose files as we transition into the production
‫concerns for how we deploy complex environments with a simple YAML file.