Authorization statement: This article authorizes the official Amazon Cloud Technology article to forward and rewrite the rights, including but not limited to Amazon Cloud Technology official channels such as Developer Centre, Zhihu, self-media platforms, third-party developer media, etc.
Foreword
We sometimes encountered such a problem in our previous development: after developing a function locally, it was deployed to the server, or when others pulled it to the local and continued development, the function would become unusable.
And it usually takes a long time for us to build the environment. For team collaboration, every time a new person comes in, this avoidable time needs to be wasted. Moreover, when setting up the environment, various problems often occur, causing the project code to run abnormally.
If Docker is used, only the first person needs to write the development container, and others only need to pull it down to complete the construction of the project environment, which can effectively avoid meaningless waste of time.
So today I will talk about how to deploy docker on the server and simply run it
Prepare cloud server
Login
Click My Account->aws Management Console. Can jump to the login page
If you are a new user, you can register first. New users can use the server for free for 1 year
Console
- After entering the console, click Service->Compute->EC2
- Create instance
After selecting the region where you want to create an instance, click Start a new instance.
Just select the default system here. If you want another system, you can select the system you want.
Configure the login key. This file must be saved as it will be used for subsequent logins.
You can modify the size of the configuration storage, because 30G is given to free users, but the default is only 8G.
After configuring the basic information, just click Start Instance on the right. Be careful not to exceed the scope of the free package.
You can see that the list already has server information.
- Connect to instance
docker
Installation
Method 1: Use the system package manager
Special Note: Because I chose Amazon Linux 2023, I used the following command to install it
sudo yum install -y docker
If it’s Amazon Linux 2, run the following command:
sudo amazon-linux-extras install docker
After the installation is complete, check the docker version
docker -v # Check docker version
Start and join boot
sudo systemctl start docker #start sudo systemctl enable docker #Start automatically after booting
Check the docker status
Method 2: Install using binary files
Download binaries
You can select the binary file you want to download from https://download.docker.com/linux/static/stable/
#Download binary installation package wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.7.tgz #Unzip the compressed package tar -zxvf docker-20.10.7.tgz #Copy all the files in the unzipped docker directory to /usr/bin cp docker/* /usr/bin/
Manage docker with systemd
Add docker group
groupadd docker
Install two unit files (service and socket) into /usr/lib/systemd/system/
cat > /usr/lib/systemd/system/docker.service << EOF [Unit] Description=Docker Application Container Engine Documentation=https://docs.docker.com After=network-online.target firewalld.service Wants=network-online.target Requires=docker.socket [Service] Type=notify ExecStart=/usr/bin/dockerd ExecReload=/bin/kill -s HUP $MAINPID TimeoutSec=0 RestartSec=2 Restart=always StartLimitBurst=3 StartLimitInterval=60s LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity TasksMax=infinity Delegate=yes KillMode=process OOMScoreAdjust=-500 [Install] WantedBy=multi-user.target EOF
cat > /usr/lib/systemd/system/docker.socket << EOF [Unit] Description=Docker Socket for the API [Socket] ListenStream=/var/run/docker.sock SocketMode=0660 SocketUser=root SocketGroup=docker [Install] WantedBy=sockets.target EOF
I used systemctl start docker and it failed to start.
Tip: Job for docker.service failed because the control process exited with error code.
See “systemctl status docker.service” and “journalctl -xeu docker.service” for details.
When starting with dockerd & amp;, the prompt “failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: Iptables not found”
Description: Iptables is not installed. Install Iptables.
yum install iptables -y
Re-execute systemctl start docker and the startup is successful.
Docker is now installed and running
We verify that Docker is installed correctly by running the hello-world image.
You can see that the operation was successful, so we successfully installed docker using the binary file.
Easy to use
Use docker to deploy a redis service
- Pull redis image
sudo docker pull redis:7.0.12
Here we pulled a redis image of version 7.0.12
- Start the redis container
sudo docker run --restart=always -p 6379:6379 --name myredis -d redis:7.0.12 --requirepass redispwd
– restart always Automatically restart the container when it exits
– name myredis Specify a name for the container, here it is “myredis”
– requirepass redispwd sets the password of the Redis server
-p 6379:6379: Map port 6379 on the host to port 6379 of the container so that the outside world can access the Redis container through port 6379 of the host
-d runs the container in background mode, running in the background without blocking the terminal
- Enter the redis container
sudo docker exec -it myredis /bin/bash
Use redis-cli to interact with the redis database
Effect:
Summary
You can see that it is very simple for us to use docker to deploy a service. We only need to pull the image and run the container. And as long as we specify the image version, we don’t have to worry about environment inconsistencies.