To put it simply: Docker Compose builds a stack of applications to run a complete service. The docker-compose.yml file is broken into sections, each section represents a single container which, when combined with the other containers, create the service. For example, you could have a docker-compose.yml file that consists of two sections:
web - the web server portion of the application.
db - the database server portion of the application.
Experience the Freedom of Backup as a Service
Simplify your data protection, ensure recovery, and defend against ransomware with a modern, hyperscale solution. Cohesity DataProtect, delivered as a service, gives you the freedom to focus on managing your data, not your infrastructure. Set up in min…
Downloads provided by Cohesity
Each section can be comprised of different components. And because you might need to get quite specific with the web section, you could instruct docker-compose that the web portion will be defined in the Dockerfile (we’ll hold that off for another how-to). In fact, with docker-compose, you can include multiple Dockerfiles to build very complex stacks. However, we’re going to build a rather simple stack to easily get you off the ground.
So, let’s create this docker-compose.yml file with the web and db sections. We’ll use NGINX and MySQL to create a stack that is easily used for development purposes.
The docker-compose.yml file
The first thing we must do is create a new file. We’ll create that file within a newly created directory. Issue the command mkdir ~/docker-build. Change into that newly created directory with the command cd ~/docker-build. Once inside that directory, create the new docker-compose.yml file with the command:
Remember, this is a YAML file, so the formatting must be consistent (otherwise the build will fail).
The first thing to do is add the header of the file. At the top of the file, add the following:
This instructs Docker Compose that we’re using version 3 of the tool. The next line will instruct Docker Compose that what follows will be the services to deploy. That is defined with:
So far, the file is:
version : ‘3’
Our first section to define will be the web portion of the stack. We can do this with two simple lines:
What the above does is instruct Docker Compose to deploy a container using the official nginx image. We’re not doing anything special for this container. It’s just basic.
Our next section will define the database. This one gets a bit more complicated, as we must configure the necessary parameters for the database to function. We begin by defining the section with;
Next, we instruct Docker Compose to use the mysql image for the database with:
Now we define both the external and internal ports to use for the database. For this we’ll make use of the default MySQL ports and define them like so:
Finally, we configure the database environment. The environment will be the configuration options for the database (passwords, users, database name). This section looks like:
password1 is the password for the MySQL admin user.
user is a new MySQL user.
password2 is the password for the new MySQL user.
database is the name of the database to be used.
Each of the above will be defined by you. Make sure to use strong, unique passwords, so to keep your stack safe.