How Do You Use docker-compose?

Problem scenario
You want to create an example of a docker compose file.  How do you use docker-compose?

Prerequisites
i.  This assumes you have Docker installed and running.  If you need assistance, see this posting.
ii.  This assumes that you have installed docker-compose.  If you need assistance, see this posting.
iii.  This assumes the Docker host has access to the internet.
iv.  Port 8080 is not being used (e.g., any containers that use this port are turned off).  If you need to keep port 8080 open for another purpose, change the "8080" in the directions below.

Procedures
1.  Run these three commands:

mkdir dockercompose && cd dockercompose
mkdir webapp
echo "<h2>It worked\!</h2>" > webapp/index.html

2.a.  Create a Dockerfile in the webapp directory. (You may want to run vi webapp/Dockerfile.)
2.b.   This is the content for the Dockerfile:

FROM tecadmin/ubuntu-ssh:16.04
RUN apt-get update \
   && apt-get install -y apache2
COPY index.html /var/www/html/
WORKDIR /var/www/html
CMD ["apachectl", "-D", "FOREGROUND"]
EXPOSE 80

3.a.  Create a docker-compose.yml file.  (You may want to run vi docker-compose.yml.)
3.b.  This is the content of the docker-compose.yml file:

version: '3'
services:
  db:
     image: mysql
     container_name: mysql_db
     restart: always
     environment:
        - MYSQL_ROOT_PASSWORD="secret"
  web:
    image: apache
    build: ./webapp
    depends_on:
       - db
    container_name: apache_web
    restart: always
    ports:
      - "8080:80"

4.  Run these commands:  

echo "You just used Docker Compose via ContinualIntegration.com" >> webapp/index.html
docker-compose build
docker-compose up -d

5.  Open a web browser.  Go to x.x.x.x:8080 where x.x.x.x is the external IP address of the server (or the result of curl icanhazip.com on the back-end).

6.  You should see these:

"It Worked\!
You just used Docker Compose via ContinualIntegration.com"

You are now done.  (Remember that modifications to the index.html command will require you to use "docker-compose build" once again.  New containers may conflict on port 8080 if you do not change the docker-compose.yml file or stop the containers from running.)

The above directions were adapted (but mostly copied) from Tecadmin.net. The directions above were tested in February of 2022.

How Do You Get Jenkins Working in a Docker Container?

One of the following problems apply:

Problem scenario #1
You have no idea how to start Jenkins in a Docker container.  What should you do?

OR

Problem scenario #2
You have run some Docker commands to try to start deploy Jenkins in a container.  You get output like this:

[4213] [10236] Disconnected
[9146] Connection from [::1]:41118
[9146] Extended attributes (16 bytes) exist <host=localhost>
[9146] Request upload-pack for '/repo'
[4213] [10255] Disconnected

How do you get Jenkins running in a Docker container?

Solution
1.  Install Docker.  If you need assistance with this, see this posting.

2.  Start the Docker service (e.g., run "sudo service docker start")

3.  Run these two commands:

docker run -d -v /var/jenkins_home:/var/jenkins_home:z -p 8080:8080 -p 50000:50000 --name myjenkins jenkins/jenkins:lts

sudo chown 1000:1000 /var/jenkins_home

4.  Run this:  docker ps -a

5.  Look at the output of the above command.  Find the container ID that was just created with step #3.  Then run this command (but substitute abcd1234 with the container ID you just identified):

docker start abcd1234

6.  Docker should now be accessible via the Docker host's external IP address on port 8080.  You can run this on the host to find the external IP address: curl icanhazip.com

In a web browser you should be able to go to x.x.x.x:8080 (where x.x.x.x is the external IP address of the server returned in from the curl command above) and get to the Jenkins instance in the Docker container.

You may want to see this posting for your initial set up because it explains how to enter a Docker container if you are new to Docker.

How Do You Delete Servers in AWS with Python?

Problem scenario
You want to write a Python program to delete servers.  How do you delete EC-2 instances in AWS with Python?

Solution
Prerequisites

a.  You have installed Boto3.  If you do not know how, see this posting
b.  You know the instance IDs.  If you do not know them, see this posting.

Procedures
Read the "Usage instructions" of this Python program we call "deleteec2.py". The content of this Python script is below.

""" Usage instructions
  1. replace "aaa111" with the aws_access_key_id of your account
  2. replace "bbb222" with the aws_secret_access_key of your account
     If you are not sure how to find these credentials do the following:
     To find the AWS Access Key ID and AWS Secret Access Key, in the AWS console, click on your name in the upper right hand corner.
     Then click on "My Security Credentials."  Click "Create New Access Key"  Click "Show Access Key."
  3.  Replace us-west-1 with the region that you want to list EC-2 instances for.
  4.  Replace i-1234abcd with the instance ID of the server you want to delete.  If you have multiple servers, place the instance IDs in single quotes and separate those quoted strings by commas inside the square brackets.
  5.  To run it, call it deleteec2.py and run it with a command like this: "python deleteec2.py"

"""

import boto3

ec2 = boto3.resource('ec2', aws_access_key_id=' aaa111', aws_secret_access_key=' bbb222', region_name='us-west-1')

alist = ['i-1234abcd']  # alist = ['i-1234abcd','i-5678-efgh'] is how multiple servers are deleted
ec2.instances.filter(InstanceIds=alist).stop()
ec2.instances.filter(InstanceIds=alist).terminate()

