How to Deploy a Container with Docker Compose
Docker is a fantastic tool. Docker is like using a mini virtual machine without the fuss and overhead of a hypervisor or VMs. It's not uncommon nowadays for businesses or tech enthusiasts to run their applications with a Docker container. That's all well and good, but what do you do if you need to run multiple Docker containers together?
That's when you would use Docker Compose. So, you might be wondering, "How do I deploy a container with Docker Compose?" Well, you are in luck because that is precisely what we are going to discuss today!
What is Docker Compose?
Docker Compose is a tool to launch multiple Docker containers together that depend on each other. For instance, if you need to host a LAMP server (Linux, Apache, MySQL, PHP) with WordPress, you could create four different Docker containers that work together to host your website. This contrasts with using one monolithic Docker Container. Let's explain the benefits of this.
Docker is a great tool to deploy and launch applications between various environments. Think of Docker like a mini VM. Unlike a VM, though, a Docker container uses the host computer's kernel, memory scheduler, and other resources. Those resources are shared with other Docker containers and the host computer.
In contrast, a hypervisor will slice up a computer's resources and dedicate those resources to various VMs. Those dedicated resources traditionally are not shared between the host operating system and the virtualized computer. A virtual machine also contains virtualized pieces of all the hardware and software on your computer including a full OS install.
A docker container is an application blob that contains all the dependencies for that application. That application container can be 'installed' on different computers without configuring it repeatedly. Containers also use the host OS and do not contain their own.
This is amazing for single-use applications like FFMPEG, MongoDB, or Code Server. Let's focus on the Code Server example for a moment. Code Server is a web-based version of Microsoft's Visual Studio Code. Instead of launching VS Code on your computer, you would use Code Server from a browser window.
This can be handy for a team of developers. Most software development teams have specifications and criteria they keep for their development environments and codebase. Configuring their development environments from scratch to meet those criteria can be a pain in the butt. With Code Server, when a software dev team hires a new programmer, all they must do is clone the Code Server environment, update their Git credentials, and be off to the races. That process can be made even faster by containerizing Code-Server within a Docker container.
What happens if you want to containerize both Code-Server and MongoDB together? MongoDB isn't updated often. On the other hand, Microsoft updates Visual Studio Code regularly. If both MongoDB and Code Server were containerized within the same container, you would need to rebuild the entire container each time Microsoft updated Visual Studio Code (along with Mongo DB). That seems like a lot of work. Because of the extra complexity, that also increases the chances of mistakes building that container.
On the other hand, you could keep MongoDB and Code Server in their own containers. That way, when Microsoft does update VS Code, you only need to rebuild the Code Server container. Docker Compose can link those two containers together and start and stop them as if they were a single container.
How do I Deploy a Container with Docker Compose?
Docker Compose can be used in a ton of different ways. Because of its versatility, we won't cover all the edge cases for Compose. Instead, we will cover how to launch multiple pre-built containers together. The containers discussed below can be pulled through Docker Hub as if you were launching them individually.
Let's use our Code Server example again for this exercise. In case you skipped directly to this section, Code Server is a self-hosted web-based version of Microsoft's Visual Studio Code. In this case, we will also need MySQL too. This specific example may be helpful for development teams, so let's get started.
Docker Compose depends on a file called docker-compose.yml. Think of those YAML files as a recipe for Docker. It tells Docker what images to use and what special configurations to launch those images with. That config file also tells Docker which containers depend on other containers.
So, let's look at our docker-compose.yml example below:
version: ‘1’ services: db: image: mysql volumes: – db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: myPassword MYSQL_DATABASE: myAwesomeDB MYSQL_USER: myUserName MYSQL_PASSWORD: myOtherPassword codeServer: depends_on: – db image: linuxserver/code-server ports: – “8000:80” restart: always environment: CODESERVER_DB_HOST: db:3306 CODESERVER _DB_USER: myUserName CODESERVER _DB_PASSWORD: myOtherPassword
'Version' just tells us how many times this YAML file has been configured and updated. In our case, this is our first try. It would be wise to update the version number each time the YAML file needs to be changed.
Next, we need to define our services. In this block, we will define both MySQL and Code-Server. First, we named MySQL 'db.' It doesn't matter what you call the MySQL service. Just make sure you know what its name is.
Next, we define parameters for MySQL. In this case, we are telling Docker where to store the database. We are also configuring our user information for MySQL as well.
Next, we define our Code Server service. For this block, pay attention to the 'depends-on' parameter. This line is telling Docker that Code-Server depends on MySQL (or 'db'). Therefore, Code Server can't launch unless MySQL launches appropriately. The rest of the Code Server block defines standard parameters like which port to access Code Server with and Code Server's MySQL user information.
This docker-compose.yml file must be created before Docker Compose can be used. We mentioned above that we will be using containers that can be readily pulled from Docker Hub for this example. In the YAML file, we defined the container image we wish to use with the 'image' parameter.
If you build a new Docker container, replace the 'image' parameter with 'build.' Likewise, the docker-compose.yml file must live in the project’s root directory for that Docker container. If you are not building a new Docker container and instead are pulling containers from Docker Hub, the docker-compose.yml file can be located anywhere in your file system that Docker has permission to access.
Finally, when you are ready to use Docker Compose, launch your containers with the command below in the folder where your configuration file is stored:
You can manually define your YAML file with the command below as well:
Docker-compose –file .\ docker-compose.yml
An Overview of How to Deploy a Container With Docker Compose [Video]
In this video, Trevor Sullivan covers how to build and deploy a custom built container image with Docker Compose. In this step by step demonstration, you'll see the various settings you need to configure, where to go to get the container instructions, and how to customize a publicly accessible image for your specific requirements.
Docker Compose is a simple tool to launch multiple Docker containers together that depend on each other. An excellent example of using Docker Compose would be running a LAMP stack to host a WordPress website. By keeping each application in its own containers, individual containers can be rebuilt as updates to those applications are released. Likewise, those containers can be reused for other application stacks as well.
Docker Compose depends on a file called docker-compose.yml. In that file, each Docker Container, as well as its parameters, are defined. In addition, that file also states which containers depend on other containers. Using Docker Compose is as simple as opening a command-line environment, navigating to the folder where the docker-compose.yml is located, and using the command 'docker-compose up.'
Docker can be a handy tool in IT. If you would like to learn more about different ways to use Docker, consider our Docker training course.
delivered to your inbox.