×
Community Blog Deploy and Use Docker on ECS Instances

Deploy and Use Docker on ECS Instances

This article describes how to deploy and use Docker on an Alibaba Cloud ECS instance.

By Alibaba Cloud ECS Team

Background Information

Docker is an open-source application container engine that offers advantages such as portability, scalability, high security, and manageability. It allows developers to package applications and dependencies into portable containers, enabling efficient building, deployment, and management of applications on Linux machines. Alibaba Cloud provides Docker image repositories for quick deployment of Docker.

Prerequisites

Ensure that you have created an ECS instance with the following configurations. If you haven't created one yet, please refer to this document: Create an instance by using the wizard.

  • Operating system: CentOS 7.x 64-bit, CentOS 8.x 64-bit, Alibaba Cloud Linux 3 64-bit, or Alibaba Cloud Linux 2 64-bit
  • Network type: Virtual Private Cloud (VPC)
  • IP address: a public IP address
  • Security group: inbound traffic allowed on ports 80, 22, and 8080. For more information, see Add a security group rule.

Deploy Docker

1.  Connect to an ECS instance. For more information about connection methods, see Connection methods.

2.  Install Docker.

In this example, the image version Alibaba Cloud Linux 3 is used.

a) Run the following command to add the DNF repository of Docker Community Edition (Docker-CE):

sudo dnf config-manager --add-repo=https://mirrors.aliyun.com/docker-
ce/linux/centos/docker-ce.repo

b) Run the following command to install the DNF repository plug-in that is dedicated to Alibaba Cloud Linux 3:

sudo dnf -y install dnf-plugin-releasever-adapter --repo alinux3-plus

c) Run the following command to install Docker:

sudo dnf -y install docker-ce --nobest

If you run the command and an error message similar to the following one is returned, commend out the CentOS repository under the /etc/yum.repos.d directory and then reinstall Docker-CE.

1

3.  Run the following command to check whether Docker is installed:

sudo docker -v

A command output similar to the following one indicates that Docker is installed.

2

4.  Run the following command to start Docker and configure Docker to run on system startup:

sudo systemctl start docker
sudo systemctl enable docker

5.  Run the following command to check whether Docker is started:

sudo systemctl status docker

A command output similar to the following one indicates that Docker is started.

3

Basic Docker Usage

This section describes only the basic usage of Docker. For information about more Docker usage, visit the Docker official website.

  • Manage Docker daemon
sudo systemctl start docker # Run the Docker daemon.
sudo systemctl stop docker # Stop the Docker daemon.
sudo systemctl restart docker # Restart the Docker daemon.
sudo systemctl enable docker # Configure Docker to start on system startup.
sudo systemctl status docker # Check the status of Docker
  • Manage images

In this article, we will use Apache images from Alibaba Cloud Container Registry as an example to demonstrate how to manage images using Docker.

o Pull an image.

sudo docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5

o Modify tags. The names of images from Alibaba Cloud Container Registry are long. Use tags to make the images easy to identify.

sudo docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1

o View existing images.

sudo docker images

o Forcefully delete an image.

sudo docker rmi -f registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5
  • Manage containers
  • To query the <Image ID> value for use in the following code snippets, run the docker images command.

o Start a new container.

sudo docker run -it <Image ID> /bin/bash

o Start a new container in the background and specify the name of the container.

sudo docker run -d --name <Container name> <Image ID>

o Query the container ID.

sudo docker ps

o Create an image from the container.

sudo docker commit <Container ID or container name> <Repository name >:< Tag>

Use Docker to Create Image

This section describes how to create a simple custom NGINX image from a Dockerfile.

1.  Run the following command to pull an image: In this example, an Apache image from Alibaba Cloud Container Registry is pulled.

sudo docker pull registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5

2.  Modify the tag of the image to make the image easier to identify.

sudo docker tag registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5:latest aliweb:v1

3.  Run the following command to create and edit a Dockerfile:

a) Run the following command to create and edit a Dockerfile:

vim Dockerfile

b) Press the i key to enter Insert mode and add the following content to the file:

#Declare a base image. 
FROM aliweb:v1
#Declare the owner of the base image. 
MAINTAINER DTSTACK
#Specify the commands that you want to run before the container starts. You must append the commands to the end of the RUN command. A Dockerfile can contain up to 127 lines. If the total length of your commands exceeds 127 lines, we recommend that you write the commands to a script. 
RUN mkdir /dtstact
#Specify the commands that are run on system startup. The last command must be a frontend command that runs constantly. Otherwise, the container exits after all commands are run. 
ENTRYPOINT ping www.aliyun.com

c) Press the Esc key, enter :wq, and then press the Enter key to save and close the Dockerfile file.

4.  Run the following command to create an image based on the base NGINX image. Create the command in the following format: docker build -t <Image name>:<Image version>..

The period (.) at the end of the command indicates the path of the Dockerfile. You must add the period (.).

For example, to create an image with the name aliweb and version v2, run the following command:

sudo docker build -t aliweb:v2 .

5.  Run the following command to check whether the new image is built:

sudo docker images

A command output similar to the one displayed in the following figure indicates that the image is created.

4

Install and Use docker-compose

docker-compose is an open-source container orchestration tool provided by the Docker team. It allows you to define and run multiple Docker containers. By using a YAML file, you can configure all the services required for your application. After parsing the YAML file configurations with the docker-compose command, you can create and start all the specified Docker services. Compose offers advantages such as lower operation and maintenance costs and improved deployment efficiency.

For more information about docker-compose, visit the Docker official website.

Note: Only Python 3 and above versions support docker-compose. Make sure you have pip installed.

Install docker-compose

1.  Run the following command to install setuptools.

pip3 install -U pip setuptools

2.  Run the following command to install docker-compose:

pip3 install docker-compose

3.  Run the following command to check whether docker-compose is installed:

docker-compose --version

If the version of docker-compose is returned, docker-compose is installed.

Use docker-compose to Deploy an Application

This section describes how to use docker-compose to deploy WordPress.

1.  Create and edit docker-compose.yaml files.

a) Run the following command to create a docker-compose.yaml file.

vim docker-compose.yaml

b) Press the i key to enter Insert mode and add the following content to the file.

In this example, the content that is used to install WordPress is added.

version: '3.1'               # Specify the version of Docker Compose.
services:
wordpress:               # Specify a service name.
image: wordpress       # Specify an image name.
restart: always        # Configure the container to start each time Docker starts.
ports:
- 80:80              # Specify port mappings.
environment:           # Configure environment variables.
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: 123456
WORDPRESS_DB_NAME: wordpress
volumes:               # Configure mappings between containers and ECS volumes.
- wordpress:/var/www/html
db:                      # Specify a service name.
image: mysql:5.7       # Specify an image name.
restart: always        # Configure the container to start each time Docker starts.
ports:
- 3306:3306         #  Specify port mappings.
environment:           # Configure environment variables.
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: 123456
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:               # Configure mappings between containers and ECS volumes.
- db:/var/lib/mysql
volumes:
wordpress:
db:

c) Press the Esc key to exit Insert mode, enter :wq and then press the Enter key to save and close the file.

2.  Run the following command to start the application:

sudo env "PATH=$PATH" docker-compose up -d

3.  Enter an address in the https://<Public IP address of the ECS instance> format in the address bar of your browser to go to the WordPress configuration page. You can configure the parameters as prompted to access WordPress.

0 1 0
Share on

Alibaba Cloud Community

895 posts | 201 followers

You may also like

Comments

Alibaba Cloud Community

895 posts | 201 followers

Related Products