Docker Container Usage

Docker Container Usage

Docker Client

The docker client is very simple , we can directly type docker command to see all the command options of the Docker client.

apidemos@apidemos:~# docker

The command docker command –help can be used to get more insight into how to use the specified Docker command.

For example, to see how to use the docker stats command, we would use

apidemos@apidemos:~# docker stats --help

container use

Get the image

If we don’t have an ubuntu image locally, we can use the docker pull command to load the ubuntu image: “`shell

$ docker pull ubuntu

Start the container

The following command starts a container using the ubuntu image, with the following argument to enter the container in command line mode.

$ docker run -it ubuntu /bin/bash

Docker Container Usage

Parameter description.

  • -i: Interactive operation.
  • -t: terminal.
  • ubuntu: The ubuntu image.
  • /bin/bash: The command that is placed after the image name, here we want an interactive shell, so we use /bin/bash.

To exit the terminal, just type exit:

root@ed09e4490c57:/# exit

Docker Container Usage

Start a container that has stopped running

The View All Containers command is as follows.

$ docker ps -a

Docker Container Usage

Start a stopped container using docker start.

$ docker start b750bbbcfd88 

Docker Container Usage

Running in the background

In most scenarios, we want docker’s services to run in the background, and we can specify the container’s run mode with -d.

$ docker run -itd --name ubuntu-test ubuntu /bin/bash

Docker Container Usage

Docker Container Usage

Note: Added -d parameter will not enter the container by default, you need to use the command docker exec to enter the container (will be described below).

Stopping a container

The command to stop a container is as follows.

$ docker stop <container ID>

Docker Container Usage

A stopped container can be restarted by docker restart:.

$ docker restart <container ID>

Docker Container Usage

Enter the container

When using the -d parameter, the container will enter the background after starting. To enter the container at this point, you can do so with the following command.

  • docker attach
  • docker exec: It is recommended that you use the docker exec command, because this command will exit the container terminal, but will not cause the container to stop.

attach command.

The following demonstrates the use of the docker attach command.

$ docker attach 1e560fca3906 

Docker Container Usage

Note: If you exit from this container, it will cause the container to stop.

exec command

The following demonstrates the use of the docker exec command.

docker exec -it 243c32535da7 /bin/bash

Docker Container Usage

Note: If you exit from this container, the container will not stop, which is why it is recommended to use docker exec.

For more parameter descriptions, please use the docker exec –help command to see.

Exporting and importing containers

export container

To export a local container, you can use the docker export command.

$ docker export 1e560fca3906 > ubuntu.tar

Export the container 1e560fca3906 snapshot to the local file ubuntu.tar.

Docker Container Usage

This will export the container snapshot to a local file.

Importing container snapshots

You can use docker import to import from a container snapshot file to a mirror, as in the example below, which imports the snapshot file ubuntu.tar to the mirror test/ubuntu:v1:

$ cat docker/ubuntu.tar | docker import - test/ubuntu:v1

Docker Container Usage

Alternatively, it can be imported by specifying a URL or a directory, e.g.

$ docker import http://example.com/exampleimage.tgz example/imagerepo

Deleting a container

To delete a container use the docker rm command.

$ docker rm -f 1e560fca3906

Docker Container Usage

The following command cleans up all containers that are in the terminated state.

$ docker container prune

Running a web application

The container we ran earlier wasn’t of some special use.

Next let’s try building a web application using docker.

We will run a Python Flask application in a docker container to run a web application.

apidemos@apidemos:~# docker pull training/webapp # Load image
apidemos@apidemos:~# docker run -d -P training/webapp python app.py

Parameter description:

  • -d: Leave the container running in the background.
  • -P: Map the network port used inside the container to a random host we use.

View WEB application containers

Use docker ps to view the containers we are running in.

apidemos@apidemos:~# docker ps
CONTAINER ID IMAGE COMMAND ...        PORTS                 
d3d5e39ed9d3 training/webapp "python app.py" ...        0.0.0.0:32769->5000/tcp

More port information here.

PORTS
0.0.0.0:32769->5000/tcp

Docker opens up port 5000 (the default Python Flask port) to be mapped to the host port 32769.

At this point we can access the web application through the browser

Docker Container Usage

We can also set a different port with the -p parameter: “The

apidemos@apidemos:~$ docker run -d -p 5000:5000 training/webapp python app.py

docker ps to see the running containers

apidemos@apidemos:~# docker ps
CONTAINER ID IMAGE PORTS NAMES
bf08b7f2cd89 training/webapp ...        0.0.0.0:5000->5000/tcp wizardly_chandrasekhar
d3d5e39ed9d3 training/webapp ...        0.0.0.0:32769->5000/tcp xenodochial_hoov

The 5000 port inside the container is mapped to the 5000 port on our local host.

Shortcut to network ports

The docker ps command allows you to see the port mapping of the container. docker also provides another shortcut docker port, using docker port you can see the port number of a given (ID or name) container with a certain port mapped to the host.

The web application container ID we created above is bf08b7f2cd89 and the name is wizardly_chandrasekhar.

I can use docker port bf08b7f2cd89 or docker port wizardly_chandrasekhar to see how the container ports are mapped.

apidemos@apidemos:~docker port bf08b7f2cd89
5000/tcp -> 0.0.0.0:5000
apidemos@apidemos:~ docker port wizardly_chandrasekhar
5000/tcp -> 0.0.0.0:5000

View WEB application logs

docker logs [ID or name] allows you to view the standard output from inside the container.

apidemos@apidemos:~$ docker logs -f bf08b7f2cd89
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.239.1 - - [09/May/2016 16:30:37] "GET / HTTP/1.1" 200 -
192.168.239.1 - - [09/May/2016 16:30:37] "GET /favicon.ico HTTP/1.1" 404 -

-f: Let docker logs output the standard output inside the container like you would with tail -f.

From above, we can see that the application is using port 5000 and can see the application access logs.

View the processes of the WEB application container

We can also use docker top to see the processes running inside the container

apidemos@apidemos:~$ docker top wizardly_chandrasekhar
UID PID PPID ...       TIME CMD
root 23245 23228 ...       00:00:00 python app.py

Inspecting WEB applications

Use docker inspect to see the underlying information of Docker. It will return a JSON file recording the configuration and status information of the Docker container.

apidemos@apidemos:~$ docker inspect wizardly_chandrasekhar
[
    {
        "Id": "bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85",
        "Created": "2018-09-17T01:41:26.174228707Z",
        "Path": "python",
        "Args": [
            "app.py"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 23245,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-09-17T01:41:26.494185806Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
......

Stop the WEB application container

apidemos@apidemos:~$ docker stop wizardly_chandrasekhar   
wizardly_chandrasekhar

Restart the WEB application container

The containers that have been stopped, we can start using the command docker start.

apidemos@apidemos:~$ docker start wizardly_chandrasekhar
wizardly_chandrasekhar

docker ps -l Query the last created container.

# docker ps -l 
CONTAINER ID IMAGE PORTS NAMES
bf08b7f2cd89 training/webapp ...        0.0.0.0:5000->5000/tcp wizardly_chandrasekhar

The running container, we can restart it using the docker restart command.

Remove the WEB application container

We can use the docker rm command to remove unwanted containers

apidemos@apidemos:~$ docker rm wizardly_chandrasekhar  
wizardly_chandrasekhar

When deleting a container, the container must be stopped, otherwise the following error will be reported

apidemos@apidemos:~$ docker rm wizardly_chandrasekhar
Error response from daemon: You cannot remove a running container bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85. Stop the container before attempting removal or force remove
Like(1)