Help Center> Cloud Container Instance> Best Practices> Workload Creation> Using docker run Commands to Run Containers
Updated on 2024-01-16 GMT+08:00

Using docker run Commands to Run Containers

Docker is an open source engine that manages images and containers. A Docker image includes all dependencies required for running an application. The processes contained in the image are isolated from each other.

Docker containers are built on Docker images.

Preparing Images

WordPress and MySQL images are general-purpose images and can be obtained from the container registry.

You can run the docker pull command on the device where the container engine is installed to download images.

docker pull mysql:5.7
docker pull wordpress

Run the docker images command to view the images. As shown in the following figure, two images exist on the local host.

Running Containers

You can use the container engine to run the WordPress and MySQL containers, and use the --link parameter to connect the two containers. In this way, the WordPress container can access the MySQL container without code changes.

Run the following command to run the MySQL container:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=******** -e MYSQL_DATABASE=wordpress -d mysql:5.7

The parameters are described as follows:

  • --name: specifies the container name as some-mysql.
  • -e: specifies the environment variable of the container. In this example, the value of MYSQL_ROOT_PASSWORD is ******** (replace ******** with your password). The environment variable MYSQL_DATABASE indicates the name of the database to be created when the image is started. Its value is wordpress in this example.
  • -d: indicates that the container runs in the backend.

Run the following command to run the WordPress container:

docker run --name some-wordpress --link some-mysql:mysql -p 8080:80 -e WORDPRESS_DB_PASSWORD=******** -e WORDPRESS_DB_USER=root -d wordpress

The parameters are described as follows:

  • --name: specifies the container name as some-wordpress.
  • --link: connects the some-wordpress container to the some-mysql container and changes the name of the some-mysql container to mysql. --link provides an easy way to connect two containers. Alternatively, you can configure the environment variable WORDPRESS_DB_HOST of the some-wordpress container to access the IP address and port of the mysql container.
  • -p: specifies ports for mapping. In this example, port 80 of the container is mapped to port 8080 of the host.
  • -e: specifies the environment variable of the container. In this example, the value of WORDPRESS_DB_PASSWORD is ******** (replace ******** with your password). The value of WORDPRESS_DB_PASSWORD must be the same as that of MYSQL_ROOT_PASSWORD because the WordPress requires a password to access the MySQL database. WORDPRESS_DB_USER indicates the username for accessing the MySQL database. Set it to root.
  • -d: indicates that the container runs in the backend.

After the WordPress runs, you can access WordPress blogs through http://127.0.0.1:8080.