How To Use Docker Compose To Build And Run Linux Containers
Think you can do better? Show us!
Docker Compose is a tool that defines multi-service applications using YAML files. In this video, Francisco will walk through how to create a YAML file to configure a WordPress application using Linux containers. The YAML file will define 2 services, WordPress and a MySQL database. Once the services are defined in the YAML file, we will use “docker-compose build“ to build the services and check for any errors. Once the build completes successfully, we will us “docker-compose up“ to start the containers for our services. We will also go over how to run the containers in the background using the same command. Finally, we will go over how to stop and remove containers using “docker-compose down“ and go over how to remove everything using “docker-compose down –volumes”.
there in this video. We're going to go over how to use Docker compose an Linux to build a multi service docker application. Our project will be based on deploying WordPress to get started. I'd like to showcase the version of Docker and Linux distribution were using for this video. To do that, we're going to type in Sudo. Docker info and as you can see our Docker version of 18061 community edition and our Linux distribution is fedora, 28 with composed you use a yaml file to define the services that make up an application? We have a directory called WordPress, where we have our compose file. Normally you would need to execute a Docker compose command from within the directory in which the Yaml file is stored. Today, you can provide Docker composed a path of your yaml like so. So here we're using the flag dash F. Then presenting the directory in the name of the file something else to note is the Yaml file by default requires a name to be said to Docker compose Yaml. This is where the flag dash F comes in handy as it also allows us to provide our own filename as opposed to using the default name. But before running anything and compose. Let's head over to our code editor and dissect? What's inside the yaml file inside the yaml How we begin with a few crucial keys. The version key is mandatory and it's always the first line at the root of the file. This defines the version of the compose file format. You can consult the compatibility matrix on which one to use but normally you would use the latest version. The next key is services. And it defines the 2 services of our WordPress application, a database an WordPress. Beneath the service key are multiple parameters, which define additional policies and settings for our containers. For the first parameter you must define a name for the service or container. I decided to call her purse container DB or next parameter in our first container. We define the image. This container will be built on top of. You can define a custom image build inside the Yaml file. But for this video will be using an image from the Docker hub in this case will be using mySQL version 5. Not 7 next. We defined the volume for persistent data, which will be generated and used by the containers following that we define a restart policy. If the Docker Daemon restarts, so will the container last we define a few mySQL variables, such as. The root password the database name. The username and password for the database for next container. I label date WordPress on a next parameter. We define the dependencies between the 2 services in this case, the WordPress container will depend on the container above label DB. We also define the image to be the image in Docker hub for WordPress and the latest version. Next we define the network ports to be exposed from the host our WordPress Web Interface listens on port 80, so will be exposing that port to the physical network on port 8000. We also set the same. Restart policy to this container and last we set the variables for WordPress such as the internal port. The container will use to communicate with the mySQL and the username and password to the database for the final key volumes. We're defining a persistent Docker volume. To store any updates generated by WordPress to the database OK, moving on now. Let's run. Docker compose build to verify our application is defined accordingly and to build out any custom images as you can see, there are no errors in no custom images to build since we have defined Docker images. Instead, for the purposes of this video. I've already downloaded the image is necessary for our application. Now let's run Docker compose up to create and start her services. What happens here is while the containers are being deployed my console session begins to receive verbose logging output? Of each container once the process is complete. I need to use control C to exit and get back to the command line. But you'll see as I exit the containers are actually stopped. This is useful when you maintenance your containers as compose captures any updates stops the containers and then recreates them in order to apply. The changes in order to leave the containers running in the background. We're going to use the flag dash D. Now let's head over to a browser and see if we can pull up the WordPress setup page. If you recall we're using port 8000 to get to the page. And there, you have it, we've successfully deployed our application. Now let's go over a few commands to manage our containers. First, if we want to stop our containers, we simply type in Docker compose stop. Next if we want to restart those containers or start them from a stop state we simply type in Docker compose start. If we want to reboot or restart our containers, either from a stop state or even if the containers are running. We simply type in Docker compose restart. Another useful command is if we want to look at logs from our services. We simply type in Docker compose logs. To clean out our containers, we will run Sudo Docker compose down. This cleans out the containers and networking, but not the persistent volume. As you can see our volume still exist. If you want to clean up the containers networking in associated volumes all-in-one swoop. You must run Docker compose down dash dash volumes. And for our purposes. I restarted our docker containers and I'm going to go ahead and run the command to clean them out completely. You will start to see that the containers are stopping there being removed the network is being removed and also the volume? Thank you for watching.