Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Linux Shell Scripting Cookbook

You're reading from   Linux Shell Scripting Cookbook Do amazing things with the shell and automate tedious tasks

Arrow left icon
Product type Paperback
Published in May 2017
Publisher
ISBN-13 9781785881985
Length 552 pages
Edition 3rd Edition
Tools
Arrow right icon
Authors (3):
Arrow left icon
Clif Flynt Clif Flynt
Author Profile Icon Clif Flynt
Clif Flynt
Sarath Lakshman Sarath Lakshman
Author Profile Icon Sarath Lakshman
Sarath Lakshman
Shantanu Tushar Shantanu Tushar
Author Profile Icon Shantanu Tushar
Shantanu Tushar
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Shell Something Out FREE CHAPTER 2. Have a Good Command 3. File In, File Out 4. Texting and Driving 5. Tangled Web? Not At All! 6. Repository Management 7. The Backup Plan 8. The Old-Boy Network 9. Put On the Monitors Cap 10. Administration Calls 11. Tracing the Clues 12. Tuning a Linux System 13. Containers, Virtual Machines, and the Cloud

Using Docker

The lxc containers are complex and can be difficult to work with. These issues led to the Docker package. Docker uses the same underlying Linux functionalities of namespaces and cgroups to create lightweight containers.

Docker is only officially supported on 64-bit systems, making lxc the better choice for legacy systems.

The major difference between a Docker container and an lxc container is that a Docker container commonly runs one process, while an lxc container runs many. To deploy a database-backed web server, you need at least two Docker containers–one for the web server and one for the database server–but only one lxc container.

The Docker philosophy makes it easy to construct systems from smaller building blocks, but it can make it harder to develop blocks since so many Linux utilities are expected to run inside a full Linux system with crontab entries to carry out operations such as cleanup, log rotation, and so on.

Once a Docker container is created, it will run exactly as expected on other Docker servers. This makes it very easy to deploy Docker containers on cloud clusters or remote sites.

Getting ready

Docker is not installed with most distributions. It is distributed via Docker's repositories. Using these requires adding new repositories to your package manager with new checksums.

Docker has instructions for each distribution and different releases on their main page, which is available at http://docs.docker.com.

How to do it...

When Docker is first installed, it is not running. You must start the server with a command such as the following:

    # service docker start

The Docker command has many subcommands that provide functionality. These commands will find a Docker container and download and run it. Here's a bit about the subcommands:

  • # docker search: This searches Docker archives for containers with names that match a key
  • # docker pull: This pulls the named container to your system
  • # docker run: This runs an application in a container
  • # docker ps: This lists the running Docker containers
  • # docker attach: This attaches to a running container
  • # docker stop: This stops a container
  • # docker rm: This removes a container

The default Docker installation requires that the docker command be run either as a root or using sudo.

Each of these commands have a man page. This page is named by combining the command and subcommand with a dash. To view the docker search man page, use man docker-search.

The next recipe demonstrates how to download a Docker container and run it.

Finding a container

The docker search command returns a list of Docker containers that match a search term:

    docker search TERM

Here TERM is an alphanumeric string (no wild cards). The search command will return up to 25 containers that include the string in their name:

# docker search apache
NAME            DESCRIPTION                STARS OFFICIAL   AUTOMATED
eboraas/apache  Apache (with SSL support)  70                   [OK]
bitnami/apache  Bitnami Apache Docker      25                   [OK]
apache/nutch    Apache Nutch               12                   [OK]
apache/marmotta Apache Marmotta             4                   [OK]
lephare/apache  Apache container            3                   [OK]

Here STARS represent a rating for the container. The containers are ordered with the highest rating first.

Downloading a container

The docker pull command downloads a container from the Docker registry. By default, it pulls data from Docker's public registry at registry-1.docker.io. The downloaded container is added to your system. The containers are commonly stored under /var/lib/docker:

# docker pull lephare/apache
latest: Pulling from lephare/apache
425e28bb756f: Pull complete 
ce4a2c3907b1: Extracting [======================> ] 2.522 MB/2.522 MB
40e152766c6c: Downloading [==================>    ] 2.333 MB/5.416 MB
db2f8d577dce: Download complete 
Digest: sha256:e11a0f7e53b34584f6a714cc4dfa383cbd6aef1f542bacf69f5fccefa0108ff8
Status: Image is up to date for lephare/apache:latest

Starting a Docker container

The docker run command starts a process in a container. Commonly, the process is a bash shell that allows you to attach to the container and start other processes. This command returns a hash value that defines this session.

When a Docker container starts, a network connection is created for it automatically.

The syntax for the run command is as follows:

    docker run [OPTIONS] CONTAINER COMMAND

The docker run command supports many options, including:

  • -t: Allocate a pseudo tty (by default, false)
  • -i: Keep an interactive session open while unattached
  • -d: Start the container detached (running in the background)
  • --name: The name to assign to this instance

This example starts the bash shell in the container that was previously pulled:

    # docker run -t -i -d --name leph1 lephare/apache  /bin/bash
    1d862d7552bcaadf5311c96d439378617d85593843131ad499...

Listing the Docker sessions

The docker ps command lists the currently running Docker sessions:

# docker ps
CONTAINER ID  IMAGE           COMMAND   CREATED  STATUS  PORTS  NAMES
123456abc     lephare/apache  /bin/bash 10:05    up      80/tcp leph1

The -a option will list all the Docker containers on your system, whether they are running or not.

Attaching your display to a running Docker container

The docker attach command attaches your display to the tty session in a running container. You need to run as the root within this container.

To exit an attached session, type ^P^Q.

This example creates an HTML page and starts the Apache web server in the container:

$ docker attach leph1
root@131aaaeeac79:/# cd /var/www
root@131aaaeeac79:/var/www# mkdir symfony
root@131aaaeeac79:/var/www# mkdir symfony/web
root@131aaaeeac79:/var/www# cd  symfony/web
root@131aaaeeac79:/var/www/symfony/web# echo "<html><body><h1>It's Alive</h1></body></html>"   
    >index.html
root@131aaaeeac79:/# cd /etc/init.d
root@131aaaeeac79:/etc/init.d# ./apache2 start
[....] Starting web server: apache2/usr/sbin/apache2ctl: 87: ulimit: error setting limit (Operation 
    not permitted)
Setting ulimit failed. See README.Debian for more information.
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 
    172.17.0.5. Set the 'ServerName' directive globally to suppress this message
. ok 

Browsing to 172.17.0.5 will show the It's Alive page.

Stopping a Docker session

The docker stop command terminates a running Docker session:

    # docker stop leph1

Removing a Docker instance

The docker rm command removes a container. The container must be stopped before removing it. A container can be removed either by name or identifier:

    # docker rm leph1

Alternatively, you can use this:

    # docker rm 131aaaeeac79

How it works

The Docker containers use the same namespace and cgroup kernel support as that of the lxc containers. Initially, Docker was a layer over lxc, but it has since evolved into a unique system.

The main configuration files for the server are stored at /var/lib/docker and /etc/docker.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image