How to create a docker image interactively

Source: Internet
Author: User
Tags docker run

Today we'll learn how to create a Docker image interactively with a docker image. When we start a Docker process from the mirror, Docker acquires the image and its parent image and repeats the process until it reaches the underlying image. The Federated File System (UFS) then adds a read-write layer to its top level. The read-write layer is called a container, which contains information about the parent image and some other information, such as unique ID, network configuration, and resource constraints. The container is stateful and its state can be switched from the running state to the exiting state. A running container contains a process tree running on the CPU, isolated from other processes running on that host, and the exit State refers to the status of the filesystem and retains its exit value. You can use it to start, stop, and restart a container. In this article, we will build the CentOS environment and then use the Apache Web server to provide a Web service.

1. Running a Docker instance

Docker will first attempt to get and run the desired image locally, and if it is not found on the local host, it will pull from the Docker public registry. Here, we will pull the image and create a Fedora instance in the Docker container and connect to the bash shell on its tty.

    1. # docker run -i -t fedora bash

Downloading Fedora Base Image

2. Installing the Apache network server

Now, after our Fedora basic image instance is ready, we will start to install the Apache Web server interactively instead of creating dockerfile for it. To do this, we need to run the following command in the terminal or shell.

    1. # yum update

Installing httpd

    1. # yum install httpd

Installing httpd

Exits the container's TTY.

    1. # exit
3. Save the image

Now, we're going to save the changes made in the Fedora instance. To do this, we first need to know the container ID of the instance. In order to get the ID, we also need to run the following command (LCTT: Execute the command outside the container).

    1. # docker ps -a

Docker Running Container

Then, we will save these changes to a new image, run the following command.

    1. # docker commit c16378f943fe fedora-httpd

Committing Fedora httpd

Here, the modification has been saved by using the container ID, and the image name is called FEDORA-HTTPD. In order to verify that the new image is running, we will run the following command.

    1. # docker images

View Docker Images

4. Add content to the new image

Our own new Fedora Apache image is running successfully, and now we want to add some of our website's web content to the Apache Web server, making the site available out of the box. To do this, we need to create a new dockerfile that handles everything from copying the contents of the Web page to enabling port 80. To achieve this, we need to create a dockerfile file using our favorite text editor, as shown below.

    1. # nano Dockerfile

Now, we need to add the following command line to the file.

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

Configuring Dockerfile

Here, in the above Dockerfile, the contents of the Web page placed in Mysite.tar will be automatically extracted to the/tmp/folder. Then, the entire site will be moved to the Apache Web root directory/var/www/html/, command expose 80 will open 80 port, so that the site will be able to access the normal. Second, the entry point is placed inside the/USR/SBIN/HTTPS to ensure that the Apache server is able to execute.

5. Build and run a container

Now we're going to create our container with the dockerfile we just created in order to add our site to it. To do this, we need to run the following command.

    1. # docker build -rm -t mysite .

Building MySite Image

Once we have established our new container, we need to use the following command to run the container.

    1. # docker run -d -P mysite

Running MySite Container

Summarize

Finally, we have successfully built a docker container in an interactive way. In this section of the method, we create our containers and mirrors directly from the interactive shell command. This approach is simple and fast when creating and configuring mirrors and containers. If you have any questions, suggestions and feedback, please write them down in the comment box below so that we can upgrade or update our articles. Thank you! Wish you a happy life:-)

How to create a docker image interactively

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.