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 :-)



posted @ 2016-06-21 08:46  张同光  阅读(106)  评论(0编辑  收藏  举报