This topic describes how to install Docker and Docker Compose on a Linux Simple Application Server instance, and how to deploy containerized applications.
For a quick automated setup, see Quickly deploy Docker. For Docker documentation, see the Docker overview.
Prerequisites
Before you begin, make sure that you have:
A Linux Simple Application Server instance. See Create a Simple Application Server instance.
Ports 80 and 22 open in the instance firewall. See Set up a firewall.
Configure registry mirrors
If you experience slow image pulls from Docker Hub due to network issues, you can configure registry mirrors to speed up downloads. Note that this configuration does not apply to the docker search command, which always queries Docker Hub directly.
To configure the Alibaba Cloud Container Registry (ACR) image accelerator, log on to the ACR console. In the left-side navigation pane, choose . On the Image Accelerator page, find your unique accelerator address.
An unstable network connection might prevent the accelerator from pulling specific image versions. For more solutions, see Accelerate official image pulls.
Edit the Docker configuration file at
/etc/docker/daemon.json. If the file does not exist, create it.{ "registry-mirrors": ["<your-accelerator-address>"] }Save the file and run the following command to restart the Docker service and apply the changes.
ImportantRestarting the Docker service causes a brief service interruption. Proceed with caution.
sudo systemctl restart docker
Supported operating systems
| Operating system | Package manager |
|---|---|
| Alibaba Cloud Linux 4 | yum / dnf |
| Alibaba Cloud Linux 3 | dnf |
| Alibaba Cloud Linux 2 | yum |
| Ubuntu | apt |
| Debian | apt |
| Red Hat | yum |
| Fedora | dnf |
| Anolis OS | yum |
| CentOS 7.x | yum |
| CentOS 8.x | dnf |
Install Docker
Connect to your Simple Application Server instance. See Remotely connect to a Linux server.
Follow the steps for your operating system.
Alibaba Cloud Linux 4
Alibaba Cloud Linux 4 ships with Moby as the default container runtime. Choose one of the following options based on your needs:
| Option | When to use |
|---|---|
| Moby (recommended) | Default for most workloads. Maintained by Alibaba Cloud, optimized for Alibaba Cloud Linux 4, and updated with the OS. |
| Docker CE | Use this when you need Docker Community Edition features not available in Moby, or when you require a specific Docker version. |
Step 2: Install Docker
Option A: Install Moby (recommended)
sudo yum install -y mobyMoby is the default container runtime on Alibaba Cloud Linux 4 and will continue to receive updates in future versions. Older Docker components remain available for forward compatibility but will no longer be updated.
Option B: Install Docker CE
If you are not using an Alibaba Cloud server, replacehttp://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.
# Add the Docker CE package repository
sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo
# Select a CentOS version: replace $releasever with 9 or 10
# To use CentOS 9:
sudo sed -i 's|$releasever|9|' /etc/yum.repos.d/docker-ce.repo
# To use CentOS 10:
sudo sed -i 's|$releasever|10|' /etc/yum.repos.d/docker-ce.repo
# Install Docker CE, containerd.io, and the Buildx and Compose plugins
sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginTo switch back to Moby later:
sudo yum remove -y docker-ce docker-ce-cli
sudo rm -rf /etc/yum.repos.d/docker-ce.repoStep 3: Start Docker
# Start Docker and enable it to start on boot
sudo systemctl start docker
sudo systemctl enable docker
# Verify the installation
docker --versionRun sudo systemctl status docker to confirm the service is active:
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled)
Active: active (running) since ...Alibaba Cloud Linux 3
Step 2: Install Docker CE
If you are not using an Alibaba Cloud server, replacehttp://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.
# Add the Docker package repository
sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo
# Install the dnf repository compatibility plugin for Alibaba Cloud Linux 3
sudo dnf -y install dnf-plugin-releasever-adapter --repo alinux3-plus
# Install Docker CE, containerd.io, and the Buildx and Compose plugins
sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStep 3: Start Docker
sudo systemctl start docker
sudo systemctl enable dockerVerify the service is active:
sudo systemctl status dockerExpected output:
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled)
Active: active (running) since ...Ubuntu
Step 2: Install Docker CE
If you are not using an Alibaba Cloud server, replacehttp://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.
# Update the package list and install prerequisites
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# Add Docker's GPG key and repository
sudo curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository -y "deb [arch=$(dpkg --print-architecture)] http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Install Docker CE, containerd.io, and the Buildx and Compose plugins
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStep 3: Start Docker
sudo systemctl start docker
sudo systemctl enable dockerVerify the service is active:
sudo systemctl status dockerExpected output:
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled)
Active: active (running) since ...Debian
Step 2: Install Docker CE
If you are not using an Alibaba Cloud server, replacehttp://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.
# Update the package list and install prerequisites
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# Add Docker's GPG key and repository
sudo curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository -y "deb [arch=$(dpkg --print-architecture)] http://mirrors.cloud.aliyuncs.com/docker-ce/linux/debian $(lsb_release -cs) stable"
# Install Docker CE, containerd.io, and the Buildx and Compose plugins
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStep 3: Start Docker
sudo systemctl start docker
sudo systemctl enable dockerVerify the service is active:
sudo systemctl status dockerExpected output:
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled)
Active: active (running) since ...Red Hat
Step 2: Install Docker CE
If you are not using an Alibaba Cloud server, replacehttp://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.
# Add the Docker package repository
sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/rhel/docker-ce.repo
sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo
# Install Docker CE, containerd.io, and the Buildx and Compose plugins
sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStep 3: Start Docker
sudo systemctl start docker
sudo systemctl enable dockerVerify the service is active:
sudo systemctl status dockerExpected output:
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled)
Active: active (running) since ...Fedora
Step 2: Install Docker CE
If you are not using an Alibaba Cloud server, replacehttp://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.
# Add the Docker package repository
sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/fedora/docker-ce.repo
sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo
# Install Docker CE, containerd.io, and the Buildx and Compose plugins
sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStep 3: Start Docker
sudo systemctl start docker
sudo systemctl enable dockerVerify the service is active:
sudo systemctl status dockerExpected output:
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled)
Active: active (running) since ...Anolis OS
Step 2: Install Docker CE
If you are not using an Alibaba Cloud server, replacehttp://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.
# Add the Docker package repository
sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo
# Install Docker CE, containerd.io, and the Buildx and Compose plugins
sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStep 3: Start Docker
sudo systemctl start docker
sudo systemctl enable dockerVerify the service is active:
sudo systemctl status dockerExpected output:
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled)
Active: active (running) since ...Alibaba Cloud Linux 2
Step 2: Install Docker CE
If you are not using an Alibaba Cloud server, replacehttp://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.
# Add the Docker package repository
sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo
# Install the yum repository compatibility plugin for Alibaba Cloud Linux 2
sudo yum install yum-plugin-releasever-adapter --disablerepo=* --enablerepo=plus
# Install Docker CE, containerd.io, and the Buildx and Compose plugins
sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStep 3: Start Docker
sudo systemctl start docker
sudo systemctl enable dockerVerify the service is active:
sudo systemctl status dockerExpected output:
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled)
Active: active (running) since ...CentOS 7.x
Step 2: Install Docker CE
If you are not using an Alibaba Cloud server, replacehttp://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.
# Add the Docker package repository
sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo
# Install Docker CE, containerd.io, and the Buildx and Compose plugins
sudo yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStep 3: Start Docker
sudo systemctl start docker
sudo systemctl enable dockerVerify the service is active:
sudo systemctl status dockerExpected output:
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled)
Active: active (running) since ...CentOS 8.x
Step 2: Install Docker CE
If you are not using an Alibaba Cloud server, replacehttp://mirrors.cloud.aliyuncs.comwithhttps://mirrors.aliyun.com.
# Add the Docker package repository
sudo wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.cloud.aliyuncs.com/docker-ce/linux/centos/docker-ce.repo
sudo sed -i 's|https://mirrors.aliyun.com|http://mirrors.cloud.aliyuncs.com|g' /etc/yum.repos.d/docker-ce.repo
# Install Docker CE, containerd.io, and the Buildx and Compose plugins
sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginStep 3: Start Docker
sudo systemctl start docker
sudo systemctl enable dockerVerify the service is active:
sudo systemctl status dockerExpected output:
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled)
Active: active (running) since ...Deploy a web application with Docker
This section walks through building a simple Nginx-based web app and running it as a container.
Build the web application image.
# Pull the latest Nginx image sudo docker pull nginx:latest # Create a Dockerfile using Nginx as the base image, # serving a simple "Hello World!" page sudo tee Dockerfile <<-'EOF' FROM nginx:latest RUN echo 'Hello World!' > /usr/share/nginx/html/index.html EOF # Build the image sudo docker build . -t hello-world:latestStart a container named
hello-worldfrom the image, mapping port 80.sudo docker run -d -p 80:80 --name hello-world hello-world:latestVerify the web application is running.
curl http://localhostThe expected output is:
Hello World!
Install Docker Compose
Docker Compose is included in the docker-compose-plugin package installed in the previous steps. If you need to install it separately, run the following command for your OS.
If you followed the steps in this topic to install Docker, the Compose plugin is installed by default. Verify the installation by running the following command.
sudo docker compose versionExpected output (where vN.N.N is the version): Docker Compose version vN.N.N.
If the Compose plugin was not installed, or if you prefer to use standalone Compose (docker-compose), see the following sections.
When using standalone Compose, include a hyphen in commands, for example,
docker-compose up.When using the Compose plugin, run commands without a hyphen, for example,
docker compose up.
Compose plugin
Alibaba Cloud Linux 3, CentOS 8.x, and Fedora
sudo dnf -y install docker-compose-pluginUbuntu and Debian
sudo apt-get -y install docker-compose-pluginAlibaba Cloud Linux 2, CentOS 7.x, Red Hat, and Anolis OS
sudo yum -y install docker-compose-pluginVerify the installation by checking the Compose version.
sudo docker compose versionStandalone Compose
Download and install standalone Compose.
NoteYou might encounter connection timeouts when accessing GitHub due to network issues. If this occurs, try again.
curl -SL https://github.com/docker/compose/releases/download/v2.30.3/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose # Apply executable permissions to the binary. sudo chmod +x /usr/local/bin/docker-compose sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-composeTest the installation by checking the version.
sudo docker-compose version
Deploy an application with Docker Compose
This section shows how to use Docker Compose to run a WordPress site backed by a MySQL database.
Create a Compose file named
docker-compose.yaml.sudo tee docker-compose.yaml <<-'EOF' version: '3.1' services: wordpress: image: wordpress restart: always ports: - "80:80" environment: WORDPRESS_DB_HOST: wordpress_db WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: ${WORDPRESS_DB_PASSWORD} WORDPRESS_DB_NAME: wordpress volumes: - wordpress:/var/www/html db: image: mysql:5.7 restart: always container_name: wordpress_db environment: MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: ${WORDPRESS_DB_PASSWORD} MYSQL_RANDOM_ROOT_PASSWORD: ${WORDPRESS_DB_PASSWORD} volumes: - db:/var/lib/mysql volumes: wordpress: db: EOFStart the services.
export WORDPRESS_DB_PASSWORD=<your-db-password> sudo -E docker compose up -dOpen
https://<public IP address of the Simple Application Server instance>in a browser. The WordPress setup page appears. Follow the on-screen instructions to complete the configuration.
FAQ
How do I run Docker without sudo?
By default, Docker commands require root privileges. To run Docker as your current user, add the user to the docker group.
Add the current user to the
dockergroup.sudo usermod -aG docker $USERApply the group change to the current shell session.
newgrp dockerVerify that your user is now in the
dockergroup.groupsThe output should include
docker:<your-username> sudo dockerConfirm you can run Docker without
sudo.docker run hello-worldA success message confirms Docker is working correctly for your user. For the change to persist across login sessions, log out and log back in.
Why is http://mirrors.cloud.aliyuncs.com inaccessible?
If you see a connection failure when accessing http://mirrors.cloud.aliyuncs.com, the possible cause is network instability. Replace http://mirrors.cloud.aliyuncs.com with https://mirrors.aliyun.com in all commands.
https://mirrors.aliyun.com requires your instance to have a public IP address. See Enable Internet access.Why does the dnf config-manager command fail?
If you see a DeprecationWarning about distutils when running dnf config-manager, your dnf version is outdated. Update it and retry:
sudo dnf update dnfWhy does dnf install docker-ce fail?
Symptom: download failure
containerd.io-1.6.26-3.1.el8.x86_64: Cannot download, all mirrors were already tried without success.The package cache is stale. Clear it and retry:
sudo dnf clean packagesSymptom: metadata download failure on CentOS 8
Errors during downloading metadata for repository 'base':
- Curl error (6): Couldn't resolve host name for http://mirror.centos.org/...
Error: Failed to download metadata for repo 'base'The default CentOS 8 repository is discontinued. Replace it with the Alibaba Cloud-provided archive:
# Back up the existing repo file
sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# Download the Alibaba Cloud CentOS vault repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.aliyuncs.com/repo/Centos-vault-8.5.2111.repo
# Update the mirror addresses
sudo sed -i 's/mirrors.cloud.aliyuncs.com/url_tmp/g' /etc/yum.repos.d/CentOS-Base.repo \
&& sudo sed -i 's/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g' /etc/yum.repos.d/CentOS-Base.repo \
&& sudo sed -i 's/url_tmp/mirrors.aliyun.com/g' /etc/yum.repos.d/CentOS-Base.repo
# Rebuild the cache
sudo yum clean all && sudo yum makecacheThen retry the Docker installation.
What's next
Artifact Center: Free, secure base container images from Alibaba Cloud and the OpenAnolis community.
Use P2P acceleration in other container environments: Speed up image pulls with enterprise P2P acceleration.
Run Cloud Assistant CLI in a Docker container: Manage Alibaba Cloud resources from within a container.