Skip to main content

Ubuntu

Ubuntu Server Installation

This article will cover how to install Ubuntu Server by using different methods. We will have a look at how to install Ubuntu Server in the manual and automated mode by using a simple or an advanced installation.

To reach this goal, we will follow this plan:

  • Preparation before the installation
  • The manual installation
  • The automated installation

Adding a cloud image to OpenStack in Ubuntu

In the previous recipe, we installed and configured OpenStack. Now, to start using the service, we need to upload virtual machine images. The OpenStack installation uploads a test image named Cirros. This is a small Linux distribution designed to be used as a test image in the cloud. We will upload prebuilt cloud images available from Ubuntu.

Getting ready

Make sure you have installed the OpenStack environment and you can access the OpenStack dashboard with valid credentials. It is not necessary to have an admin account to create and upload images.

Select the cloud image of your choice and get its download URL. Here, we will use the Trusty Ubuntu Server image. The selected image format is QCOW2, though OpenStack support various other image formats. The following is the URL for the selected image:

https://cloud-images.ubuntu.com/trusty/current/trusty-server- cloudimg-amd64-disk1.img

How to do it…

The OpenStack dashboard provides a separate section for image management. You can see the images that are already available and add or remove your own images. Follow these steps to create your own image:

Log in to your OpenStack dashboard. On successful login, you should get an Overview page for your account.

Now, from the left-hand side Project menu, under the Compute submenu, click on Images:

This should show you a list of all publicly available images—something like this:

Click on the Create Image button to add a new image. This should open a popup box with various details. Here, you can choose to add an image URL or enter an image path if you have downloaded the image to your local machine.

Fill in the name and other required details. Under Image Source, select the image location, and in the next box, Image Location, enter the URL for the Ubuntu Cloud image.

Under Format, select the image format of your selected image. In this case, it's QCOW2.

Enter amd64 under Architecture. Make sure you match this with your selected image.

Enter the minimum disk and RAM size. As we have selected an Ubuntu image, the minimum disk size should be 5 GB and minimum RAM 256 MB. These values will affect the selection of instance flavors while creating a new instance.

Finally, click on the Create Image button to save the details and add the image to OpenStack. This will download the image from the source URL and save it in the image repository. The resulting image will be listed under the Project tab, as follows:

Now, the image is ready can be used to launch new cloud instances.

How it works…

OpenStack is a cloud virtualization platform and needs operating system images to launch virtual machines in the cloud. The Glance OpenStack imaging service provides the image-management service. It supports various types of image, including Qemu format, raw disk files, ISO images, and images from other virtualization platforms, as well as Docker images. Like every other thing in OpenStack, image management works with the help of APIs provided by Glance.

OpenStack, being a cloud platform, is expected to have ready-to-use images that can be used to quickly start a virtual instance. It is possible to upload the operating system installation disk and install the OS to a virtual instance, but that would be a waste of resources. Instead, it is preferable to have prebuilt cloud images. Various popular operating systems provide their respective cloud images, which can be imported to cloud systems. In the previous example, we used the Ubuntu Cloud image for the Ubuntu Trusty release.

We imported the image by specifying its source URI. Local image files can also be uploaded by selecting the image file as an image source. You can also build your own images and upload them to the image store to be used in the cloud. Along with the image source, we need to provide a few more parameters, which include the type of the image being uploaded and the minimum resource requirements of that image. Once the image has been uploaded, it can be used to launch a new instance in the cloud. Also, the image can be marked as public so that it is accessible to all OpenStack users. You will need specific rights for your OpenStack account to create public images.

There's more…

OpenStack images can also be managed from the command line with the client called glance. To access the respective APIs from the command line, you need to authenticate with the Glance server. Use the following steps to use glance from the command line:

First, add authentication parameters to the environment:

export OS_USERNAME=demo

export OS_PASSWORD=password

export OS_AUTH_URL=http://10.196.69.158/identity

export OS_TENANT_ID=8fe52bb13ca44981aa15d9b62e9133f4

Now, use the following command to obtain the image list for the specified user:

$ glance image-list

You can get a list of available command-line options with glance help.

See also

Read more about OpenStack image management: http://docs.openstack.org/image-guide/

Command-line image management: http://docs.openstack.org/user- guide/common/cli_manage_images.html

