Create Image from Docker Container for Centos RHEL 6

How to create Image from Docker Container for Centos RHEL 6

     We can create image from docker container for centos RHEL 6. Creating docker image from existing container can save us time if we need to change certain things regularly. For example if you need puppet agent on all your container then you do not have to install for every instance. Just create a container, install puppet agent and create an image out of it. Every container you create out of that agent will have puppet agent running. Similarly you can create your own custom tailored Docker images.

Exercise to create an image from docker container

  1. Download centos latest image. Start container in interactive mode and run the bash command.Then update the OS and install apache (httpd). Now exit the container and commit to make new image from the created container.

Download docker centos image and use docker run to create a container

[user@docker6 ~]$ docker pull centos:centos6
latest: Pulling from library/centos
256b176beaff: Pull complete
[user@docker6 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos6              5182e96772bf        3 weeks ago         200MB
[user@docker6 ~]$ docker run -it centos:centos6 /bin/bash

Now update the OS and install httpd package and other helpful tools. Now enable httpd to start at boot and start service httpd.

[root@f6548fdf6668 ~]# yum update -y 
[root@f6548fdf6668 ~]# yum install -y httpd curl wget elinks net-tools mlocate zip tar telnet 
[root@f6548fdf6668 ~]# chkconfig httpd on 
[root@f6548fdf6668 ~]# service httpd start

Check for runnng httpd and port

[root@f6548fdf6668 ~]# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      306/httpd

Check if port 80 is open to verify httpd is running, to do this you can telnet on the same container or detach from container and telnet the container ipaddress on port 80.  Then attach the container again

[root@f6548fdf6668 ~]# #PRESS ctrl+p+q to detach the container and got to the host terminal
[root@f6548fdf6668 ~]# 

Verifying container is still up and running

[user@docker7 ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f6548fdf6668        centos:latest       "/bin/bash"         About an hour ago   Up About an hour                        DockerTest

You are now in your host machine, use telnet command and to get out press ctrl+] and type in quit

[user@docker7 ~]$ sudo yum install telnet -y
[user@docker7 ~]$ telnet 172.17.0.2 80
Trying 172.17.0.2...
Connected to 172.17.0.2.
Escape character is '^]'.
^]
telnet> quit
Connection closed.

If you want to go back to the container you can  attach the container with docker attach command, use either container ID or Container NAME, in this example name is DockerTest

[user@docker6 ~]$ docker attach DockerTest
[user@docker6 ~]$ exit 

Here STATUS should show Exited

[user@docker6 ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
f6548fdf6668        centos:latest       "/bin/bash"         About an hour ago   Exited (137) 2 seconds ago                       DockerTest

Now using the container name commit the changes you made in the container to a new base image called centos6:withapache. List the images in your system.

[user@docker6 ~]$ docker commit DockerTest centos6:withapache
sha256:99e0c24ea26274cb8f5aadb9b36eb5f2842b82ab6b0504fa02842894b7f0326a
[user@docker6~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             withapache          99e0c24ea262        8 seconds ago       377MB
centos              latest              5182e96772bf        3 weeks ago         200MB

now check if apache is running in new container

[user@docker6 ~]$ docker run -it centos:centos6 /bin/bash
[user@docker6 ~]$ service httpd status

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.