網頁

顯示具有 技術---Docker 標籤的文章。 顯示所有文章
顯示具有 技術---Docker 標籤的文章。 顯示所有文章

2017年2月28日 星期二

Docker 1.13.0 --- How to add "restart" policy to the initiating Container if never set it

Since docker 1.11.0 or above version, we can change the restart policy if an initiating container forget to set it.
docker update --restart [Option] [Container ID]
where
        --restart Restart policy to apply when a container exits
 [Option]  
   no         Do not automatically restart the container when it exits. This is the default.
   on-failure[:max-retries] Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts.
    always          Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.
    unless-stopped Always restart the container regardless of the exit status, but do not start it on daemon startup if the container has been put to a stopped state before.

     Note that if the container is started with “–rm” flag, you cannot update the restart policy for it. The AutoRemove and RestartPolicy are mutually exclusive for the container.

As to old version as 1.11.0 before, we can rename the orginial container, start a temp container (but attach the existing volumes of the orginial container), and remove the temp container now that it's no longer needed.
$ docker stop my-old-container
$ docker rename my-old-container my-temp-container
$ docker run  ...  --volumes-from=my-temp-container --restart always myimage
$ docker rm my-temp-container

 To restart the Docker daemon and we can find out the container will start automatically now.


2017年2月25日 星期六

Docker Machine 0.9.0 --- Docker Machine install on the local client and manage Docker Engine on the remote host

Docker Machine is a tool that lets we install Docker Engine on virtual hosts and manager the hosts with docker-machine command.

By docker-machine command, we can start, inspect, stop and restart a managed host, upgrade the Docker client and daemon, and configure a Docker client to talk to the host next to run docker commands directly on this host.

In the moment, we will demo how to practice that docker machine manage the remote host.