Dashboard image management: http://docs.openstack.org/user- guide/dashboard_manage_images.html

Glance documentation: http://docs.openstack.org/developer/glance/

Installing MongoDB in Ubuntu

Until now, we have worked with the relational database server, MySQL. In this recipe, we will learn how to install and configure MongoDB, which is a not only SQL (NoSQL) document storage server.

Getting ready

You will need access to a root account or an account with sudo privileges.

How to do it…

To get the latest version of MongoDB, we need to add the MongoDB source to Ubuntu installation sources:

First, import the MongoDB GPG public key:

$ sudo apt-key adv \

--keyserver hkp://keyserver.ubuntu.com:80 \

--recv 7F0CEB10

Create a list file and add an install source to it:

$ echo “deb http://repo.mongodb.org/apt/ubuntu “$(lsb_release -sc)”/mongodb-org/3.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list

Update the apt repository sources and install the MongoDB server:

$ sudo apt-get update

$ sudo apt-get install -y mongodb-org

After installation completes, check the status of the MongoDB server:

$ sudo service mongod status

Now you can start using the MongoDB server. To access the Mongo shell, use the following command:

$ mongo

How it works…

We have installed the MongoDB server from the MongoDB official repository. The Ubuntu package repository includes the MongoDB package in it, but it is not up to date with the latest release of MongoDB. With GPG keys, Ubuntu ensures the authenticity of the packages being installed. After importing the GPG key, we have created a list file that contains the installation source of the MongoDB server.

After installation, the MongoDB service should start automatically. You can check logs at /var/log/mongodb/mongod.log.

See also

MongoDB installation guide at http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

Adding e-mail accounts in Ubuntu

In this recipe, we will learn how to add e-mail accounts to Postfix. The easiest way to add a new e-mail account to Postfix is to add a new user account on your server. Postfix will check for user accounts and deliver e-mails to respective users. We will create a virtual user setup so that we do not need to create user accounts for each e-mail user.

Getting ready

You will need access to a root account or an account with sudo privileges.

I assume that you have completed your basic Postfix setup and that it is working properly.

How to do it…

Follow these steps to add e-mail account:

Create a new user account:

$ useradd -s /usr/bin/nologin -m vmail

Get the UID and GID for this account:

$ grep vmail /etc/passwd

vmail:x:1001:1001::/home/vmail:/usr/bin/nologin

Create a base directory layout for domains and users:

$ sudo mkdir -p /home/vmail/example.org/bob

$ sudo mkdir -p /home/vmail/example.net/alice

Allow only the user vmail to access these files:

$ sudo chown -R vmail:vmail /home/vmail

$ chmod -R 700 /home/vmail

Next, configure Postfix. Edit /etc/postfix/main.cf and add the following lines:

virtual_mailbox_base = /home/vmail

virtual_mailbox_domains = /etc/postfix/virtual_domains

virtual_mailbox_maps = hash:/etc/postfix/virtual_maps

virtual_alias_maps = hash:/etc/postfix/virtual_alias

virtual_uid_maps = static:1001 # user ID for user vmail

virtual_gid_maps = static:1001 # group ID for user vmail

Create the file virtual_domains under /etc/postfix:

$ sudo nano /etc/postfix/virtual_domains

example.org

example.net

Create the virtual_maps file:

$ sudo nano /etc/postfix/virtual_maps

bob@example.org example.org/bob/

alice@example.org example.org/alice/

@example.org example.org/catchall/ # catch all address

Create the virtual_alias file and optionally set a redirect:

$ sudo nano /etc/postfix/virtual_alias

# redirect emails for tim to bob

tim@example.org bob@example.org

Now generate database of virtual maps and aliases by hashing respective files:

$ sudo postmap /etc/postfix/virtual_maps

$ sudo postmap /etc/postfix/virtual_alias

Reload Postfix and send an e-mail to the newly created address:

$ sudo postfix reload

$ sendmail bob@example.org

How it works…

Here, we have created a virtual mailbox setup to enable our Postfix server to serve multiple domains as well as add e-mail users without creating user accounts on the server. All e-mails received by virtual users will be stored under the home directory of the vmail user (virtual_mailbox_base in Postfix configuration). When you need to add a new e-mail account, simply add the e-mail address with its respective domain to the virtual_maps file. In case you need to support a new domain, you can easily add it to the virtual_domains file.

