How to Interactively Create a Docker Container


http://linoxide.com/linux-how-to/interactively-create-docker-container/


March 21, 2015 | By Arun Pyasi in LINUX HOWTO


Hi everyone, today we'll learn how we can interactively create a docker container using a docker image. Once we start a process in Docker from an Image, Docker fetches the image and its Parent Image, and repeats the process until it reaches the Base Image. Then the Union File System adds a read-write layer on top. That read-write layer, the information about its Parent Image and some other information like its unique id, networking configuration, and resource limits is called a Container. Containers has states as they can change from running to exited state. A container with state as running includes a tree of processes running on the CPU, isolated from the other processes running on the host where as exited is the state of the file system and its exit value is preserved. You can start, stop, and restart a container with it.


Docker Technology has brought a remarkable change in the field of IT enabling cloud service for sharing applications and automating workflows, enabling apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments. In this article, we'll build CentOS Instance in which we'll host a website running under Apache Web Server.

Here is quick and easy tutorial on how we can create a container in an interactive method using an interactive shell.

1. Running a Docker Instance

Docker initially tries to fetch and run the required image locally and if its not found in local host the it pulls from the Docker Public Registry Hub . Here. we'll fetch and create a fedora instance in a Docker Container and attach a bash shell to the tty.

# docker run -i -t fedora bash

Downloading Fedora Base Image

2. Installing Apache Web Server

Now, after our Fedora base image with instance is ready, we'll now gonna install Apache Web Server interactively without creating a Dockerfile for it. To do so, we'll need to run the following commands in a terminal or shell.

# yum update

Updating Fedora Base Image

# yum install httpd

Installing httpd

# exit

3. Saving the Image

Now, we'll gonna save the changes we made into the Fedora Instance. To do that, we'll first gonna need to know the Container ID of the Instance. To get that we'll need to run the following command.

# docker ps -a

Docker Running Container

Then, we'll save the changes as a new image by running the below command.

# docker commit aefcdd0669ad fedora-httpd

committing fedora httpd

Here, the changes are saved using the Container ID  and image name fedora-httpd. To make sure that the new image is running or not, we'll run the following command.

# docker images

view docker images

4. Adding the Contents to the new image

As we have our new Fedora Apache image running successfully, now we'll want to add the web contents which includes our website to Apache Web Server so that our website will run successfully out of the box. To do so, we'll need to create a new Dockerfile which will handle the operation from copying web contents to allowing port 80. To do so, we'll need to create a file Dockerfile using our favorite text editor as shown below.

# nano Dockerfile

Now, we'll need to add the following lines into that file.

FROM fedora-httpd
ADD mysite.tar /tmp/
RUN mv /tmp/mysite/* /var/www/html
EXPOSE 80
ENTRYPOINT [ "/usr/sbin/httpd" ]
CMD [ "-D", "FOREGROUND" ]

configuring Dockerfile

Here, in above Dockerfile, the web content which we have in mysite.tar will get automatically extracted to /tmp/ folder. Then, the entire site will move to the Apache Web root ie /var/www/html/ and the expose 80 will open port 80 so that the website will be available normally. Then, the entrypoint is set to /usr/sbin/httpd so that the Apache Server will execute.

5. Building and running a Container

Now, we'll build our Container using the Dockerfile we just created in order to add our website on it. To do so, we'll need to run the following command.

# docker build -rm -t mysite .

Building mysite Image

After building our new container, we'll want to run the container using the command below.

# docker run -d -P mysite

Running mysite Container

Conclusion

Finally, we've successfully built a Docker Container interactively. In this method, we build our containers and image directly via interactive shell commands. This method is quite easy and quick to build and deploy our images and containers. If you have any questions, suggestions, feedback please write them in the comment box below so that we can improve or update our contents. Thank you ! Enjoy :-)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值