Two ways to create containers in docker (interactive and guarded)
When using Docker, there are two common ways to create containers: the interactive way and the daemonized way. This article will introduce these two methods and related commands in detail, and illustrate them with specific cases.
Create containers interactively
Creating a container interactively means starting an interactive session inside the container, similar to logging into a virtual machine in a terminal. You can interact directly with the container and execute commands inside the container.
Here are the steps and related commands to create a container interactively:
Step 1: Pull the image
First, we need to pull the required images from Docker Hub or a private repository. For example, we will use the centos image as an example:
docker pull centos
Step 2: Create the container
Next, use docker run
the command to create an interactive container. Here is the command to create the container:
docker run -it --name <container_name> <image_name> /bin/bash
<container_name>
: Specifies the name of the container.<image_name>
: Specify the image name to use.
For example, we create a mycentos2
container named and use the centos image:
docker run -it --name mycontainer ubuntu /bin/bash
After the creation is successful, enter the container, and then you can see that there are many things in the container
Enter exit to exit
After exiting, if you want to run again, you can enter the following command
First enter docker start <容器ID或名称>
the start container
and then enter the docker exec -it <容器ID或名称> /bin/bash
run container to see all the containers, and then the mycentos2 container is running.
docker ps -a
Step 3: Interact with the container
You are now at the command line interface of the interactive container. You can execute arbitrary commands inside the container, just like in a local terminal. For example, run the following command to see a list of files inside the container:
ls
You can also install packages, edit files, and more.
Step 4: Exit the container
When you're done working inside the container, you can exit the container and return to the host system's command-line interface with the following command:
exit
Daemonized way to create containers
Creating a container in daemon mode means running the container in the background without entering the container for interaction. This approach is suitable for long-running services or applications.
The following are the steps and related commands to create a container in daemon mode:
Step 1: Pull the image
Same as the interactive method, you first need to pull the required image from Docker Hub or a private warehouse.
Step 2: Create the container
docker run
Create a daemonized container using the command. Here is the command to create the container:
docker run -d --name <container_name> <image_name> <command>
<container_name>
: Specifies the name of the container.<image_name>
: Specify the image name to use.<command>
: Specifies the command to execute when the container starts.
For example, we create a mycentos4
container named and use the centos image to run a simple centos server:
docker run -di --name=mycentos4 centos:latest
Step 3: View container status
You can view the status of the container with the following command:
docker ps
Step 4: Interact with the container (optional)
If you need to interact with the container, you can enter the container's interactive session with the following command:
docker exec -it <container_name> /bin/bash
Step 5: Stop and delete the container
To stop a daemon container, the following command can be used:
docker stop <container_name>
To delete a container, the following command can be used:
docker rm <container_name>