Prerequisites
docker host never install a docker engine on the remote machine (# 1)
docker client   install docker client and docker machine on the local machine(as laptop)
where
OS in the remote need to be CentOS 7, Ubuntu 16.04 or above version
The net-tools package (# 2) need to be installed on the remote machine

[Remote Machine]
To create a docker manager account with new password (# 3)
$ sudo adduser {dockermanager}

To allow sudo for docker manager
In the remote machine, we have to edit the /etc/sudoers file and add the following content

                          {dockermanager}  ALL=(ALL) NOPASSWD:ALL
where
        {dockermanager} is the username on the remote machine
So do that Machine communicates with the hosts over password-less SSH

To check whether ssh daemon is running and is listening on port 22
sudo service ssh status
sudo netstat -anp | grep sshd
If it don't show the listening on tcp 0.0.0.0:22, we need to install openssh server
sudo apt-get update
sudo apt-get install openssh-server



[Local Machine]
To generate a public/private keys with empty passphrase (# 4)
$ ssh-keygen -t rsa
where
id_rsa.pub (RSA public key for authentication) file is the public key
id_rsa (RSA authentication identity of the user) file is the private key
In bash shell, the SSH key will be saved on ~/.ssh on linux platform

To copy a public key from local machine to a remote machine
$ ssh-copy-id -i ~/.ssh/id_rsa.pub {dockermanager}@{docker host}
where
{dockermanager} is the username on the remote machine
{docker host} is the ip address or host name of the remote machine

To connect the remote machine by using the SSH key
$ ssh {dockermanager}@{docker host}

To install Docker Client Tool
Until now, there is no docker client-only debs/rpms binary so that we need to do something for getting the individual binary. That is download the docker latest release next to extract it and execute docker command directly.


To install Docker Machine (# 5)
$ curl -L https://github.com/docker/machine/releases/download/v0.9.0/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine

$ chmod +x /tmp/docker-machine

$ sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

To check docker machine version
$ docker-machine version

To create the remote docker-vm using an existing VM/Host with SSH
$ docker-machine create \
 --driver generic \
 --generic-ip-address={remote docker host ip} \
 --generic-ssh-key ~/.ssh/id_rsa \
 --generic-ssh-user {dockermanager} \
 --generic-ssh-port 22 \
 --generic-engine-port 2376 \
{remote-docker-host}

To check which docker engine is active now
$ docker-machine ls

To print out the detail about the docker client to use the remote docker engine
$ docker-machine env {remote-docker-host}

To connect your shell to the new machine
$ eval $(docker-machine env {remote-docker-host})

To create a docker container on the remote docker host
$ docker run -d -p 8080:80 --name httpserver nginx
so do that any docker command you type at this command prompt will be executed on that remote host

Reference:
(# 1) How to prove that docker engine never is installed on this computer? To switch to the command path next to run $./docker ps command
(# 2) Installing net-tools package on Ubuntu 16.04 (Xenial Xerus) by running the following command on terminal
         $ sudo apt-get update
         $ sudo apt-get install net-tools
(# 3) We can change the account's password by running the following command on terminal
         $ sudo passwd {dockermanager}
(# 4)  If have already set a password for SSH passphrase, we have to change it by the following command
          To change directory to $HOME/.ssh         $ cd ~/.ssh/
          To change RSA passphrase                       $ ssh-keygen -f id_rsa -p
(# 5)  We can download the release version of docker machine from docker/machine release page.
(# 6)  Install Docker Machine https://docs.docker.com/machine/install-machine/
          Machine Driver Generic https://docs.docker.com/machine/drivers/generic/

2017年1月30日 星期一

Docker 1.13.0 --- How to install Docker on Lubuntu 16.10 x64

For a long time no go deep into the Docker's Knowledge!
Due to the job rotation, I have to spend some time to mange team member so that no time to write down something. In the duration of the Chinese holiday, I go to the Docker's Official Website to read the related document(# 1) and find out the some knowledge is a little different with before learning.
I will follow up the Docker's document to re-learn the installation step. For avoiding the forget memory in my mind, I will summary something as follows:

To remove the old Docker component if we ever install Docker or the system build in
sudo apt-get purge lxc-docker

To install extra packages for allowing Docker to use the aufs storage drivers
$ sudo apt update
$ sudo apt install curl linux-image-extra-$(uname -r) linux-image-extra-virtual

To install Docker by using the Docker's repositories
For setting up the Docker repository, we need to install some packages to allow apt to use a repository over HTTPS
$ sudo apt install apt-transport-https ca-certificates
and add Docker's official GPG key
curl -fsSL https://yum.dockerproject.org/gpg | sudo apt-key add -

and add the record of the stable repository into the /etc/apt/sources.list
$ sudo add-apt-repository "deb https://apt.dockerproject.org/repo/ ubuntu-$(lsb_release -cs) main"

To update the apt package index
$ sudo apt update

To verify the apt pull from the right repository
$ apt-cache policy docker-engine

To install the latest version of Docker(# 2)
sudo apt install docker-engine

To check Docker is running
sudo systemctl status docker

To verify the Docker is installed correctly
$ sudo docker run hello-world


Reference
(# 1). Get Docker for Ubuntu
(# 2). If we want to install a specific version of Docker Engine, not the latest version, we can use apt-cache madison command to list all available version.
apt-cache madison docker-engine
next to choose the the appropriate version
$ sudo apt-get install docker-engine=1.13.0-0~ubuntu-yakkety
(# 3). If we want to manage Docker as a non-root user, we need to create the docker group and add the specific user into this group.
To create the docker group
$ sudo groupadd docker

To add a logon user to the docker group
$ sudo usermod -aG docker $USER
Log out and log back again so that we can use docker command without sudo now.

2015年9月27日 星期日

Docker 1.7.1 --- How to change the docker image installation directory in Ubuntu/Lubuntu 15.04

In general, the Docker will put all the data including images under /var/lib/docker after install the Docker package. It is easy to get the space problem so that need to move the docker default location to new place.  That is to mount docker default location /var/lib/docker to new mount point.
How to do it?

At first, we will prepare the new block storage for planning the moved docker folder
  1. Add a new disk about 20GB
# sudo fdisk -l
AS-IS
TO-BE
  1. Create a new partition by using the sudo fdisk /dev/sd?  
  1. with g option for creating a new empty GPT partition table
  1. with n option for adding a new partition
  1. with p option for printing the partition table and with w option for writing table to disk and exit

  1. Format the new block storage device with ext4 file system
# sudo mkfs.ext4 /dev/sd??

  1. Mount the new block storage device
    1. create a mount point
# sudo mkdir -p /mnt/diskX
    1. mount the file or do equivalent mount in /etc/fstab
# sudo mount -o bind /dev/sd?? /mnt/diskX
    1. create a new folder to house docker
# sudo mkdir /mnt/diskX/docker
# sudo chmod 700 /mnt/diskX/docker

When the new space is ready and mounted, we will move the docker image from the the original path to the new path.
How to do it?
  1. In Ubuntu/Lubuntu 15.04, check out system-wide information
# sudo docker info

  1. In Ubuntu/Lubuntu 15.04, stop containers and docker daemon
# sudo docker ps -q | xargs docker kill

# sudo service docker stop

  1. In Ubuntu/Lubuntu 15.04, if it exists /var/lib/docker/aufs/mnt
# sudo -s
# cd /var/lib/docker/aufs/mnt
# umount ./*
# exit

  1. In Ubuntu/Lubuntu 15.04, move /var/lib/docker to the new mounted storage device
# sudo -s
# mv /var/lib/docker/* /mnt/diskX/docker/
# exit

  1. In Ubuntu/Lubuntu 15.04, set the flag in /etc/default/docker & /etc/init.d/docker to
DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 -g /mnt/diskX/docker"
/etc/default/docker

/etc/init.d/docker

  1. In Ubuntu/Lubuntu 15.04, bind mount instead of linking
# sudo mount -o bind /mnt/diskX/docker /var/lib/docker

  1. In Ubuntu/Lubuntu 15.04, periste on restart about mount new disk and bind mount by editing /etc/fstab file and adding the following content:
UUID /mnt/diskX ext4 defaults 0 0
/mnt/diskX/docker /var/lib/docker bind defaults,bind 0 0

  1. In Ubuntu/Lubuntu 15.04, restart docker daemon by using the sudo service docker start command

  1. test that when you create a container the hash appears in /mnt/diskX/docker/containers
After finishing the above step and starting a container, the available space will be extended now.
AS-IS
TO-BE


Since 2010 Design by Davidwa
©Copyright Davidwa Inc. All rights reserved.