This topic describes how to deploy and use Docker on an ECS instance that runs an Alibaba Cloud Linux 2.1903 LTS 64-bit operating system. This tutorial is intended for developers who are familiar with Linux but new to Alibaba Cloud ECS.
Prerequisites
- An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the account registration page.
- At least one instance is created. For more information, see Create an instance by using the wizard.
The following instance configurations are used in the examples:
- Instance type: ecs.g6.large
- Operating system: Alibaba Cloud Linux 2.1903 LTS 64-bit
Note The operation commands described in this example can also be used in the CentOS 7 operating system.
- Network type: VPC
- IP address: a public IP address
Background information
This topic describes the following operations:
- Deploy Docker. For more information, see the Deploy Docker section.
- Use Docker.
- For information about the basic usage of Docker, see the Use Docker section.
- For information about how to create a Docker image, see the Create a Docker image section.
Deploy Docker
This section describes how to manually install Docker. You can also purchase the required image from Alibaba Cloud Marketplace and easily deploy Docker.
Use Docker
You can use Docker in the following ways:
- Manage the Docker daemon.
systemctl start docker #Run the Docker daemon. systemctl stop docker #Stop the Docker daemon. systemctl restart docker #Restart the Docker daemon. systemctl enable docker #Configure Docker to run on system startup.
- Manage images. Apache images from Alibaba Cloud Container Registry are used in the
following examples.
docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
- Modify the tags of images from Alibaba Cloud Container Registry to simplify image
identification.
docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
- Check existing images.
docker images
- Force delete an image.
docker rmi -f registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
- Modify the tags of images from Alibaba Cloud Container Registry to simplify image
identification.
- Manage containers.
- Enter a container. Run the
docker images
command to obtain the ImageId value, which is e1abc****. Then, run thedocker run
command to access the container.docker run -it e1abc**** /bin/bash
- Exit the container. Run the
exit
command to exit the container. - You can combine the
run
command with the–d
parameter to run the container in the background. The--name
parameter specifies apache as the container name.docker run -d --name apache e1abc****
- Access the container that runs in the background.
docker exec -it apache /bin/bash
- Query the container ID.
docker ps
- Create an image from the container. Description of the parameters in the command:
docker commit <Container ID or container name> [<Repository name> [: <Tag>]]
.docker commit containerID/containerName repository:tag
- To easily test and restore an image, you can run the source image, derive a new image
that has a simple name from the source image, and then test the new image.
docker commit 4c8066cd8**** apachephp:v1
- Run the container and map port 8080 of the host to the container.
docker run -d -p 8080:80 apachephp:v1
In a browser, enter the IP address of the instance followed by the port number 8080 to connect to the container. The following response indicates that the container runs normally.Note An inbound rule must be added to a security group of the ECS instance to allow inbound traffic on port 8080. For more information, see Add security group rules.
- Enter a container. Run the