Windows(WSL2) Linux subsystem builds Docker environment
www.cnblogs.com/itqn/p/1383…
Start a Redis
docker run -it --rm -p 6379:6379 redis:alpine redis-server --requirepass "123123"
Copy the code
Docker container is used
- Access to the mirror
docker pull ubuntu
Copy the code
- Start the container
Start a container with an Ubuntu image and enter the container in command line mode:
docker run -it ubuntu /bin/bash
Copy the code
-i: interactive operation -t: terminal
- View all containers
docker ps -a
Copy the code
Output details:
CONTAINER ID: indicates the ID of a CONTAINER.
IMAGE: IMAGE used.
COMMAND: The COMMAND that is run when the container is started.
CREATED: time when the container was CREATED.
STATUS: indicates the STATUS of the container.
There are seven states: created (created)
Restarting
Running or Up
Removing (on the move)
Paused
Exited (stop)
He was dead.
PORTS: port information of the container and connection type used (TCP \ UDP).
NAMES: automatically assigned container NAMES.
- View the running container
docker ps
Copy the code
- View the standard output in the container
Docker logs < container ID>Copy the code
- Start a stopped container
Docker start < container ID>Copy the code
- Specifies the container to run in the background
docker run -itd --name ubuntu-test ubuntu /bin/bash
Copy the code
-d The container is not entered by default
- Stopping a container
Docker stop < container ID>Copy the code
- Restart the container
Docker restart < container ID>Copy the code
- Into the container
With the -d argument, the container goes into the background after it starts.
Exiting from the container causes the container to stop.
Docker attach < container ID>Copy the code
Exiting the container terminal does not cause the container to stop.
docker exec< ID > containerCopy the code
- Export container
docker export 1e560fca3906 > ubuntu.tar
Copy the code
- Import the container
Import the snapshot file ubuntu.tar to the image test/ Ubuntu :v1
cat docker/ubuntu.tar | docker import - test/ubuntu:v1
Copy the code
Specify a URL or a directory to import
docker import http://example.com/exampleimage.tgz example/imagerepo
Copy the code
- Remove the container
Docker rm -f < container ID>Copy the code
- Clean up all terminating containers
docker container prune
Copy the code
Run a Web application
Run a Python Flask application in a Docker container to run a Web application
docker pull training/webapp # Load image
docker run -d -P training/webapp python app.py
Copy the code
-d: Allows the container to run in the background. -p: randomly maps the network ports used inside the container to the hosts we use.
- View the Web application container
View a running container:
docker ps
Copy the code
PORTS: indicates the port information
0.0.0.0:49153 – > 5000 / TCP, : : : 49153 – > 5000 / TCP
Docker opens port 5000 (default Python Flask port) mapped to host port 32769.
Then we can through the browser to access WEB applications at http://192.168.31.127:49153/
We can also set different ports with the -p parameter:
docker run -d -p 5000:5000 training/webapp python app.py
Copy the code
View the running container
docker ps
Copy the code
Port 5000 inside the container maps to port 5000 on our localhost.
http://192.168.31.127:5000/
- View the shortcut to the network port
Docker port < container ID>Copy the code
- View WEB application logs
View the standard output inside the container.
Docker logs < container ID>Copy the code
-f: Let docker logs output internal container standard output as if using tail -f.
Docker logs -f < container ID>Copy the code
- View the processes of the WEB application container
Docker top < container ID>Copy the code
- Check the WEB application
Use Docker inspect to view the underlying information of docker. It returns a JSON file that records the configuration and status of the Docker container.
Docker inspect < container ID>Copy the code
- Stop the WEB application container
Docker stop < container ID>Copy the code
- Restart the WEB application container
Start the stopped container
Docker start < container ID>Copy the code
Restart the running container
Docker restart < container ID>Copy the code
- Remove the WEB application container
Docker restart < container ID>Copy the code
When deleting a container, the container must be stopped; otherwise, the following error will be reported:
Error response from daemon: You cannot remove a running container 4d8a355def18f5a13b3d933e918c2e2a7e383bd8792671a6f246b1f21d05cb1a. Stop the container before attempting removal or force remove
Copy the code
Use of Docker images
- Listing mirrors
docker images
Copy the code
Description of each option:
REPOSITORY: represents the REPOSITORY source of the image
TAG: indicates the TAG of the mirror
IMAGE ID: indicates the ID of an IMAGE
CREATED: indicates the time when a mirror is CREATED
SIZE: indicates the mirror SIZE
- Get a new image
Docker pull ubuntu: 20.04Copy the code
- Find the mirror
docker search httpd
Copy the code
NAME: indicates the NAME of the mirror repository source
DESCRIPTION: indicates the DESCRIPTION of the mirror
OFFICIAL: Is it OFFICIAL with Docker
Stars: Similar to Github star, which means to like or like.
AUTOMATED: Automatic construction.
- Drag take mirror
docker pull httpd
Copy the code
- Remove the mirror
docker rmi hello-world
Copy the code
- Create a mirror image
When the image we download from the Docker image repository does not meet our needs, we can change the image in the following two ways.
Update the image from the created container and commit the image
Use the Dockerfile directive to create a new image
- Update image
Before updating the image, we need to create a container using the image.
Docker run -t -i Ubuntu :20.04 /bin/bashCopy the code
Use apt-get update in the running container. After completing the operation, type the exit command to exit the container. At this time, the container with ID F5A234B2E470 is the container changed according to our requirements. We can commit a container copy by using the docker commit command.
sudo docker commit -m="has update" -a="tang" f5a234b2e470 tang/ubuntu:v2
Copy the code
Parameter description:
-m: indicates the description to be submitted
-a: Specifies the mirror author
F5a234b2e470: indicates the container ID
Tang/Ubuntu :v2: Specifies the name of the target image to be created
We can use the Docker images command to view our new image tang/ Ubuntu :v2
Use our new image Tang/Ubuntu to start a container
docker run -t -i tang/ubuntu:v2 /bin/bash
Copy the code
- Build the mirror
We use the command docker build to create a new image from scratch. To do this, we need to create a Dockerfile file that contains a set of instructions that tell Docker how to build our image.
touch Dockerfile
Copy the code
The FROM centos: 6.7 MAINTAINER Fisher "[email protected]" RUN/bin/echo 'root: 123456' | chpasswd RUN useradd runoob RUN /bin/echo 'tang:123456' |chpasswd RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local EXPOSE 22 EXPOSE 80 CMD /usr/sbin/sshd -DCopy the code
Each instruction creates a new layer on the mirror, and the prefix of each instruction must be uppercase.
The first FROM specifies which mirror source to use
The RUN directive tells Docker to execute commands inside the image, install what…
We then use the Dockerfile file to build an image using the Docker build command.
Docker build -t runoob/centos:6.7Copy the code
Parameter Description:
-t: specifies the name of the target mirror to be created
. : indicates the directory where Dockerfile resides. You can specify the absolute path of Dockerfile
We can use the new image to create the container
Docker run -t I runoob/centos:6.7 /bin/bash id TangCopy the code
You can see from above that the new image already contains the user Tang we created.
- Setting a Mirror Label
We can use the docker tag command to add a new tag to the image.
docker tag 860c279d2fec runoob/centos:dev
Copy the code