How Do You Run a Docker Container as a Non-root User?

Problem scenario
You run this command: docker run -it ubuntu bash

docker: Got permission denied while trying to connect to the Docker daemon sockes/create: dial unix /var/run/docker.sock: connect: permission denied.

You do not want to run a Docker container as a privileged user (as a recommended practice).  What should you do?

Solution
Background: "To do builds in the cluster, it is essential to run the build without root privileges." This quote was taken from page 222 of Kubernetes Patterns by Bilgin Ibryam and Roland Huß (O'Reilly). Copyright 2019 Bilgin Ibryam and Roland Huß, 978-1-492-05028-5.

Prerequisites

Install Docker.  If you need assistance, see this posting.

Procedures
1.  Run these commands:

sudo newgrp docker
exit
sudo usermod -aG docker jdoe  
# Replace "jdoe" with the user you want to run the Docker container with.  You can use "whoami" manually to find out what user you are. 

2.  If you were logged in as jdoe (the user you just added), log out and log back in.

3.  Now try again (e.g., run docker run -it ubuntu bash).


You may also want to see this external posting for additional assistance.

How Do You Troubleshoot an ldapsearch -x command Returning “No such object”?

Problem scenario
You want to test your OpenLDAP configuration.  You run an "ldapsearch -x" command in hope that it wil l return a "Success."  You run the "ldapsearch -x" command, and you see this:

"# search result
search: 2
result: 32 No such object"

What should you do?

Solution
Verify your /etc/ldap/ldap.conf is configured correctly.  The BASE stanza should have one or possibly three "dc=" entries separated by commas.

The URI stanza should have FQDNs or internal IP addresses depending on your configuration.  Here is an example of such stanzas from a working ldap.conf file (from a Linux server in Google Cloud Platform):

BASE    dc=c,dc=brave-fudge-193019,dc=internal
URI     ldap://10.142.0.2 ldap://10.142.0.2:666

How Do You Troubleshoot the Bash Error “unary operator expected”?

Problem scenario
You run a Bash script with an if condition that uses the -Z flag.  It looks like this:

if [ $foovar -Z ]; then

The script seems to run with an ignorable error message "unary operator expected".  How do you get this error to not appear?

Solution
Use '= ""' instead of "-Z".  Here is one way to re-write the if condition above:

if [ $foovar = "" ]; then

How Do You Troubleshoot the Error “ERROR 1273 (HY000): Unknown collation: ‘utf8mb4_general_ci6′”?

Problem scenario
You are trying to follow the Drupal installation instructions.  You run this command from a mysql prompt:

mysql> CREATE DATABASE drupaldb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci6;

But you get this error: "ERROR 1273 (HY000): Unknown collation: 'utf8mb4_general_ci6'"

What should you do?

Solution
To find the potential collations that you can run (based on your SQL database), run this command:  

SHOW COLLATION;

From the results above, change the "utf8mb4_general_ci6" value in the CREATE DATABASE command.  Then run a new version of the CREATE DATABASE command:

CREATE DATABASE drupaldb CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

What Is a Quick Way to Get All of Your Emails in One File from a Web-based Email Provider?

Problem scenario
You have a web-based email provider.  You want to save all of your emails in one location quickly.  How do you download them into one file?

Solution
If there are maximum size limits for sending a single email (e.g., 25 MB), this method may not work if all of your emails put together exceed this size limit.*

1.  Click an option to select all emails.  Find a mail icon with a down arrow on the top column and click it with the mouse.  Choose "All".

2.  Then right click one of the highlighted emails and choose "Forward as attachment."  

3.  Forward your emails to yourself or save it as a draft.  

4.  When this email arrives or from your Drafts folder, select the check box for this email and choose "Download (.eml)".  Now you should have a file with all of your emails.

*  Alternatives may include using Outlook or Thunderbird to manipulate your email.  Some web-based email providers support Outlook or Thunderbird configurations.

How Do You Fix Broken Packages on Debian Linux to Be Able to Use The apt Command as Normal?

Problem scenario
This command is not working for you:  sudo apt --fix-broken install

You get a message like this:

dpkg: error processing archive /var/cache/apt/archives/puppet_4.8.2-5_all.deb (--unpack):
 trying to overwrite '/lib/systemd/system/puppet.service', which is also in package puppet-agent 5.4.0.522.g6baf971-1xenial
dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)
Errors were encountered while processing:
 /var/cache/apt/archives/puppet_4.8.2-5_all.deb

You try this command:
sudo apt-get purge -s puppet*

You cannot remove a package.  You get a series of errors.  The errors seem to be circularly related and interdependent. What should you do?

Solution
Run these commands:

sudo apt-get update --fix-missing

sudo apt-get -y remove puppet-agent puppet-master puppet  # try to remove the package and related packages.  Replace "puppet-agent puppet-master puppet" with the packages you want to be removed.

sudo apt --fix-broken install

sudo apt autoremove

sudo apt clean

sudo rm -rf /tmp/*

How Do You Get the Curl Command to Work like wget when the URL Has a Forwarding 300 Series Return Code?

Problem scenario
You do not want to install wget on Linux.  You want curl to download a file from the internet.  How do you get curl to work through a forwarding request the same way wget natively works?

Solution
Use the "-L" flag.   For example:

curl -L www.continualintegration.com > /tmp/good.html