This topic describes how to deploy and use Docker on an Elastic Compute Service (ECS) instance that runs a CentOS 8.1 64-bit operating system. This topic is intended for developers who are familiar with Linux but new to Alibaba Cloud ECS.
Prerequisites
An ECS instance that runs a CentOS 8.1 64-bit operating system is created. For more information, see Create an instance by using the wizard.
- Instance type: ecs.g6.large
- Operating system: CentOS 8.1 64-bit
- Network type: Virtual Private Cloud (VPC)
- IP address: public IP address
Background information
- Deploy Docker. For more information, see the Deploy Docker section.
- Use Docker.
- For information about how to use 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 a Docker image from Alibaba Cloud Marketplace to deploy Docker on an ECS instance in one click.
Use Docker
- 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. systemctl status docker #Check the running state of Docker.
- Manage images. In the following example, an Apache image from Alibaba Cloud Container
Registry is used:
docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
- Modify tags. The names of images from Alibaba Cloud Container Registry are long. Use
tags to make the images easy to identify.
docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1
- View existing images.
docker images
- Forcefully delete an image.
docker rmi -f registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
- Modify tags. The names of images from Alibaba Cloud Container Registry are long. Use
tags to make the images easy to identify.
- Manage containers.
- Access the 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. - Add the
–d
parameter to therun
command to run the container in the background. Add the--name
parameter to the command to specify 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 by using the following command syntax:
docker commit <Container ID or container name> [<Repository name>[:<Tag>]]
.docker commit containerID/containerName repository:tag
- For testing and restore purposes, run the image and derive a new image that has a
simple name. Then, test the new image.
docker commit 4c8066cd8**** apachephp:v1
- Run the container and map port 8080 of the host on which the ECS instance resides
to the container.
docker run -d -p 8080:80 apachephp:v1
In a browser, enter <IP address of the ECS instance>:8080 to connect to the container. A page similar to the one shown in the following figure 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 a security group rule.
- Access the container. Run the