Docker Tutorial Series 2



Manually run a container from the mirror

Start a container manually

Manually run a web application in the docker container of the local computer

``` sh
    #The following command starts a docker container
    docker run -d -t -p 8000:5000 --name demo ubuntu:18.04

    #The interpretation of the order
    -D means to let the container run in the background
    -T is rarely used to make a blank Ubuntu image run in the background
    -P is used to specify the port mapping, which means that the local access to 8000 will be automatically transferred to port 5000 in the container
       You must ensure that no other application on this machine occupies port 8000, otherwise it will fail here
    --Name demo defines the name of the container as demo
    ubuntu:18.04  Is the image name used when starting the container. Docker will automatically download the image from the image server

Common problems of starting container

#Start a process with port 3456
    docker run -t -d -p 3456:5000 --name test ubuntu:18.04
    #Error 1, port occupied, delete test container to avoid 3456 port occupied
    docker rm -f test
    #Error 2, the name already exists, then change the name, you can also use the above delete container command
    docker run -t -d -p 3456:5000 --name test1 ubuntu:18.04

About container related commands

#Lists the containers that this machine is running
    docker container ls

    #Lists all containers on the machine, including those that terminate operation
    docker container ls --all

    #View running containers
    docker ps

    #View the status of the latest specified number of containers
    docker ps -n 10

    #Terminate container
    docker stop <container id>/<container name>

    #Restart the terminated container, and query the container ID through docker PS
    docker start <container id>/<container name>

    #View container log
    docker logs <container id>/<container name

Install prerequisite software in container

After starting the container, it shows that a mirror instance is running, such as ubuntu:18.04 Test container generated by image. This container has the file management function, memory management function and process scheduling function of Ubuntu operating system. Now you need to have Python 3, PIP environment and apt toolkit.

    docker run -d -t -p 8000:5000 --name unbuntu:18.04
    #Download apt Toolkit
    docker exec demo apt update
    docker exec apt -y install python3 python3-pip
    #Install flash Library
    docker exec demo pip3 install flask

Running a program in a container

As mentioned above, the purpose of using docker is to install software application with environment. Now that the environment is good, you need to run the software application

    #Create / code directory in demo container
    docker exec demo mkdir /code
    #CP parameter copies of the current folder to / code / of the demo container
    docker cp demo:/code/
    #Run in the demo container
    docker exec demo python3 /code/

Configure the container by script, then run the script to configure and open the new container

In fact, a better way is to write a script on the host and run it directly in the docker container
See the example operation for specific operation

Instance operation

  1. Create file in the current directory

    from flask import Flask
     app = Flask(__name__)
     def index():
         return 'Hello from Docker'
     #Default port 5000
     if __name__ == "__main__":"", debug=True)
  2. Create the file in the current directory

     apt update
     apt -y install python3 python3-pip
     pip3 install flask
  3. Create the file in the current directory

     cd /code
  4. Open the terminal and enter the following command

    docker run -d -t -p 8000:5000 --name unbuntu:18.04
     #Create / code directory in demo container
     docker exec demo mkdir /code
     docker cp "demo:/code/"
     docker cp "demo:/code/"
     docker cp "demo:/code/"
     docker exec demo bash /code/
     docker exec demo bash /code/
  5. Open the browser and enter: localhost:8000/