{"id":1970,"date":"2024-02-24T13:28:57","date_gmt":"2024-02-24T12:28:57","guid":{"rendered":"https:\/\/robermb.com\/blog\/?p=1970"},"modified":"2024-07-01T12:27:25","modified_gmt":"2024-07-01T10:27:25","slug":"my-ansible-lab-using-docker-containers","status":"publish","type":"post","link":"https:\/\/robermb.com\/blog\/geeks\/my-ansible-lab-using-docker-containers\/","title":{"rendered":"My Ansible Lab using Docker containers"},"content":{"rendered":"\n<p>This is my personal project to use Docker containers for Ansible playbooks testings. <\/p>\n\n\n\n<p>It&#8217;s faster to run your tests on your local machine than on your remote GitLab repository, pushing every time you make changes to the code, etc.<\/p>\n\n\n\n<p><strong>My GitLab project<\/strong>:  <a href=\"https:\/\/gitlab.com\/RoberMB\/lab_docker\">https:\/\/gitlab.com\/RoberMB\/lab_docker<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Requirements<\/h3>\n\n\n\n<ul><li>Download and install Docker Desktop for macOS:<\/li><\/ul>\n\n\n\n<p>Docker Desktop 4.16.2 (2023-01-19): <a rel=\"noreferrer noopener\" href=\"https:\/\/www.docker.com\/products\/docker-desktop\/\" target=\"_blank\">https:\/\/www.docker.com\/products\/docker-desktop\/<\/a> (compatible with macOS Big Sur 11.0.0 and above)<\/p>\n\n\n\n<p>Docker Desktop 4.15.0 (2022-12-01): <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.docker.com\/desktop\/release-notes\/#4150\" target=\"_blank\">https:\/\/docs.docker.com\/desktop\/release-notes\/#4150<\/a> (compatible with macOS Catalina 10.15.7 and below)<\/p>\n\n\n\n<ul><li>Clone my GitLab repository with all the lab files:<\/li><\/ul>\n\n\n\n<pre id=\"code-86\" class=\"wp-block-code\"><code>robermb@MacBook-Pro-de-RoberMB $ git clone https:\/\/gitlab.com\/RoberMB\/lab_docker.git<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Build my_lab and deploy the containers:<\/h3>\n\n\n\n<ul><li>Build my_lab:<\/li><\/ul>\n\n\n\n<pre id=\"code-86\" class=\"wp-block-code\"><code>robermb@MacBook-Pro-de-RoberMB lab_docker $ . 1-build_my_lab.sh <\/code><\/pre>\n\n\n\n<ul><li>Create containers:<\/li><\/ul>\n\n\n\n<pre id=\"code-87\" class=\"wp-block-code\"><code>robermb@MacBook-Pro-de-RoberMB lab_docker $ . 2-create_containers_from_inventory.sh inventory_small<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Commands:<\/h3>\n\n\n\n<ul><li>Build or rebuild services:<\/li><\/ul>\n\n\n\n<pre id=\"code-88\" class=\"wp-block-code\"><code># Build Docker services defined in the docker-compose.yml file\n$ docker-compose build<\/code><\/pre>\n\n\n\n<ul><li>Create container node_manager:<\/li><\/ul>\n\n\n\n<pre id=\"code-89\" class=\"wp-block-code\"><code># Start the Docker service named 'node_manager' in detached mode\n$ docker-compose up -d node_manager<\/code><\/pre>\n\n\n\n<ul><li>Connect to node_manager container:<\/li><\/ul>\n\n\n\n<pre id=\"code-90\" class=\"wp-block-code\"><code># Access the bash shell of the 'node_manager' Docker container\n$ docker exec -ti node_manager bash<\/code><\/pre>\n\n\n\n<ul><li>Create container target_node1:<\/li><\/ul>\n\n\n\n<pre id=\"code-91\" class=\"wp-block-code\"><code># Run a Docker container named 'target_node1' with specific configurations\n$ docker run -d -ti --privileged --cgroupns=host  -v \/sys\/fs\/cgroup:\/sys\/fs\/cgroup:rw --network lab_docker_default --name target_node1 lab_docker-target_node<\/code><\/pre>\n\n\n\n<ul><li>Create container target_node2:<\/li><\/ul>\n\n\n\n<pre id=\"code-92\" class=\"wp-block-code\"><code># Run a Docker container named 'target_node2' with specific configurations\n$ docker run -d -ti --privileged --cgroupns=host  -v \/sys\/fs\/cgroup:\/sys\/fs\/cgroup:rw --network lab_docker_default --name target_node2 lab_docker-target_node<\/code><\/pre>\n\n\n\n<ul><li>Here the specific configurations I&#8217;ve mentioned before:<\/li><\/ul>\n\n\n\n<pre id=\"code-93\" class=\"wp-block-code\"><code>-d: Run the container in the background (detached mode).\n\n-ti: Allocate a pseudo-TTY and keep STDIN open, allowing interactive access.\n\n--privileged: Give extended privileges to this container. This option gives the container full access to the host system.\n\n--cgroupns=host: Share the host's cgroup namespace with the container, allowing the container to view and manipulate cgroups on the host.\n\n-v \/sys\/fs\/cgroup:\/sys\/fs\/cgroup:rw: Mount the host's cgroup filesystem into the container. This is related to container resource control using cgroups.\n\n--network lab_docker_default: Connect the container to the 'lab_docker_default' network.\n\n--name target_node1: Assign the name 'target_node1' to the running container.\n\nlab_docker-target_node: The name of the Docker image used to create the container.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Avoid error during a second run:<\/h3>\n\n\n\n<p>If &#8220;target_node1&#8221; or\/and &#8220;target_node2&#8221; nodes already exist you will receive the following error. This means that we have already deployed the containers before and just need to start them up:<\/p>\n\n\n\n<pre id=\"code-94\" class=\"wp-block-code\"><code>docker: Error response from daemon: Conflict. \nThe container name \"\/target_node1\" is already in use by \ncontainer \"51668206d2461eb402543a1daa0914d4480ac2f8f4abaaa941d762ff822c9231\". \nYou have to remove (or rename) that container to be able to reuse that name.<\/code><\/pre>\n\n\n\n<p>To avoid that you can execute the docker start command to start the existing containers:<\/p>\n\n\n\n<pre id=\"code-95\" class=\"wp-block-code\"><code># Start Docker container target_node1\n$ docker start target_node1\n\n# Start Docker container target_node2\n$ docker start target_node2<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Checking Docker commands<\/h3>\n\n\n\n<ul><li>Show docker containers, images, etc:<\/li><\/ul>\n\n\n\n<pre id=\"code-96\" class=\"wp-block-code\"><code># Display running containers\n$ docker ps\n\n# Display all containers (including stopped ones)\n$ docker ps -a\n\n# List Docker images\n$ docker images\n\n# List Docker networks\n$ docker network ls<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Tests<\/h3>\n\n\n\n<ul><li>From container node_manager:<\/li><\/ul>\n\n\n\n<pre id=\"code-97\" class=\"wp-block-code\"><code>$ docker exec -ti node_manager bash<\/code><\/pre>\n\n\n\n<ul><li>Connect once to target hosts:<\/li><\/ul>\n\n\n\n<pre id=\"code-98\" class=\"wp-block-code\"><code>root@node_manager $ ssh target_node1\nAre you sure you want to continue connecting (yes\/no\/&#91;fingerprint])? yes\n\nroot@node_manager $ ssh target_node2\nAre you sure you want to continue connecting (yes\/no\/&#91;fingerprint])? yes<\/code><\/pre>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background\"><em><span style=\"text-decoration: underline;\"><strong>Note<\/strong><\/span>: ssh password is set into the file &#8216;target_node.Dockerfile&#8217; (example for the lab: mypassword)<\/em><\/p>\n\n\n\n<ul><li>Execute the playbook ping.yml:<\/li><\/ul>\n\n\n\n<pre id=\"code-99\" class=\"wp-block-code\"><code>root@node_manager $ cd \/applis\/12402-acfrb\/repository\/SAaas\/roles\n\nroot@node_manager $ ansible-playbook ansible-tests\/others\/ping.yml -i ansible-tests\/inv_target_nodes<\/code><\/pre>\n\n\n\n<p>Or remotely:<\/p>\n\n\n\n<pre id=\"code-100\" class=\"wp-block-code\"><code>robermb@MacBook-Pro-de-RoberMB $ docker exec -ti node_manager ansible-playbook -i target_nodes .\/roles\/others\/ping.yml<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">WORKAROUND<\/h3>\n\n\n\n<p>Workaround for &#8220;Unable to run systemd services on Docker Desktop&#8221; error: <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/docker\/for-mac\/issues\/6073\" target=\"_blank\">https:\/\/github.com\/docker\/for-mac\/issues\/6073<\/a><\/p>\n\n\n\n<pre id=\"code-101\" class=\"wp-block-code\"><code>Dropping back to Docker Desktop 4.2.0 fixes the issue. I assume that it's related to the note in the 4.3.0 release notes:\n\nDocker Desktop now uses cgroupv2. If you need to run systemd in a container then:\n\n* Ensure your version of systemd supports cgroupv2. It must be at least systemd 247. Consider upgrading any centos:7 images to centos:8.\n\n* Containers running systemd need the following options: --privileged --cgroupns=host -v \/sys\/fs\/cgroup:\/sys\/fs\/cgroup:rw.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">OFFICIAL DOCUMENTATION<\/h3>\n\n\n\n<ul><li>Docker: The base command for the Docker CLI.<\/li><\/ul>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/docs.docker.com\/engine\/reference\/commandline\/docker\/\" target=\"_blank\">https:\/\/docs.docker.com\/engine\/reference\/commandline\/docker\/<\/a> <\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/docs.docker.com\/engine\/reference\/commandline\/compose_build\/\" target=\"_blank\">https:\/\/docs.docker.com\/engine\/reference\/commandline\/compose_build\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is my personal project to use Docker containers for Ansible playbooks testings. It&#8217;s faster to run your tests on &hellip; <a href=\"https:\/\/robermb.com\/blog\/geeks\/my-ansible-lab-using-docker-containers\/\" class=\"more-link\">More <span class=\"screen-reader-text\">My Ansible Lab using Docker containers<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1806,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[103,2],"tags":[106,112,126,101],"_links":{"self":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/1970"}],"collection":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/comments?post=1970"}],"version-history":[{"count":11,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/1970\/revisions"}],"predecessor-version":[{"id":2156,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/1970\/revisions\/2156"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media\/1806"}],"wp:attachment":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media?parent=1970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/categories?post=1970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/tags?post=1970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}