Senin, 06 November 2023

DOCKER IMAGES-membuat images docker cent6

 

http://iamdevops.blogspot.com/2015/05/docker-creating-custom-centos-65-docker.html

Agenda

- To create a custom CentOS 6.5 Docker Container Image to spawn of different containers.

Installation Pre-Requisite

- Virtualization Software like Virtual Box or VMWare Workstation.
- CentOS 6.5 Minimal ISO Image.
- Internet

Steps to create container

Step 1 : Setting up the Virtual Machine

- With the help of the download CentOS 6.5 image, setup an virtual machine using one of the virtualization software.

- RAM : 2 GB
- HDD : 30 GB
- HDD File Format : VMDK
- Storage Structure : Dynamically Allocated
- Network Adapter : Bridged Mode


Step 2 : Edit the network template file and install EPEL repo

Once the installation is successful, boot into the virtual machine.

Edit the following file using the following command.

vim  /etc/sysconfig/network-scripts/ifconfig-eth0

- Remove the following lines from that file and set the ONBOOT to yes.

-- Remove the following lines --
HWADDR
UUID
--------------------------------

-- Edit the below line and set the value to yes --
ONBOOT=yes
--------------------------------------------------

- Save the file and exit.
- Issue the following command to bring up the interface.

ifup eth0 (or) service network start

- Download the EPEL repo using the following command.

wget http://mirror-fpt-telecom.fpt.net/fedora/epel/6/i386/epel-release-6-  
8.noarch.rpm

- Install the EPEL repo using either one of the below commands.

rpm -ivh epel-release-6-8.noarch.rpm 
                 - or -
  yum install epel-release-6-8.noarch.rpm

- Enable the CentOS Plus repository in the CentOS-Base.repo file. 


Step 3 : Install Docker

- Install Docker using the below command.

yum -y install docker-io

Step 4 : Start the Docker service

- Start the Docker service using the below command.

service docker start

- Use the below command to view the available docker images.
docker images


- Use the below command to see the entire process list for docker.
docker ps -all

Step 3 : Get the image creator script, create and save the container 

- Issue the following commands.

- cd /tmp/
- mkdir scripts
- cd scritps
- touch createimage.sh
- In order to create a docker container image, docker community have created a script supporting different package structures like yum, deb. This script will grab the core components of the OS needs and will build the image out of it. The image created will be super light weight, small and easily portable.

- Download the script from the below url.

https://github.com/docker/docker/blob/master/contrib/mkimage-yum.sh

Copy the contents of the downloaded script to /tmp/scripts/createimage.sh

Make the script executable using the following command.

chmod +x createimage.sh

- Run the script with the name of the container as the parameter as given below

./createimage.sh centos65base

- Image will start building. Once done issue the below command "docker images" to view the images as it will be imported to docker automatically.


- Also the container will start running automatically. Check it with the command "docker ps -all"


- Remove the container from running state to export it as image file.

docker rm <container_id>

- If you wanna remove the images (though we have not created one yet), use the following command.

docker rmi <image_id>

- Export / Save the container image to disk as a tar ball using the following command.

Example : docker save [image_ID] > <path_to_save>/<file_name>.tar
Command : docker save 45gh332243 > /tmp/centos65base.tar


Step 4 : Copy the image

Copy to image to any other machine where you wanna use it using the scp command.

scp root@<source_ip_address>:/tmp/centos65base.tar .
- Go ahead and shutdown the virtual machine with which the docker image was created.

Step 5 : Using Docker Image

- If you are going to use the Docker image in a Mac OS X machine or Windows machine, you need to install Boot2Docker application. Installation procedures can be found with the help of this link

For illustration purpose, we are using Mac OS X to power and use the container.

- Power the boot2docker application in Mac OS X under applications directory.


- Docker will start running in the machine.

- Load the docker image using the following command.

  docker load < /tmp/centos65base.tar

- Check the loaded docker image using the command "docker images".


- The docker image loaded doesn't have any repository name or the tag name. Let's give it a name and a tag. Use the following command.

docker tag <image_id> repository_name:tag_id



- Let's now get the docker container running. Use the command below

   Example : docker run -i -t <image_id> <type_of_shell> Command : docker run -i -t 45gh332243 /bin/bash



- Since the container has only the core components of the OS, lets try the command to open the vim editor. It will throw an error message, "Command not found"


- Let's install vim editor in the container using the command "yum install vim".

- Now the vim is installed and the editor opens.

- Now even if you accidentally exited the container, the data will remain same. That means, even if I quit the container with out saving the changes, the changes will be present. In our case, the vim will remain installed.

  
- Actually what happens when you exit the container is that, it goes to a halt state. Only when you remove the container and spin up a new container, all the changes will be discarded.


- Now to start the container, issue the below commands.

   docker ps -all
docker start <container_id>
docker attach <container_id>

- Once inside the container, execute the command "vim" and the editor will get opened.

- Now the proper way to detach from the container, leaving back all the services running is done by pressing Ctrl + P and then releasing all the keys and followed by pressing Ctrl + Q.


- Now since the container is not halted, we can get inside the command by issuing

docker attach <container_id>

- Press enter after the command and you will get a blank line and another enter will take you to the shell.

- And the hostname will be container_id always.

- Now in order to save the changes made to the container, issue the following command.

   Example : docker commit <container_id> <repository_name>:<tag_name>
Command : docker commit 45gh332243 centos65base/vim:vim_base



 - Remove the older container and spin a new container with the new image, and you will see vim pre-installed.

- To export the new image, issue any one of the below commands.
docker save [image_ID] > <path_to_save>/<file_name>.tar
docker save [repository_name] > <path_to_save>/<file_name>.tar

docker save 45gh332243 > /tmp/centos65_vim.tar
docker save centos65base/vim > /tmp/centos65_vim.tar

Tidak ada komentar:

Posting Komentar