In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
In the previous article, you have completed the Docker Desktop installation and enabled Kubernetes, and then you can deploy the application in a containerized manner. In general, a development workflow includes the following:
1. First create a Docker image and create and test a single container for each component of the application.
2. Assemble the container and the supported infrastructure into a complete application, which can be implemented with Docker stack file or Kubernetes's YAML.
3. Test, share and deploy complete containerized applications.
In this space, we will focus on step 1 of this workflow: creating an image based on an existing container. Remember that the Docker image captures the private file system on which the containerization process will run; we need to create an image that contains exactly what our application needs to run.
Containerized development environments are easier to set up than traditional development environments, and once you've learned how to build an image, we'll discuss it below. This is because the containerized development environment isolates all the dependencies required by the application in the Docker image; nothing needs to be installed on the development machine except Docker (that is, the deployment of the development environment depends only on the Docker image). In this way, you can easily develop applications for different stacks without changing any environment on the development machine.
Configuration
1. Clone the sample project from GitHub for demonstration.
Git clone-b v1 https://github.com/docker-training/node-bulletin-boardcd node-bulletin-board/bulletin-board-app
This is a simple bulletin board application written in node.js. In this example, let's assume that you wrote the application and are now trying to containerize it, with the directory structure shown in figure 1.1.
Figure 1.1
Look at the file named Dockerfile, which describes how to assemble a private file system for the container and contains some metadata that describes how to run the container based on this image. The bulletin board application Dockerfile is shown in figure 1.2.
Figure 1.2
The first step in containerized deployment of an application is to write Dockerfile. You can think of these Dockerfile commands as a step in creating a mirror image. The steps are as follows:
1. Use the image of version number 6.11.5 as the basic image of this construction. 6.11.5 the basic image is officially provided by node.js
2. Specify the working directory of the image file system as / usr/src/app, that is, every time you log in to a container created based on the image, the directory will be automatically changed to / usr/src/app.
3. Copy the package.json file to the current location of the mirror, namely / usr/src/app/package.json
4. Run the npm install command on the mirror file system (it will read the package.json to determine the application's node dependencies and install them)
Copy the rest of the source code of the application from the host to the mirror.
You can see that these steps are roughly the same as those you might take when setting up and installing the application on the host, but using Dockerfile allows us to build again on a portable, stand-alone Docker image (the official node.js image).
The above steps build the file system for the image, but there is another line in Dockerfile, the CMD command line. CMD ["Program to run", "Parameter 1", "Parameter 2"] is the command or script that tells the container to run at startup, and the command in Dockerfile tells the image that the containerization process to be supported is npm start.
Note: Dockerfile can only have one CMD command, and if there are more than one, execute the last one.
A Dockerfile always starts with the FROM command. The above is a simple Dockerfile file, and for more Dockerfile instructions, see Dockerfile reference (https://docs.docker.com/engine/reference/builder/).
Build the image and test it
Now that we have the source code and Dockerfile, it's time to build our first image and make sure that the container launched from that image works as expected.
Note: this example uses a Linux container. For users using Docker Desktop, right-click the Docker logo in the system tray to make sure your environment is running the Linux container. If not, click "Switch to Linux containers...", as shown in figure 1.3, my current demo environment is already using the Linux container.
Figure 1.3
1. Enter Powershell and make sure that your current directory is node-bulletin-board/bulletin-board-app, as shown in figure 1.4. Execute the command.
# docker image build-t bulletinboard:1.0.
Figure 1.4
The following words will be output at the end of the build, indicating that the image was built successfully.
Successfully built 49f9b9fb7daf
Successfully tagged bulletinboard:1.0
The above command table is to create an image named bulletinboard with a tag of 1.0, as shown in figure 1.5.
Figure 1.5
2. Run a container named bb based on the bulletinboard image and run it in the background, mapping the container's internal port 8080 to the host's port 8000.
# docker container run-publish 8000 detach 8080-detach-name bb bulletinboard:1.0
We can use the following command to see which parameters can be followed after run instruction
# docker container run-help
We log in to the container and see that the current directory is the working directory set in Dockerfile, as shown in figure 1.6.
Figure 1.6
3. When you access your application through http://localhost:8000, you will see the following interface, as shown in figure 1.7, which indicates that the application has been successfully deployed in containerization, followed by testing, building, publishing, sharing and other processes.
Figure 1.7
4. Delete a container, which means that the life cycle of the container ends.
# docker container rm-force bb
Summary
After doing this, we have been able to perform a simple containerization of the application and confirm that our application runs successfully in its container. The next step will be to write a yaml file for Kubernetes that describes how to run and manage containers on Kubernetes.
In addition, we should strengthen the practice of building images using Dockerfile.
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.