This topic describes how to deploy Docker CE on Linux instances, as well as common Docker operations and the image creation process.
About Docker
Docker is an open-source application container engine. It packages applications and their dependencies into virtual containers, which can run on any computer with Linux, Windows, or macOS. This capability allows applications to operate seamlessly across diverse environments, including local systems, public clouds, and private clouds. For more information, visit Docker docs.
The following items describe the main components of Docker:
Execute the following command to check whether Docker is installed as expected.
docker info
If the output is as shown below, Docker has been successfully installed.
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.
Manage mirror sites
When image pulls from the official Docker repository fail, you can configure one or multiple reliable and available mirror sites. Docker will pull images from the mirror site you specify, improving pull reliability and speed.
Edit the Docker configuration file.
sudo vim /etc/docker/daemon.json
Press i to enter edit mode, and enter the mirror site you need in the registry-mirrors structure.
tip
Replace https://registry-1.docker.io and https://registry-2.docker.io with the mirror sites you need. Separate multiple sources with ,.
{
"registry-mirrors":[
"https://registry-1.docker.io",
"https://registry-2.docker.io"
]
}
Press the Esc key to exit edit mode, enter :wq, and then press Enter to save and exit the file.
Execute the following command to restart the Docker service.
In the example provided in this topic, the Ubuntu image is used.
docker pull ubuntu
View the existing images
docker images
Forcibly delete an image
docker rmi -f ubuntu
After you delete an image, you can execute the docker images command to check whether the image is deleted.
Manage containers
Enter a container
docker run -it <ImageId> /bin/bash
You can execute the docker images command to get <ImageId> in the preceding command.
Exit a container
Execute the exit command to exit the current container.
Run a container in the background
docker run -d --name ubuntu ImageId
tip
The --name option specifies ubuntu as the container name.
Enter a container running in the background
docker exec -it <Container ID> /bin/bash
tip
Replace <Container ID> with the actual container ID, which you can obtain by executing the docker ps command.
Create an image from a container
docker commit <Container ID OR Container Name> [<Repository Name>[:<Tags>]]
tip
Replace <Container ID OR Container Name> with the actual container ID or name, <Repository Name> with the actual repository name, and <Tags> with the actual repository tags. Example: docker commit 097fdd5e**** dockertest/ubuntu:latest.
Create an image
Execute the following command to create and edit a Dockerfile.
vim Dockerfile
Press i to enter edit mode and add the following content.
The following items describe simple instructions in the Dockerfile. For more information, visit the official website.
FROM statement: Indicates that the Nginx image is used as the base image. In a Dockerfile, the FROM instruction is required and must be the first instruction.
RUN statement: The format is RUN <command>. For example, if the command in the statement is echo "Hello, Docker!", Hello, Docker! is displayed after you execute the statement.
Press the Esc key, enter :wq, and then press Enter to save and exit the Dockerfile.
Execute the following command to create an image.
docker build -t nginx:v1 . # . specifies the path to the Dockerfile and cannot be omitted.
docker images # Check whether the image is created.
Execute the following commands to run and view the containers.
docker run -d nginx:v1 # Run containers in the background.
docker ps # View currently running containers.
docker ps -a # View all containers, including containers that are not running.
docker logs CONTAINER ID/NAMES # If no running containers are displayed, use the container ID or name to check the startup logs for troubleshooting purposes.
Execute the following command to create an image.
docker commit bd0d3e7e**** nginxtest:v1 # Add the container ID, along with the name and version number of the new image, after the commit parameter.
docker images # List local (downloaded and locally created) images.
Push the image to the remote repository.
By default, the image is pushed to Docker Hub. You need to first log in to Docker, associate tags with the image, and name the image in the format of [Docker User Name]/[Image Name]:[Tag]. The push is complete.
docker login
docker tag [ImageId] [Docker User Name]/[Image Name]:[Tag]
docker push [Docker User Name]/[Image Name]:[Tag]
tip
Replace [Docker User Name] with your actual Docker username, [Image Name] with the actual image name, and [Tag] with the actual image tag.