Docker Container Usage
Docker Client
The Docker client is straightforward; we can simply enter the docker
command to view all options available for the Docker client.
tutorialpro@tutorialpro:~# docker
You can delve deeper into specific Docker commands using the command docker command --help.
For example, to see the detailed usage of the docker stats command:
tutorialpro@tutorialpro:~# docker stats --help
Container Usage
Pulling an Image
If we don't have the Ubuntu image locally, we can use the docker pull
command to pull the Ubuntu image:
$ docker pull ubuntu
Starting a Container
The following command starts a container using the Ubuntu image, with parameters to enter the container in command-line mode:
$ docker run -it ubuntu /bin/bash
Parameter explanations:
- -i: Interactive operation.
- -t: Terminal.
- ubuntu: The Ubuntu image.
- /bin/bash: The command following the image name, where we want an interactive shell, hence /bin/bash.
To exit the terminal, simply type exit:
root@ed09e4490c57:/# exit
Starting a Stopped Container
To view all containers, use the following command:
$ docker ps -a
Click the image to view it in full size:
To start a stopped container, use docker start
:
$ docker start b750bbbcfd88
Running in the Background
In most scenarios, we want the Docker service to run in the background. We can specify the container's running mode with -d
.
$ docker run -itd --name ubuntu-test ubuntu /bin/bash
Click the image to view it in full size:
Note: Adding the -d
parameter means the container won't enter the terminal by default. To enter the container, use the docker exec command (discussed below).
Stopping a Container
$ docker stop <Container ID>
A stopped container can be restarted with docker restart
:
$ docker restart <Container ID>
Entering a Container
When using the -d parameter, the container starts in the background. To enter the container, use the following commands:
- docker attach
- docker exec: It's recommended to use
docker exec
as it exits the container terminal without stopping the container.
attach Command
Example using docker attach
:
$ docker attach 1e560fca3906
Note: Exiting from this container will stop it.
exec Command
Example using docker exec
:
docker exec -it 243c32535da7 /bin/bash
Note: Exiting from this container will not stop it, which is why docker exec is recommended.
For more parameter details, use docker exec --help
.
Exporting and Importing Containers
Exporting a Container
To export a local container, use the docker export command:
$ docker export 1e560fca3906 > ubuntu.tar
This exports the container snapshot to the local file ubuntu.tar
.
Importing a Container Snapshot
Use docker import
to import a container snapshot as an image. The following example imports the snapshot file ubuntu.tar
into the image test/ubuntu:v1
:
$ cat docker/ubuntu.tar | docker import - test/ubuntu:v1
You can also import from a URL or a directory, for example:
$ 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
This command clears all terminated containers.
Running a Web Application
Previously, the containers we ran had no specific use.
Next, let's try building a web application using Docker.
We'll run a Python Flask application in a Docker container to serve a web application.
tutorialpro@tutorialpro:~# docker pull training/webapp # Pull the image
tutorialpro@tutorialpro:~# docker run -d -P training/webapp python app.py
Parameter explanations:
- -d: Run the container in the background.
- -P: Map the container's internal network ports randomly to the host.
Viewing the Web Application Container
Use docker ps
to see the running containers:
tutorialpro@tutorialpro:~# docker ps
CONTAINER ID IMAGE COMMAND ... PORTS
d3d5e39ed9d3 training/webapp "python app.py" ... 0.0.0.0:32769->5000/tcp
Additional port information is displayed:
PORTS
0.0.0.0:32769->5000/tcp
Docker has mapped port 5000 (default Python Flask port) to host port 32769.
You can now access the web application through a browser.
We can also set different ports using the -p
parameter:
tutorialpro@tutorialpro:~$ docker run -d -p 5000:5000 training/webapp python app.py
docker ps to view running containers:
tutorialpro@tutorialpro:~# 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
Port 5000 inside the container is mapped to port 5000 on the local host.
Network Port Shortcut
The docker ps
command shows the port mapping, and Docker also provides a shortcut docker port
to view a specific port mapping for a container (ID or name).
For the web application container with ID bf08b7f2cd89 and name wizardly_chandrasekhar:
tutorialpro@tutorialpro:~$ docker port bf08b7f2cd89
5000/tcp -> 0.0.0.0:5000
tutorialpro@tutorialpro:~$ docker port wizardly_chandrasekhar
5000/tcp -> 0.0.0.0:5000
Viewing Web Application Logs
docker logs [ID or name]
can view the standard output from the container.
tutorialpro@tutorialpro:~$ 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: Makes docker logs
output like tail -f
.
From the above, we can see the application uses port 5000 and can view the application's access logs.
Viewing Processes in the Web Application Container
We can use docker top
to view the processes running inside the container:
tutorialpro@tutorialpro:~$ docker top wizardly_chandrasekhar
UID PID PPID ... TIME CMD
root 23245 23228 ... 00:00:00 python app.py
Inspecting the Web Application
Use docker inspect to view the underlying information of Docker. It returns a JSON file with the container's configuration and status information.
tutorialpro@tutorialpro:~$ 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
tutorialpro@tutorialpro:~$ docker stop wizardly_chandrasekhar
wizardly_chandrasekhar
Restart the WEB Application Container
For a stopped container, we can use the docker start
command to start it.
tutorialpro@tutorialpro:~$ docker start wizardly_chandrasekhar
wizardly_chandrasekhar
To query the last created container, use docker ps -l
:
# docker ps -l
CONTAINER ID IMAGE PORTS NAMES
bf08b7f2cd89 training/webapp ... 0.0.0.0:5000->5000/tcp wizardly_chandrasekhar
For a running container, we can use the docker restart
command to restart it.
Remove the WEB Application Container
We can use the docker rm
command to delete unnecessary containers.
tutorialpro@tutorialpro:~$ docker rm wizardly_chandrasekhar
wizardly_chandrasekhar
When deleting a container, it must be in a stopped state, otherwise, the following error will occur:
tutorialpro@tutorialpro:~$ docker rm wizardly_chandrasekhar
Error response from daemon: You cannot remove a running container bf08b7f2cd897b5964943134aa6d373e355c286db9b9885b1f60b6e8f82b2b85. Stop the container before attempting removal or force remove