DCA 11 - Docker Stack Deployment

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
344
Reaction score
377
Credits
11,920
In this article, we will cover the ability to use a text file, which is a YAML file, to build and load containers. We will also set up multiple containers and cause them to be load-balanced, all from a YAML file.

There are a few extra steps to get some necessary files installed, but it's not too hard to do.

Install Docker Compose

The first thing when dealing with YAML files is to install the Docker Compose file that is needed.

Before installing 'docker-compose', we need the 'pip' file installed.

On CentOS, use the command 'sudo yum install python3-pip'. Be sure you have installed the 'epel-release' repository for CentOS. For Ubuntu, use the command 'sudo apt install python3-pip'.

Once you have the 'python3-pip' installed, you will use it to upgrade itself with the command 'sudo python3 -m pip install -U pip'. The upgrade command can be used on either OS.

Now that 'pip' is installed and updated, we can use it to install 'docker-compose'. Simply use the command 'sudo pip install docker-compose'.

You should be able to use the command 'docker-compose' and get a listing of valid parameters for the command. If the parameters are not listed, then you need to retry the commands in this section to get the 'docker-compose' properly installed.

Testing Our First YAML File

Let's create a YAML file to run in Docker and see if we can get it to work with 'docker-compose'.

Make a new folder called 'DockerFiles' in your Home folder. Next, we'll move into the 'DockerFiles' folder and create a new file by opening it with an editor. The file should be called 'Dockerfile'.

Place the following into the file:

FROM ubuntu:latest
RUN ap update
RUN apt install httpd -y

RUN echo "Linux.org" >> /var/www/html/index.html

EXPOSE 80
ENTRYPOINT apachectl -DFOREGROUND


Save the file. From within the folder, run the command 'docker build -t webservice:v1 .'. The last period uses the current folder and looks for a file named 'Dockerfile'. We are building a new container named 'webservice:v1'.

From the YAML file, we have 6 simple lines, I'm not counting the two blank lines. In the first line, we will download the latest 'Ubuntu' image. We then update the repository list and then run the command to install 'httpd'. In the fourth line, we create the file 'index.html' and place the line 'Linux.org' into it. The next line exposes port 80 so we can access the web service. The last line will run 'apachectl' in the foreground.

Now, the image should be made and we can run the image as a container with the command 'docker run -d --name web -p 80:80 webservice:v1'. I am giving the container the name 'web'.

You should be able to use the command 'docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web' to retrieve the IP Address of the container. You can use Elinks or a regular web browser like Firefox to open the address returned. On my system, the container had the IP Address of 172.17.0.2. Both browsers opened a page that simply said 'Linux.org'.

This proves our YAML file worked just fine. Now, we can remove the containers and images we created. Use the following commands:

docker stop web
docker rm web
docker rmi webservice:v1


You should now have the container stopped and removed, as well as the image.

So let's look at creating a YAML file to create multiple containers and a load-balancing service.

Multiple Containers from a YAML File

Let's open an editor and create the file 'docker-compose.yml', then add the following lines:

version: "3"
services:
web1:
image: webservice:v1
build: .
ports:
- "81:80"
web2:
image: webservice:v1
ports:
- "82:80"
load-balance:
image: nginx:latest
ports:
- "80:80"


Save the file. Make sure there are only spaces and no tabs. If you copy and paste from above, you may need to remove the tabs.

We are creating 3 images named 'web1', 'web2' and 'load-balance'. In the 'web#' images, we are using the 'webservice:v1' image from the previous section. In 'web1' we are building the image, but not in 'web2' since it is already built. We are placing each 'web#' on a different port and balancing it with Port 80 on 'load-balance'.

To start the YAML file, we can use the command 'docker-compose up -d', which looks for the file 'docker-compose.yml'.

An error will occur with 'nginx' being on port 80. When Docker builds the image for 'webservice:v1' it also starts it. So port 80 is in use.

You can stop 'webservice:v1' to open Port 80 back up. It is then possible to open the images as a service with the command 'docker stack deploy -c ./docker-compose.yml web'. I am still in the same folder as the 'docker-compose.yml' file, or I would have to give the full path to it. You can see that these are a service with the command 'docker service ls'.

I was able to check the individual Ports on IP Address '172.18.0.2:8x' depending if you want '81' or '82'. If you connect to '172.18.0.2:80', you'll see the default NGINX page. To get the load_balance to work, we would need to configure NGINX properly, but that is beyond the scope of this article.

To stop the containers started with 'docker-compose', you can use the command 'docker-compose down --volumes'. To remove the services, use 'docker service rm <service name>'. You can get the names with the command 'docker service ls'.

Conclusion

Hopefully, this can get you started on using YAML files. They aren't too difficult for basic builds.

There are a lot of parameters to make a more in-depth build with all kinds of tweaks. The DCA exam does not go into any detail on YAML files. It is still good to have an understanding of them and also set up a stack.
 



If you want to start an introduction thread, do that in the "Member Introductions" section of the site.

If you want to ask a question, start a new thread in the most appropriate sub-forum.

Thanks.
 

Members online


Latest posts

Top