The third file we used is virtual_alias. You can set e-mail forwarding in this file. It is handy when you need to create a new alias for an e-mail address or forward e-mails to one or multiple accounts. We have set a catchall entry in the virtual_alias file; this setting will redirect all e-mails received on nonexistent accounts to catchall@example.org, which can be checked by the domain administrator.

There's more…

Using files for virtual users and domains is good for getting started with setup. But once you need to add more and more user accounts and domains it is a good idea to move the users and domains to a database server. This can be easily done by changing the lookup table type. Postfix supports a variety of lookup table types, which include LDAP, MySQL, PGSQL, memcache, SQLite, and many others.

To use MySQL as a backend database, complete the following steps:

Create respective tables for virtual_domain, virtual_maps, and virtual_alias.

Change the Postfix configuration to use MySQL as a lookup table:

virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-domains

virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-maps

virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias

Add the respective details to each file using the following commands:

$ sudo nano /etc/postfix/mysql-virtual-domains

user = mysql_user

password = mysql_password

hosts = 127.0.0.1

dbname = mysql_db_name

query = SELECT 1 FROM virtual_domains WHERE name='%s'

$ sudo nano /etc/postfix/mysql-virtual-maps

...

query = SELECT 1 FROM virtual_users WHERE email='%s'

$ sudo nano /etc/postfix/mysql-virtual-alias

...

query = SELECT destination FROM virtual_aliases WHERE source='%s'

You can test your mapping with the following command. This should output 1 as a result:

$ postmap -q bob@example.org mysql:/etc/postfix/mysql-virtual-maps

Finally, restart the Postfix daemon.

Web console for virtual mailbox administration

The Vimbadmin package provides a web console for virtual mailbox administration. It is a PHP-based open source package. You can get source code and installation instructions at https://github.com/opensolutions/ViMbAdmin .

See also

Postfix guide at http://www.postfix.org/VIRTUAL_README.html

Postfix lookup table types at http://www.postfix.org/DATABASE_README.html#types

Installing Mattermost – a self-hosted slack alternative

This recipe covers another open source collaboration tool, Mattermost. Mattermost is a modern communication tool that includes one-to-one chat, group chat IRC-like channels, file sharing, and a super-fast search functionality. It can be thought of as a modern IRC tool. Mattermost is well known as an open source Slack alternative, but the Mattermost website says it is not limited to being a Slack alternative. You can find a list of features at http://www.mattermost.org/features .

The GitHub repository contains a step-by-step guide for installing Mattermost on production servers. We will use the same guide as our base.

Getting ready

You will need a 64-bit Ubuntu server and access to an account with sudo privileges. Mattermost prebuilt binaries are available only on a 64-bit platform. If you are running 32-bit Ubuntu, you will need to compile Mattermost from source. We will use MySQL as a database for Mattermost. I will use the same server for the database and Mattermost. You may want to separate these services on two different servers for better performance.

Create a separate MySQL user account and database for Mattermost. I will use the lowercase mattermost as a database name as well as a username.

Additionally, you will need a proxy if you are planning to load balance multiple Mattermost instances or have a secure setup with SSL enabled.

You will need a separate storage directory for shared multimedia contents. You should use a separate large volume specifically assigned for this purpose. Make sure that the directory is owned by the current user. To keep things simple, I will use a data directory under the current user's home, that is, /home/ubuntu/mattermost-data.

How to do it…

Mattermost is based on Golang as a backend and React, a JavaScript framework, for the frontend. Golang is capable of creating self-sufficient independent binaries. We will download the prebuilt package available on GitHub. As of writing this, the latest stable version is 1.3.0:

Download the Mattermost archive with the following command:

$ wget https://github.com/mattermost/platform/releases/download/v1.3. 0/mattermost.tar.gz

Extract content from the archive. This will create a new directory named mattermost:

$ tar -xf mattermost.tar.gz

Next, edit the Mattermost configuration file located under the config directory:

$ cd mattermost

$ vi config/config.json

It is already configured to use MySQL as a data source. We need to set our username and password details for the database. Search for the SqlSettings section and replace the content of the DataSource parameter with the following line:

"DataSource": "mattermost: password@tcp(localhost:3306)/mattermost?charset=utf8mb4,utf 8"

