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.
# 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.
# yum update
Installing httpd
# yum install httpd
Installing httpd
Exits the container's TTY.
# 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).
# docker ps -a
Docker Running Container
Then, we will save these changes to a new image, run the following command.
# 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.
# 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.
# nano Dockerfile
Now, we need to add the following command line to the 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 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.
# 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.
# 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