Home > Operation and Maintenance > Docker > How to start docker

How to start docker

PHPz
Release: 2023-04-04 10:02:33
Original
23668 people have browsed it

Docker is a containerization engine provided by the Linux kernel. It can run multiple containers on the same host. These containers are isolated from each other but can share the host's resources, including CPU, memory, network, etc. Docker is increasingly used by enterprises, developers, and operation and maintenance personnel because it can help us quickly build and deploy applications, greatly improving development efficiency and operation and maintenance efficiency. But how do you start a Docker container? This article will introduce how to start Docker.

1. Install Docker

Before starting Docker, you must first ensure that Docker has been installed. You can check whether Docker has been installed by running the following command:

docker --version
Copy after login

If it is already installed Docker will return the version number of Docker. If it is not installed, you need to install Docker first. On different operating systems, the methods of installing Docker may be slightly different. Here is an example of installing Docker on the Ubuntu operating system:

  1. Update software package:
sudo apt-get update
Copy after login
  1. Install Docker:
sudo apt-get install docker-ce
Copy after login
  1. Verify whether Docker is installed successfully:
sudo docker run hello-world
Copy after login

If the hello-world container can be run successfully, Docker is installed successfully. .

2. Start the Docker container

The startup process of the Docker container is very simple, just execute the following command:

docker run [OPTIONS] IMAGE [COMMAND] [ARGS...]
Copy after login

Among them, OPTIONS is optional, you can Specify some attributes of the container, such as port mapping, container name, etc.; IMAGE is required and specifies the image to be started; COMMAND is optional and specifies the command to be executed after the container is started; ARGS is optional and specifies the parameters of the COMMAND command.

The following is an example of starting an nginx container:

  1. Search for nginx image:
sudo docker search nginx
Copy after login
  1. Download nginx image:
sudo docker pull nginx
Copy after login
  1. Start the nginx container:
sudo docker run -d -p 8080:80 --name mynginx nginx
Copy after login

Among them, -d means starting the container in daemon (background) mode, -p means mapping the host's port 8080 to the container's port 80, --name means the name of the specified container, nginx means the image to be started.

  1. Check whether the container is started successfully:
sudo docker ps
Copy after login

If the information about the container just started is output, it means the container is started successfully.

3. Entering the Docker container

In some cases, you need to enter the Docker container to perform operations, such as viewing the file system inside the container, running commands, etc. You can enter the Docker container through the following command:

sudo docker exec -it CONTAINER_NAME COMMAND
Copy after login

Among them, -it means starting the container process in interactive mode and allocating a pseudo terminal, CONTAINER_NAME means the name of the container, and COMMAND is the command to be executed.

For example, execute the ls command inside the nginx container started above:

sudo docker exec -it mynginx ls
Copy after login

4. Stop the Docker container

When the running container is no longer needed , you can stop the container through the following command:

sudo docker stop CONTAINER_NAME
Copy after login

where CONTAINER_NAME represents the name of the container to be stopped.

If you want to stop the nginx container you just started, you can execute the following command:

sudo docker stop mynginx
Copy after login

5. Delete the Docker container

When the container is no longer needed, the space it occupies needs to be released. resources, you can delete the container through the following command:

sudo docker rm CONTAINER_NAME
Copy after login

where CONTAINER_NAME represents the name of the container to be deleted.

If you want to delete the nginx container you just started, you can execute the following command:

sudo docker rm mynginx
Copy after login

Summary

This article introduces the installation of Docker and how to start, enter, stop and delete Docker container. The use of Docker can greatly improve the efficiency of application development, testing and deployment, but it should be noted that the startup and shutdown sequence of Docker is very important. If there are dependencies between the started containers, the dependent container needs to be started first. , and then start the dependent containers to ensure interoperability between containers.

The above is the detailed content of How to start docker. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template