Next, search for the FileSettings section and set the Directory parameter to the directory we created for multimedia content:

"Directory":"/home/ubuntu/mattermost-data/"

Now, run the Mattermost server with the following command, and wait for the server to start listening:

$./bin/platform

Now you can access the Mattermost service at the hostname of your server at http://server_ip_or_host:8065 . However, the service is still running from the console and will be terminated when we close the terminal.

Terminate this process by pressing Ctrl + C and set a startup daemon so that we can start Mattermost in the backend and automatically start the service on system reboot.

Create a new upstart configuration under the /etc/init directory:

$ sudo nano /etc/init/mattermost.conf

Add the following content to the newly created file:

start on runlevel [2345]

stop on runlevel [016]

respawn

chdir /home/ubuntu/mattermost

setuid ubuntu

exec bin/platform

Now you can start Mattermost with any of the following commands:

$ sudo start mattermost

Or

$ sudo service mattermost start

Optionally, if you want to load balance the Mattermost service using Nginx or HAProxy in front of it, please refer to article 3Working with Web Servers, for detail on how to do so. The use of a load balancer will also give you an option to enable SSL security for all communication.

Once you start the Mattermost service and access the homepage, you will be asked to sign up. Create an account with an email address and you can start using your own Mattermost instance. You can access the server at http://yourserver:8065.

How it works…

Mattermost is all about team communication and collaboration. When you access the Mattermost server for the first time and sign up with your email address, you will get an option to create a new team or join existing teams.:

To join an existing team, you need to submit your email address and Mattermost will reply with links to the team page where you are a member. If you have not yet created a team, simply proceed with signup. On signup, after you have entered your email address, you will be asked to select a team name and URI or a web address for your team page. Enter a good name for your team and click Next:

On the next page, you will be asked to choose a URL for your team page. The box should be pre-filled with a suggested URL. Feel free to change it if you have a better idea:

Once you are done with signup, you will be greeted with a welcome message and a simple walkthrough of the Mattermost service. Once you are done with the introduction, you will land on the Town Square channel. This is a prebuilt public channel accessible to all users. There's one more prebuilt channel named Off-Topic listed on the left side menu. You can create your own public channel, create a Private Group, or have a one-to-one chat through Direct Messages.

Before you start using the service, invite some more users to your team. Click on the Invite others to this team link or click on your username at the top left and then select the Invite New Member link. Here, you can enter the email and name of a single member to invite them. Optionally, you can get a team invite link, which can be shared with a group:

The username menu on the left gives you some more options. You can update team settings, manage team members, and even create a new team altogether. You will need to be a team admin to access these options. If you are part of multiple teams, then you can see an option to switch to a different team.

The team members will receive all communication in public channels. A user can decide to be a part of a channel or leave it and not receive any communication from a specific channel. Other options are Private group and Direct messages. In private groups, you can communicate and share with selected people and not the entire team, whereas in a direct message, as the name suggests, it is a one-to-one chat.

Working with Ubuntu Web Servers

In this article, we will cover the following recipes:

  • Installing and configuring the Apache web server
  • Serving dynamic contents with PHP
  • Hosting multiple websites with a virtual domain
  • Securing web traffic with HTTPS
  • Installing Nginx with PHP_FPM
  • Setting Nginx as a reverse proxy
  • Load balancing with Nginx
  • Setting HTTPs on Nginx
  • Benchmarking and performance tuning of Apache
  • Securing the web server
  • Troubleshooting the web server

Chat server with Node.js on Ubuntu

Up to now, this article has covered XMPP and its usages. It is a good, mature protocol with multiple servers developed around it. Sometimes, however, you may need to set up a quick application that uses a simple message transfer, or develop a small chat application for your team. For such projects, XMPP servers may turn out to be overkill. You may not use all the features of XMPP and waste resources, even for a basic setup. Plus, developing an XMPP application is a time consuming process.

In this case, you can quickly start using Node.js-based socket communication. Node.js has gained popularity in the developer community. It is a framework developed in a commonly known language, JavaScript. In this recipe, we will learn how to develop a message passing application using Node.js sockets. We will use Socket.io, a popular Node.js library, to work with sockets and a demo app provided by Socket.io.

Getting ready

You will need access to a root account or an account with sudo privileges.

How to do it…

