Containers
Container and Docker
Container technologies originate from Linux. Containers provide lightweight virtualization and allow process and resource isolation. Containers become popular since the emergence of Docker. Docker is the first system that allows containers to be portable in different machines. It simplifies both the application packaging and the application library and dependency packaging. Even the OS file system can be packaged into a simple portable package, which can be used on any other machine that runs Docker.
Except for similar resource isolation and allocation modes as VMs, containers virtualize OSs, making them more portable and efficient.
Containers have the following advantages over VMs:
- Higher system resource utilization
With no overhead for virtualizing hardware and running a complete OS, containers outperform VMs no matter in application execution speed, memory loss, and file storage speed. Therefore, with same configurations, containers can run more applications than VMs.
- Faster startup
Traditional VMs usually take several minutes to start an application. However, Docker containerized applications run directly on the host kernel with no need to start the entire OS, so they can start within seconds or even milliseconds, greatly saving your time in development, testing, and deployment.
- Consistent running environments
One of the biggest problems in development is the inconsistency of application running environment. Due to inconsistent development, testing, and production environments, some bugs cannot be discovered prior to rollout. A Docker container image provides a complete runtime to ensure consistency in application running environments.
- Easier migration
Docker ensures the consistency in execution environment, so migrating applications becomes much easier. Docker can run on many platforms, and no matter on physical machines or virtual ones, its running results remains the same. Therefore, you can easily migrate an application from one platform to another without worrying that the environment change will cause the applications fail to function.
- Easier maintenance and extension
Tiered storage and image technology applied by Docker facilitate the reuse of applications and simplify application maintenance and update as well as further image extension based on base images. In addition, Docker collaborates with open-source project teams to maintain a large number of high-quality official images. You can directly use them in the production environment or form new images based on them, greatly reducing the image production cost of applications.
Typical Process of Using Docker Containers
Before using a Docker container, you should know the core components in Docker.
- Image: A Docker image is a software package that contains everything needed to run an application, such as the code and the runtime it requires, file systems, and executable file path of the runtime and other metadata.
- Image repository: A Docker image repository is used to store Docker images, which can be shared between different users and computers. You can run the image you compiled on the computer where it is compiled, or upload it to an image repository and then download it to another computer and run it. Some repositories are public, allowing everyone to pull images from them. Others are private, which are accessible only to some users and machines.
- Container: A Docker container is usually a Linux container created from a Docker image. A running container is a process running on the Docker host. However, it is isolated from the host and all other processes running on the host. The process is also resource-limited, meaning that it can access and use only resources (such as CPU and memory) allocated to it.
Figure 2 shows the typical process of using containers.
- A developer develops an application and creates an image in the development machine.
Docker runs the commands to create an image and store it on the machine.
- The developer sends a command to upload the image.
After receiving the command, Docker uploads the local image to the image repository.
- The developer sends an image running command to the machine.
After the command is received, Docker pulls the image from the image repository to the machine, and then runs a container based on the image.
Example
In the following example, Docker packages a container image based on the Nginx image, runs an application based on the container image, and pushes the image to the image repository.
Installing Docker
Docker is compatible with almost all operating systems. Select a Docker version that best suits your needs.
In Linux, you can run the following command to install Docker:
curl -fsSL get.docker.com -o get-docker.sh sh get-docker.sh systemctl restart docker
Packaging a Docker Image
Docker provides a convenient way to package your application, which is called Dockerfile.
# Use the official Nginx image as the base image. FROM nginx:alpine # Run a command to modify the content of the nginx image index.html. RUN echo "hello world" > /usr/share/nginx/html/index.html # Permit external access to port 80 of the container. EXPOSE 80
Run the docker build command to package the image.
docker build -t hello .
In the preceding command, -t indicates that a tag is added to the image, that is, the image is named. In this example, the image name is hello. . indicates that the packaging command is executed in the current directory.
Run the docker images command to view the image. You can see the hello image has been created successfully. You can also see an Nginx image, which is downloaded from the image repository and used as the base image of the hello image.
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello latest d120ec16dcea 17 minutes ago 158MB nginx alpine eeb27ee6b893 2 months ago 148MB
Running the Container Image Locally
After obtaining the image, you can run the docker run command on the local host to run the container image.
#docker run -p 8080:80 hello
The docker run command will start a container. In the preceding command, -p indicates that port 8080 of the local host is mapped to port 80 of the container. That is, the traffic of port 8080 of the local host will be forwarded to port 80 of the container. When you access http://127.0.0.1:8080 on the local host, you can access the container. In this case, the content returned by the browser is hello world.
Pushing the Image to the Image Repository
You can push images to SoftWare Repository for Container (SWR). The following describes how to push images to SWR.
Log in to the SWR console. In the navigation pane, choose My Images. On the page that is displayed, click Upload Through Client. In the dialog box that is displayed, click Generate a temporary login command. Then, copy the command and run it on the local host to log in to the SWR image repository.
Before uploading an image, you need to specify a complete name for the image.
# docker tag hello registry.eu-west-0.prod-cloud-ocb.orange-business.com/container/hello:v1
In the preceding command, registry.eu-west-0.prod-cloud-ocb.orange-business.com indicates the repository address. The address varies depending on the region. v1 indicates the tag allocated to the hello image.
- registry.eu-west-0.prod-cloud-ocb.orange-business.com is the repository address. Addresses of each region are different.
- container is the organization name. Generally, an organization is created in SWR. If no organization is created, an organization is automatically created when the image is uploaded for the first time. The organization name is globally unique in a single region. You need to select a proper organization name.
- v1 is the version number allocated to the hello image.
Run the docker push command to upload the image to SWR.
# docker push registry.eu-west-0.prod-cloud-ocb.orange-business.com/container/hello:v1
If you need to use the image, run the docker pull command to pull (download) the image.
# docker pull registry.eu-west-0.prod-cloud-ocb.orange-business.com/container/hello:v1
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot