How to restart a service running in docker compose

I’m playing with docker-compose for a test Flask app. My docker-compose.yml looks like this:

version: '2'
services:

  www:
    build: www/.
    ports:
      - "5000:5000"
    volumes:
      - ./www:/opt/www
    links:
      - db
    depends_on:
      - db

  db:
    build: db/.
    volumes:
      - ./database:/var/lib/mysql
    environment:
       MYSQL_ROOT_PASSWORD: supersecure

I noticed any change I made in my files within my templates folder did not take effect when I reloaded the browser locally at localhost:5000

So, I found that I can just restart the ‘www’ service defined on my docker-compose.yml file using this simple command:

docker-compose restart www

I’m not sure if this is the recommended way, but its working for now. If you have any suggestions please leave them on the comments area below.

Connect to a remote MySQL database using Linux terminal

This is simple. After you setup a user with proper access rights run the below commands.

# mysql -u yourUser -p -h <yourHostname or IP>

A little explanation:

-u tells mysql what your username

-p tells mysql you have a password and will prompt you to enter it after you press enter

-h tells mysql the hostname or IP address of your MySQL server

Good luck! Contact me if you have any questions.

Restart sound service in Ubuntu

The other day my sound stopped working on Ubuntu 18.04 desktop. I have no clue why. The sound stopped working overnight for some reason. After some research I found the below command to restart the sound service on Ubuntu:

pulseaudio -k && sudo alsa force-reload

All works now! Give it a try and let me know if it works for you. Follow me on Twitter @itprohelper

Minicom error “Device /dev/ttyS0 is locked”

Sometimes if the connection to a device via Minicom is lost, the next time you start Minicom you could see an error:

Device /dev/ttyS0 is locked.

The name /dev/ttyS0 may be different, depending on your COM port. To avoid this issue you should always shut down Minicom using CTRL+A keys and then the Q key.

To resolve the issue you can kill the process as a root user. Use the following command in your terminal:

killall -9 minicom

In Ubuntu is similar. Run the command in your terminal using sudo:

sudo killall -9 minicom

You can also try to delete the file:

LCK..ttyS0 in the /var/lock directory.

That was all for me! Hope this little tutorial was useful. If you like to learn more about networking check out this book CompTIA Network+ Certification All-in-One Exam Guide, Seventh Edition (Exam N10-007)

How to run Ansible when Python not installed on Ubuntu EC2

I started an Ubuntu EC2 the other day hoping to run Ansible on it and was surprised that Python was not installed. A requirement for Ansible to run on target machines is to have a modern version of Python.

You can just run sudo apt install python but this is not ideal if you need to install python on let’s say 200 Ubuntu Servers.

The raw module to the rescue

Before writing the playbook, you will need to make minor changes to how Ansible connects to remote hosts.

Create a new file in your current directory and call it ansible.cfg. This file will override settings in the default /etc/ansible/ansible.cfg. Example:

[defaults]
inventory = hosts
remote_user = ubuntu
# Use the below if you’re connecting for the first time
host_key_checking = False
# Location of your .pem you got from AWS
private_key_file = ~/.ssh/yourkey.pem

Now create a hosts file:

[ubuntu_server]
34.210.125.120 # This is a sample IP address. Use your IP.

The playbook using the raw module

This playbook will install python on the target host Ubuntu EC2 server:

- hosts: ubuntu_server
become: yes
gather_facts: no
pre_tasks:
- name: 'install python'
raw: 'sudo apt -y install python'

Breaking Up Long Lines in Your Dockerfile

I try to limit my line characters to 79-80 per line. For example check this 122 character multi-command line:

RUN wget -O afile.tar.gz http://example.com/afile.tar.gz && tar -xvf afile.tar.gz -C /usr/src/myapp && rm afile.tar.gz

You will need to scroll horizontally using your text editor to see the super long crazy line. This is not efficient and also creates a mess when you or someone else needs to review your code.

Below is the same command broken into multiple lines:

RUN wget -O afile.tar.gz http://example.com/afile.tar.gz \
    && tar -xvf afile.tar.gz -C /usr/src/myapp \
    && rm afile.tar.gz

So neat and comfortable to read now! You just need to use a backslash ‘ \ ‘ to break up lines. Enjoy!

Using .gitignore examples

A .gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Normally, this is placed in the root directory of your repository. This is the recommended way. The patterns in the .gitignore file are relative to the location of the file.

File names

Easiest pattern is a literal file name:

.DS_Store

This will ignore any files named .DS_Store which is common in MacOS.

Directories

You can ignore entire directories by including their paths and putting a / at the end:

node_modules/
logs/
secret_directory/

If you leave the slash ‘ / ‘ at the end you will match both directories and file names.

GREP command overview

The grep command means Global Regular Expression Print. This Linux command is one of the most useful commands out there. Below are some basic examples:

grep 'word' filename
grep 'word' filename1 filename2 filename3
grep 'string1' string2' filename
cat filename | grep 'something'
command | grep 'something'

Next example, you can search for the user ‘tom’ in the Linux passwd file:

grep tom /etc/passwd

You can instruct grep to ignore word case. Match abc, Abc, ABC and all possible combinations:

grep -i "tom" /etc/passwd

How to change the default SSH port in MacOS using Terminal

This is simple. Open a terminal and follow the steps below. This worked for me on MacOS 10.13.6

  • Open the Terminal.
  • Run sudo vim /etc/services
  • Enter your password
  • Find the lines assigned to port 22, something like the image below

Replace both ports 22 with your desired ssh port using a number between 1024 and 32,767

Now you need to restart the services. Be sure to do this if you have direct access to the console for your MacOS. If you do it remotely you may loose connection. I could not find a way to ‘restart’ a service on MacOS as we normally do in Linux. You have to ‘unload’ and ‘load’ the service in MacOS. Use the below commands.

sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist
sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist

Now you can SSH using:

ssh <youruser>@<yourHostOrIP> -p <yourNewPort>
For example: ssh john@localhost -p 18765

Hope this small post was useful. If you like to learn more about MacOS you can grab this book MacOS and iOS Internals, Volume I: User Mode (v1.3)