We are going to set up a Node.js-based application, so we need to install Node.js on our Ubuntu server.

Follow these steps to install Node.js:

Install Node.js with the following command:

$ sudo apt-get update

$ sudo apt-get install nodejs

Optionally, check your Node.js version:

$ node -v

Next, download the sample application from the Socket.io GitHub repo:

$ wget https://github.com/rauchg/chat- example/archive/master.zip

Unzip the downloaded contents. This will create a new directory named chat-sample-master:

$ unzip master.zip

Change the path to the newly created directory:

$ cd chat-sample-master

Next, we will need to install the dependencies for this sample application. Use the following Node.js command to install all dependencies.

$ npm install

This will fetch all dependencies and install them in the node_modules directory under chat-sample-master. Once the install command completes, you can start your application with the following command:

$ node index.js

ubuntu: ~/chat-example-master $ node index.js listening on *:3000

This will start an inbuilt HTTP server and set it to listen on default port 3000. Now you can access the app at http://server-ip:3000. The screen will look similar to the following image:

Open another instance in a separate browser window and start sending your messages.

How it works…

We have set up a very simple application that listens on a given Node.js socket. To send a message, we have used the socket.emit() function, which writes the data from text box to socket:

$('form').submit(function(){

socket.emit('chat message', $('#m').val());

...

});

When this message is received on the server side, the server writes it to all connected sockets, resulting in a group chat scenario:

io.on('connection', function(socket){

socket.on('chat message', function(msg){

io.emit('chat message', msg);

});

});

Similarly, to receive a message, we keep listening on the socket, and when an event chat message happens, we write the received data to an HTML page as a message:

socket.on('chat message', function(msg){

$('#messages').append($('

  • ').text(msg));
  • });

    This is very basic application and can be extended easily to implement one-on-one chat. All we need is a unique ID for all clients and a little modification to the interface to separate messages. Right now, the message is sent as it is; you can collect the message and create a JSON object to contain sender and receiver IDs, plus any additional information.

    The advantage of using NodeJS is quick and easy development. JavaScript is a commonly used language and you can easily get support from the large community. You can always develop the application as per your requirements. The disadvantage is regarding scaling; you will need to code the clustering mechanism on your own, whereas for XMPP, clustering is implemented by nearly all leading servers.

    There's more…

    The Node.js setup available with the Ubuntu repository is not the latest one. You can download the latest version from the node official download page.

    Download NodeJS binaries for Linux. Choose your desired version by visiting the NodeJS download page. As of writing this, the latest stable version is 5.1:

    $ wget https://nodejs.org/download/release/v5.1.0/node-v5.1.0-linux- x64.tar.xz

    Extract binaries and move it to /use/local so that it is accessible globally:

    $ tar Jxv --strip=1 -C /usr/local/

    Check the node version with the following command:

    $ node -v

    See also

    Node.js download page: https://nodejs.org/en/download

    Node: how to install: https://github.com/nodejs/help/issues/41

    Sample chat application on GitHub: https://github.com/rauchg/chat-example

    How to Set up public key authentication in Ubuntu

    In this recipe, you will see how to set up secure public key authentication.

    Getting ready

    You might need root privileges for certain tasks.

    How to do it...

    Follow these steps to set up public key authentication:

    Add a new user. You can skip this step if you have already created a user:

    $sudo adduser john

    Log in as john and change to the home directory with cd ~/:

    Create a .ssh directory if it doesn't already exist:

    $ mkdir .ssh

    Create a file named authorized_keys under the .ssh directory:

    $ touch .ssh/authorized_keys

    Set permissions on the .ssh directory to 700:

    $chmod 700 .ssh

    Set permissions for authorized_keys to 600:

    $ chmod 600 .ssh/authorized_keys

    Generate public key pair on your local system with the following command:

    $ ssh-keygen

    Copy the generated public key from the .ssh/id_rsa.pub file to the authorized_keys file on the server.

    Now, open an ssh connection from local to server with the following command:

    $ ssh john@server

    If asked for confirmation, type yes and press the Enter key to continue:

    How it works…

    Logging in with SSH supports different authentication methods. Public key authentication and password-based authentication are two common methods. To log in with public key authentication, we need a public private key pair. We generate this key pair with the ssh-keygen command. This command creates two files under the .ssh directory in the user's home:

    id_rsa: This is the private key file

    id_rsa.pub: This is the public key file

    You can view the contents of the files with $cat id_rsa.pub. It should start with something like ssh-rsa AAAA...(except for the trailing dots).

    We then copy the contents of public key to the server's authorized_keys file. Ensure that all contents are listed on single line in the authorized_keys file.

    Also, ensure the permissions are properly set for the .ssh directory, and ensure that the authorized_keys file and directory are owned by the user. The permissions for the .ssh directory limits read, write, and execute permissions to the owner of the file. Similarly, for authorized_keys file, permissions are limited to read and write for owner only. This ensures that no other user can modify the data in the .ssh directory. If these permissions are not properly set, the SSH daemon will raise the warning Permission denied?.

    Working of SSH authentication

    When the SSH client initiates a connection with the server, the server sends public key identification of server to client. If a client is connecting to the server for the first time, it shows a warning and asks for user confirmation to store the server key in the known_hosts file under the .shh directory. After receiving the identity, the client authenticates server to ensure that it is really the intended server.

    After server authentication, the server sends a list of possible authentication methods. The client selects the authentication method and selection to the server. After receiving the authentication method, the server sends a challenge string encrypted with client's private key. The client has to decrypt this string and send it back to server along with previously shared session key. If the response from the client matches the response generated by the server, then client authentication succeeds.

    There's more…

    You might be searching for a secure option to install key on server. Here's one way!

    If your local system has the ssh-copy-id tool installed, you can directly add your public key to the server's authorized_keys file with a single command:

    $ ssh-copy-id john@serverdomain

    After providing the password, your local public key will be added to the authorized_keys file under the .ssh directory of the user john.

    Troubleshooting SSH connections

    Most of the connection issues are related with configuration problems. If you happen to face any such issue, read the error message in detail. It is descriptive enough to understand the mistake. You can also go through following checklist:

    Check if the SSH daemon is running. Check the port in use and port conflicts, if any

    Check whether the firewall configuration allows SSH ports

    Check the list of configuration methods that are enabled

    Check permissions for your private keys on your local system

    Check authorized_keys file for your public key on the server

    Check for any entry with the old address of the server in known_hosts on the local system

    Additionally, you can use the verbose flag (-v or -vvv) with the ssh command to get details of every step taken by the SSH client. Also, check SSH daemon logs on server.

    SSH tools for the Windows platform

    If your local system runs Windows, then you can use tools provided by puTTYto generate new keys and connect to the server:

    putty.exe: This is the SSH client on Windows

    puttygen.exe: This tool generates public or private keys

    pscp.exe: This is the SCP client for secure file transfer

    When using public key generated by the puttygen.exe tool, make sure that you convert the key to OpenSSH key format. Remove all comments and prepend ssh-rsa. Additionally, the entire key should be listed on a single line.

    Another easy option is to use puttygen.exe. Load your private key in PuTTYgen and then copy the public key from the Key section of the PuTTYgen window.

    See also

    For more information on the full working of SSH authentication, visit http://www.slashroot.in/secure-shell-how-does-ssh-work

    Understanding Docker volumes in Ubuntu

    One of the most common questions seen on Docker forums is how to separate data from containers. This is because any data created inside containers is lost when the container gets deleted. Using docker commit to store data inside Docker images is not a good idea. To solve this problem, Docker provides an option called data volumes. Data volumes are special shared directories that can be used by one or more Docker containers. These volumes persist even when the container is deleted. These directories are created on the host file system, usually under the /var/lib/docker/ directory.

    In this recipe, we will learn to use Docker volumes, share host directories with Docker containers, and learn basic backup and restore tricks that can be used with containers.

    Getting ready

    Make sure that you have the Docker daemon installed and running. We will need two or more containers.

    You may need sudo privileges to access the /var/lib/docker directory.

    How to do it…

    Follow these steps to understand Docker volumes:

    To add a data volume to a container, use the -v flag with the docker run command, like so:

    $ docker run -dP -v /var/lib/mysql --name mysql\

    -e MYSQL_ROOT_PASSWORD= passwdmysql:latest

    This will create a new MySQL container with a volume created at /var/lib/mysql inside the container. If the directory already exists on the volume path, the volume will overlay the directory contents.

    Once the container has been started, you can get the host-specific path of the volume with the docker inspect command. Look for the Mounts section in the output of docker inspect:

    $ docker inspect mysql

    To mount a specific directory from the host system as a data volume, use the following syntax:

    $ mkdir ~/mkdir

    $ docker run -dP -v ~/mysql:/var/lib/mysql \

    --name mysql mysql:latest

    This will create a new directory named mysql at the home path and mount it as a volume inside a container at /var/lib/mysql.

    To share a volume between multiple containers, you can use named volume containers.

    First, create a container with a volume attached to it. The following command will create a container with its name set to mysql:

    $ docker run -dP -v /var/lib/mysql --name mysql\

    -e MYSQL_ROOT_PASSWORD= passwd mysql:latest

    Now, create a new container using the volume exposed by the mysql container and list all the files available in the container:

    $ docker run --rm --volumes-from mysql ubuntu ls -l /var/lib/mysql

    To back up data from the mysql container, use the following command:

    $ docker run --rm--volumes-from mysql -v ~/backup:/backup \

    $ tar cvf /backup/mysql.tar /var/lib/mysql

    Docker volumes are not deleted when containers are removed. To delete volumes along with a container, you need to use the -v flag with the docker rm command:

    $ dockerrm -v mysql

    How it works…

    Docker volumes are designed to provide persistent storage, separate from the containers' life cycles. Even if the container gets deleted, the volume still persists unless it's explicitly specified to delete the volume with the container. Volumes can be attached while creating a container using the docker create or docker run commands. Both commands support the -v flag, which accepts volume arguments. You can add multiple volumes by repeatedly using the volume flag. Volumes can also be created in a Dockerfile using the VOLUME instruction.

    When the -v flag is followed by a simple directory path, Docker creates a new directory inside a container as a data volume. This data volume will be mapped to a directory on the host filesystem under the /var/lib/docker directory. Docker volumes are read-write enabled by default, but you can mark a volume to be read-only using the following syntax:

    $ docker run -dP -v /var/lib/mysql:ro --name mysql mysql:latest

    Once a container has been created, you can get the details of all the volumes used by it, as well as its host-specific path, with the docker inspect command. The Mounts section from the output of docker inspect lists all volumes with their respective names and paths on the host system and path inside a container.

    Rather than using a random location as a data volume, you can also specify a particular directory on the host to be used as a data volume. Add a host directory along with the volume argument, and Docker will map the volume to that directory:

    $ docker run -dP -v ~/mysql:/var/lib/mysql \

    --name mysql mysql:latest

    In this case, /var/lib/mysql from the container will be mapped to the mysql directory located at the user's home address.

    Need to share a single file from a host system with a container? Sure, Docker supports that too. Use docker run -v and specify the file source on the host and destination inside the container. Check out following example command:

    $ docker run --rmd -v ~/.bash_history:/.bash_history ubuntu

    The other option is to create a named data volume container or data-only container. You can create a named container with attached volumes and then use those volumes inside other containers using the docker run --volumes-from command. The data volumes container need not be running to access volumes attached to it. These volumes can be shared by multiple containers, plus you can create temporary, throwaway application containers by separating persistent data storage. Even if you delete a temporary container using a named volume, your data is still safe with a volume container.

    From Docker version 1.9 onwards, a separate command, docker volume, is available to manage volumes. With this update, you can create and manage volumes separately from containers. Docker volumes support various backend drivers, including AUFS, OverlayFS, BtrFS, and ZFS. A simple command to create a new volume will be as follows:

    $ docker volume create --name=myvolume

    $ docker run -v myvolume:/opt alpine sh

    See also

    The Docker volumes guide: http://docs.docker.com/engine/userguide/dockervolumes/

    Clean up orphaned volumes with this script: https://github.com/chadoe/docker-cleanup-volumes

    Ubuntu

    Ubuntu Linux can be defined in many ways and from different angles. First off, it is an operating system (usually shortened to OS). Ubuntu is a distribution of Linux, based on Debian, and that gives it some characteristic features. But to describe it only as an OS would be nothing short of unfair: it also has a wide range of pre-installed applications and many more readily available at the click of the mouse, and an ever-growing user community. Ubuntu Server is an extremely powerful distribution for servers and network appliances. Whether you're setting up a high-end database host or a small office file server, the flexible nature of Ubuntu Server will meet and surpass your needs.