# How to install and use docker on Ubuntu 16.04
Ref.: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-
ubuntu-16-04

Docker is an application that makes it simple and easy to run application processes in a
container, which are like virtual machines, only more portable, more resource-friendly,
and more dependent on the host operating system. For a detailed introduction to the 
different components of a Docker container, check out "The Docker Ecosystem: An Introdu-
-ction to Common Components".

Step 1: Installing Docker (from official docker repository)

First, in order to ensure the downloads are valid, add the GPG key for the official
docker repository to your system:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Add the docker repository to APT sources:
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu
  $(lsb_release -cs) stable"

Next, update the package database with the docker packages from the newly added repo:
$ sudo apt update

Make sure you are about to install from the Docker repo instead of the default 
Ubuntu 16.04 repo:
$ apt-cache policy docker-ce

Note that docker-ce is not installed, but the candidate for installation is from the 
docker repository for Ubuntu 16.04 (xenial).

Finally, install docker:
$ sudo apt install -y docker-ce

Docker should now be installed, the daemon started, and the process enabled to start on
boot. Check that it's running:
$ sudo systemctl status docker

Step 2: Executing the Docker command without sudo (optional)

By default, running the docker command requires root privileges - that is, you have to
prefix the command with sudo. It can also be run by a user in the docker group, which is
automatically created during the installation of docker. 

If you want to avoid typing sudo whenever you run the docker command, add your username
to the docker group:
$ sudo usermod -aG docker ${USER}

To apply the new group membership, you can log out of the server and back in, or you can
type the following:
$ su - ${USER}

You will be prompted to enter your user's password to continue. Afterwords, you can
confirm that your user is now added to the docker group by typing:
$ id -nG

If you need to add a user to the docker group that you're not logged in as, declare that
username explicity using:
$ sudo usermod -aG docker username

Note: The rest of this article assumes you are running the docker command as a user in
the docker user group. If you choose not to, please prepend the commands with sudo.

Step 3: Using the docker command

With docker installed and working, now's the time to become familiar with the command
line utility. Using docker consists of passing it a chain of options and commands
followed by arguments. The syntax takes this form:
$ docker [option] [command] [arguments]

To view all available subcommands, type:
$ docker

To view the switches available to a specific command, type:
$ docker docker-subcommand --help

To view system-wide information about docker, use:
$ docker info

Step 4: Working with docker images

Docker containers are run from docker images. By default, it pulls these images from 
docker hub, a docker registry managed by Docker.

To check whether you can access and download images from docker hub, type:
$ docker run hello-world

Output:
...
Hello from Docker!
This message shows that your installation appears to be working correctly.
...

You can search for images available on docker hub by using the docker command with the
search subcommand. For example, to search for the Ubuntu image, type:
$ docker search ubutnu

In the offical column, OK indicates an image built and supported by the company behind
the project. Once you've identified the image that you would like to use, you can down-
-load it to your computer using the pull subcommand. Try this with the ubuntu image like
so:
$ docker pull ubuntu

After an image has been downloaded, you may then run a container using the downloaded
image with the run subcommand. If an image has not been downloaded when docker is
executed with the run subcommand, the Docker client will first download the image, then
run a container using it:
$ docker run ubuntu

To see the images that have been downloaded to your computer type:
$ docker images

Step 5: Running a Docker container

The hello-world container you ran in the previous step is an example of container that 
runs and exits after emitting a text message. Containers can be much more useful than
that, and they can be interactive. After all, they are similar to virtual machines, only
more resource-friendly.

As an example, let's run a container using the latest image of Ubuntu. The combination 
of the -i and -t switches gives you interactive shell access into the container:
$ docker run -it ubuntu

Note: The default behavior for the run command is to start a new container. Once you run
the preceding the command, you will open up the shell interface of a second Ubuntu
container.

Your command prompt should change to reflect the fact that you're now working inside the
container and should take this form:

outpur:
root@9b0db8a30ad1:/#

Note: 9b0db8a30ad1 is the container id. You'll need that container id later to identify
the container when you want to remove it.

Now you can run any command inside the container. For example, let's update the package
database inside the container. You don't need to prefix any command with sudo, because
you're operating inside the container as the root user:

root@9b0db8a30ad1:/# apt update

Then install any application in it. Let's install Node.js:

root@9b0db8a30ad1:/# apt install -y nodejs

This installs Node.js in the container from the offical Ubuntu repository. When the
installation finishes, verfiy that Node.js is installed:

root@9b0db8a30ad1:/# node -v

You'll see the version number displayed in your terminal:

output:
v8.10.0

Any changes you make inside the container only apply to that container. To exit the 
container, type exit at the prompt. 

Step 6: Managing Docker containers

After using Docker for a while, you'll have many active (running) and inactive container
on your computer. To view the active ones, use:
$ docker ps

To view all containers - active and inactive - run docker ps with the -a switch:
$ docker ps -a

To view the latest container you created, pass it the -l switch:
$ docker ps -l

To start a stopped container, use docker start, followed by the container ID or the
container's name. Let's start the Ubuntu-based container with the ID of 9b0db8a30adl:
$ docker start 9b0db8a30adl

The container will start, and you can use docker ps to see its status:
$ docker ps

To stop a running container, use docker stop, followed by the container ID or name. This
time, we'll use the name that Docker assigned the container, which is 
xenodochial_neumann:
$ docker stop xenodochial_neumann

Onece you've decided you no longer need a container anymore, remove it with the docker
rm command, again using either the container ID or the name. Use the docker ps -a 
command to find the container ID or name for the container associated with the 
hello-world image and remote it:
$ docker rm youthful_roentgen

You can start a new container and give it a name using the --name switch. You can also
use the --rm switch to create a container that removes itself when it's stopped. See the
docker run help command for more information on these options and others.

Step 7: Committing changes in a container to a docker image

When you start up a docker image, you can create, modify, and delete files just like you
can with a virtual machine. The changes that you make will only apply to that container.
You can start and stop it, but once you destory it with the docker rm command, the 
changes will be lost for good.



# mysql installation in docker
Ref: https://dev.mysql.com/doc/refman/8.0/en/docker-mysql-getting-started.html

sudo docker search mysql
sudo docker pull mysql/mysql-server
sudo docker images
sudo docker rmi <imagename>
docker help | less -N
-----
sudo docker run -d mysql/mysql-server
sudo docker ps
sudo docker logs <container_name> 2>&1 | grep GENERATED
sudo docker exec -it <container_name> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
mysql> show databases;
mysql> exit
sudo docker rm <container_name>
-----

sudo docker images
sudo docker ps
sudo docker ps -a
sudo docker start <containerID>
sudo docker exec -it <containername> mysql -uroot -p
sudo docker stop <containerID>
-----

# Docker Administration
docker ps
docker ps -a
docker rm <NAMES>

docker start <containerID>/<NAMES>
docker exec -it <containerID>/<NAMES> bash
docker stop <containerID>/<NAMES>

# copying files from host to docker container
docker cp file.txt <containerID>:/home/user/path/

# remove
docker rm <NAME>

# rename
docker rename <currentName> <newName>

# Information regarding docker images
docker info

# Inspect docker image
docker image inspect --format '{{json .}}' "$IMAGE_ID"
docker image inspect debian | less -N
docker inspect debian | less -N

# Find OS running in Docker container
lsb_release -sirc
cat /etc/os-release
cat /etc/issue

# change user
su - raman      # msqdb 


# rpm commands

# list all installed packages
rpm -qa
rpm -qa | grep httpd
rpm -qa --last
yum list installed
sudo -su raman
exit

# ip address
yum install iproute2 (ip addr)   # not working in dbase
yum install net-tools (ifconfig) # working in dbase

# wget
yum install wget

# systemd
wget https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py -O /usr/local/bin/systemctl
chmod u+x /usr/local/bin/systemctl
/usr/local/bin/systemctl status sshd
/usr/local/bin/systemctl start sshd

yum remove openssh-server
yum autoremove openssh-server


# init.d
sudo /etc/init.d/ssh restart

# configuration files for ssh
/etc/ssh/

# to check sshd started when boot
ps aux | grep sshd


docker run -it -p 1001:25 --postfix1 --privileged -d --cap-add=SYS_ADMIN -v /sys/fs/cgroup:/sys/fs/cgroup:ro postfix
docker run -d -name centos7 --privileged=true centos:7 /usr/sbin/init

#
docker container diff <container-id>/<name>
