Exploring Docker: Achieving Seamless Integration between Local and Cloud
- Seamless Switching between Local Development and Cloud Deployment
Abstract: In this era, we are all looking for a solution that can seamlessly switch between local and cloud. Our team, along with a group of “veterans”, researched how to use Docker to achieve this goal. Our research results will help IT developers better understand and utilize the powerful features of Docker.
Body:
In our daily work, we often need to switch between local and cloud. This is because we develop applications locally and then deploy them to the cloud. However, this process is not always smooth. Our team, along with a group of “veterans”, researched how to use Docker to solve this problem.
We found that Docker’s underlying system can be any Linux distribution, depending on the base image you choose. This means that whether your container is running the Ubuntu operating system or the CentOS operating system, you can achieve seamless switching through Docker.
However, we found in practice that even on a Windows host, you can run containers based on Ubuntu or CentOS. This is because Docker uses Hyper-V on Windows to create a Linux virtual machine to run containers. This is why there are some differences between Docker on Linux and Windows, because their kernels are different.
In our research, we also found that when using Docker, you need to pay attention to the issue of apt-get sources. We found that in the local Windows system’s Ubuntu-22.04, the apt-get source configuration file is \wsl.localhost\Ubuntu-22.04\etc\apt\sources.list. However, in the CentOS system of Alibaba Cloud server, there is no apt-get source, only yum source.
To solve this problem, we proposed a solution. We use a new debian.sources file in the container to overwrite the old file. The content of the new /etc/apt/sources.list.d/debian.sources file is as follows:
Types: deb
URIs: http://mirrors.aliyun.com/debian
Suites: bookworm bookworm-updates
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
Types: deb
URIs: http://mirrors.aliyun.com/debian-security
Suites: bookworm-security
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
You can also add the following command to the Dockerfile to implement this function
RUN echo "deb http://mirrors.aliyun.com/debian bookworm main" > /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main" >> /etc/apt/sources.list
In this way, no matter where we use Docker, we can ensure the consistency of apt-get sources.
Main problem: 【The apt-get command in the docker python container in the local windows is normal】 【The apt-get command in the docker python container in the cloud server centos is abnormal】
Question 1: What is the difference between using a docker container locally and using a docker container on an Alibaba Cloud server? Answer: The docker container environment of the local Windows is based on the Ubuntu underlying system, which means that the Ubuntu operating system is running inside your container. The docker container of the cloud server centos is based on the centos underlying system, which means that the CentOS operating system is running inside your container. The underlying system of the Docker container can be any Linux distribution, depending on the base image you choose.
Although the operating systems inside the containers can be different, they all depend on the kernel of the host machine. This is why there are some differences between Docker on Linux and Windows, because their kernels are different. On Windows, Docker uses Hyper-V to create a Linux virtual machine to run containers. This is why even on a Windows host, you can run containers based on Ubuntu or CentOS. On a Linux host (such as CentOS), Docker containers interact directly with the host kernel, without the need for an additional virtualization layer. This is why Docker’s performance on Linux is usually better than Windows.
About the local development environment: 1: My local environment, windows11, windows needs to install wsl2 to install docker, so there is Ubuntu system on the local windows
windows中的ubuntu系统
2: Check the apt-get source configuration file in Ubuntu-22.04 in the local windows system
\\wsl.localhost\Ubuntu-22.04\etc\apt\sources.list
The following figure shows that the sources.list file is the apt-get source configuration file in Ubuntu-22.04 in the local windows system So the apt-get command in the container may be this source
Ubuntu上的apt-get源
About the cloud server: 3: Check the source configuration file in the centos of the Alibaba Cloud server The Alibaba Cloud server used, the centos system, this centos system does not have this apt-get source, only the yum source The yum source file is recorded in the following folder
/etc/yum.repos.d/
The following figure shows that the yum.repos.d file contains the yum source configuration
Using docker in the centos system, there is a part of the underlying core that uses things in the centos system, but many of the underlying systems in the docker image are debian-like, so when the docker image is decompressed into the container, the apt-get command is executed. When you need to check what the apt-get source in the container is And you need to check whether the server’s network can access the link to this source, whether it can be downloaded from this source
3.1【Check whether the cloud server can link to the ubuntu official website】 Test whether the centos system can connect to the apt-get source connection test,【Use the ping command to test】
ping -c 5 archive.ubuntu.com
The following figure shows that the connection result can be connected
3.2【Start the container and check whether it can be linked to the ubuntu official website in the container】 Start a debian-like container in the centos system
docker run --rm --name pythons -v /home/luichun/luichuns/testfile:/home/testfile -it python:3.12.3-bookworm bash
Here 3.2 uses a mount directory,【Because an operation in step 4 is to copy files from the host to the container】
Because there is no ping command in the container, but there is a curl command, so use the curl command instead of the ping command to test the link
curl -I http://archive.ubuntu.com
The following figure shows that the connection result can be connected
3.3【Check whether the apt-get command in the container can be used】 Check whether the apt-get command in the container can be used
apt-get
The following figure shows that the apt-get source in the docker container in the centos system of the cloud server is only a single one
3.4【Execute the apt-get update command to check whether the link can be updated】 Execute the apt-get update command
apt-get update
The effect is as follows: Cannot connect to the network【If it cannot be updated, then installing the plug-in with apt-get will report an error】
How to solve the compatibility problem between the development environment and the deployment environment:
4: Solve the【apt-get update method】 Use a new debian.sources file in the container to overwrite the old file The content of the new /etc/apt/sources.list.d/debian.sources file, after overwriting, can normally execute the apt-get command
Types: deb
URIs: http://mirrors.aliyun.com/debian
Suites: bookworm bookworm-updates
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
Types: deb
URIs: http://mirrors.aliyun.com/debian-security
Suites: bookworm-security
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
Copy the debian.sources file in the mount directory to the apt-get source configuration in the container
cp /home/testfile/debian.sources /etc/apt/sources.list.d/debian.sources
If you don’t want to use the operation of copying to overwrite the apt-get source, you can add the source command in the Dockerfile Add the following command in the Dockerfile:
RUN echo "deb http://mirrors.aliyun.com/debian bookworm main" > /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main" >> /etc/apt/sources.list
After changing, execute the apt-get update update command The following figure shows: Successfully executed the apt-get update update command