preface

Containers are one of the three core concepts of Docker. Simply put, a container is a single application or group of applications that run independently, along with their runtime environment. A virtual machine, in turn, can be understood as a simulation of a running operating system (providing a running environment and other system environments) and the applications running on it. Through this article, we will talk about some common commands of Docker container, such as create, stop, restart, pause and so on.

Create a container

The new container

The command is as follows:

docker create <image>
Copy the code

The container created using the create command is stopped by default, requiring us to start the container manually.

Create and run the container

The command is as follows:

docker run <image>
Copy the code

Containers created by the run command are run by default.

Common parameters are as follows:

-i keeps the container’s standard input open

-t Assigns a dummy terminal

-d the container is running in the daemon

–name Sets the container name

-P can map the host port to the container port. For example, -p 8080:8081 indicates the host port on the left and container port on the right

-v can be mounted from the host directory to the container directory, for example, -v /data:/ TMP /data. The left side is the host directory and the right side is the container directory.

For example, a command that actually runs the container:

docker run -it -d --name my_ubuntu -p 8088:22 -v /root/ubuntu_data:/data ubuntu:latest /bin/bash
Copy the code

Container management

View a list of containers

The command is as follows:

docker ps 
Copy the code

Docker ps is to view the running containers. You can add the -a parameter to view the information of all containers.

Stop the container

Stop container command:

docker stop <container_name/id>
Copy the code

Start the container

docker start <container_name/id>
Copy the code

Restart the container

docker restart <container_name/id>
Copy the code

Remove the container

The command is as follows:

docker rm <container_id/name>
Copy the code

If the container is running, a message is displayed indicating that the deletion fails. You can use the -f parameter to forcibly delete the container. For example, to delete a running my_Ubuntu container:

docker rm -f my_ubuntu

Copy the code

Container log

You can run the following command to view the container logs:

docker logs <id/name>

Copy the code

Suspension of the container

docker pause <id/name>

Copy the code

You can view the status of the container by using the docker ps command

Resume paused containers

docker unpause <id/name>

Copy the code

Into the container

If we create the container with the -d argument, the container will run in the background. So, we can connect to the container using the Docker attach command and the Docker exec command.

Docker attach command

The docker attach command is used as follows:

docker attach <container_name/id>

Copy the code

For example, I’ve created a container called my_Ubuntu here.

docker attach my_ubuntu

Copy the code

When you enter the container, you will see the same effect as if you were logging in to the server using SSH:

Docker attach has a fatal shortcoming, that is, when multiple Windows are attached to the same container at the same time, all Windows will be displayed synchronously. When one window is blocked, all Windows will be blocked. For example, IF I attach to the my_Ubuntu container from two terminals, all operations will be synchronized:

When I type something in one window, the other window synchronizes.

Because of this, the ATTACH command is rarely used.

Docker exec command

Docker Exec does not inherently connect containers, but instead executes commands inside containers, as follows:

docker exec -it <container_name/id> <exec>

Copy the code

Where <exec> is the command that needs to be executed, such as /bin/bash under my_Ubuntu:

docker exec -it my_ubuntu /bin/bash

Copy the code

Execution Result:

The /bin/bash command runs a bash terminal, so we can connect to the container by running the /bin/bash command inside the container.

Viewing container changes

The container also supports viewing the changed contents, using the following command:

docker diff <name/id>

Copy the code

For example, check my_Ubuntu’s changes:

There are three symbols. They are:

Submit the container

Like Git, Docker can commit changes as new images. The command is as follows:

docker commit <container_id/name> repo/name:tag

Copy the code

For example, IF I submit my_Ubuntu locally:

docker commit my_ubuntu test/ubuntu:personal

Copy the code

Then look at the image:

After submitting the image, you can push it to the warehouse for testing and deployment.

conclusion

As one of the three core concepts of Docker, container has many operations, and it is difficult to list one by one in this paper. It just lists some commands that I think are commonly used.