Last active
June 27, 2016 11:45
-
-
Save ajeetraina/585da353ca77c00e41ebd7c36786128b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Getting started with Docker: | |
1. First Container | |
root@master1:~# docker run busybox echo hello world | |
hello world | |
What we did? | |
. We used one of the smallest, simplest images available: busybox. | |
• busybox is typically used in embedded systems (phones, routers...) | |
• We ran a single process and echo'ed hello world | |
2. Let's explore further more: | |
root@master1:~# docker run -it ubuntu | |
root@91fa3b4e1048:/# | |
This is a brand new container. | |
• It runs a bare-bones, no-frills ubuntu system. | |
• -it is shorthand for -i -t. | |
• -i tells Docker to connect us to the container's stdin. | |
• -t tells Docker that we want a pseudo-terminal | |
3. Let's try installing figlet inside this container | |
Try to run figlet in our container. | |
root@04c0bb0a6c07:/# figlet hello | |
bash: figlet: command not found | |
Alright, we need to install it. | |
Let's check how many packages are installed here. | |
root@04c0bb0a6c07:/# dpkg -l | wc -l | |
189 | |
• dpkg -l lists the packages installed in our container | |
• wc -l counts them | |
• If you have a Debian or Ubuntu machine, you can run the same command and | |
compare the results. | |
4. Installing Package inside the container: | |
We want figlet, so let's install it: | |
root@04c0bb0a6c07:/# apt-get update | |
... | |
Fetched 1514 kB in 14s (103 kB/s) | |
Reading package lists... Done | |
root@04c0bb0a6c07:/# apt-get install figlet | |
One minute later, figlet is installed! | |
# figlet hello | |
_ _ _ | |
| |__ ___| | | ___ | |
| '_ \ / _ \ | |/ _ \ | |
| | | | __/ | | (_) | | |
|_| |_|\___|_|_|\___/ | |
5. How do we exit the shell in the container? | |
Just exit the shell, like you would usually do. | |
(E.g. with ^D or exit) | |
root@04c0bb0a6c07:/# exit | |
• Our container is now in a stopped state. | |
• It still exists on disk, but all compute resources have been freed up. | |
Reading package lists... Done | |
6. Starting another container | |
What if we start a new container, and try to run figlet again? | |
$ docker run -it ubuntu | |
root@b13c164401fb:/# figlet | |
bash: figlet: command not found | |
• We started a brand new container. | |
• The basic Ubuntu image was used, and figlet is not here. | |
7. Our first containers were interactive. | |
We will now see how to: | |
• Run a non-interactive container. | |
• Run a container in the background. | |
• List running containers. | |
• Check the logs of a container. | |
• Stop a container. | |
• List stopped containers. | |
• We will see in the next chapters how to bake a custom image with figlet. | |
8. This container just displays the time every second. | |
$ docker run jpetazzo/clock | |
Fri Feb 20 00:28:53 UTC 2015 | |
Fri Feb 20 00:28:54 UTC 2015 | |
Fri Feb 20 00:28:55 UTC 2015 | |
... | |
• This container will run forever. | |
• To stop it, press ^C. | |
• Docker has automatically downloaded the image jpetazzo/clock. | |
• This image is a user image, created by jpetazzo. | |
• We will hear more about user images (and other types of images) later. | |
9. Run a container in the background | |
Containers can be started in the background, with the -d flag (daemon mode): | |
$ docker run -d jpetazzo/clock | |
47d677dcfba4277c6cc68fcaa51f932b544cab1a187c853b7d0caf4e8debe5ad | |
• We don't see the output of the container. | |
• But don't worry: Docker collects that output and logs it! | |
• Docker gives us the ID of the container. | |
10.List running containers | |
How can we check that our container is still running? | |
With docker ps, just like the UNIX ps command, lists running processes. | |
$ docker ps | |
CONTAINER ID IMAGE ... CREATED STATUS ... | |
47d677dcfba4 jpetazzo/clock ... 2 minutes ago Up 2 minutes ... | |
Docker tells us: | |
• The (truncated) ID of our container. | |
• The image used to start the container. | |
• That our container has been running (Up) for a couple of minutes. | |
• Other information (COMMAND, PORTS, NAMES) that we will explain later | |
11.Starting more containers | |
Let's start two more containers. | |
$ docker run -d jpetazzo/clock | |
57ad9bdfc06bb4407c47220cf59ce21585dce9a1298d7a67488359aeaea8ae2a | |
$ docker run -d jpetazzo/clock | |
068cc994ffd0190bbe025ba74e4c0771a5d8f14734af772ddee8dc1aaf20567d | |
Check that docker ps correctly reports all 3 containers. | |
12.Two useful flags for docker ps | |
To see only the last container that was started: | |
$ docker ps -l | |
CONTAINER ID IMAGE ... CREATED STATUS ... | |
068cc994ffd0 jpetazzo/clock ... 2 minutes ago Up 2 minutes ... | |
To see only the ID of containers: | |
$ docker ps -q | |
068cc994ffd0 | |
57ad9bdfc06b | |
47d677dcfba4 | |
Combine those flags to see only the ID of the last container started! | |
$ docker ps -lq | |
068cc994ffd0 | |
13.View the logs of a container | |
We told you that Docker was logging the container output. | |
Let's see that now. | |
$ docker logs 068 | |
Fri Feb 20 00:39:52 UTC 2015 | |
Fri Feb 20 00:39:53 UTC 2015 | |
... | |
• We specified a prefix of the full container ID. | |
• You can, of course, specify the full ID. | |
• The logs command will output the entire logs of the container. | |
(Sometimes, that will be too much. Let's see how to address that.) | |
14.View only the tail of the logs | |
To avoid being spammed with eleventy pages of output, we can use the --tail | |
option: | |
$ docker logs --tail 3 068 | |
Fri Feb 20 00:55:35 UTC 2015 | |
Fri Feb 20 00:55:36 UTC 2015 | |
Fri Feb 20 00:55:37 UTC 2015 | |
• The parameter is the number of lines that we want to see. | |
15.Follow the logs in real time | |
Just like with the standard UNIX command tail -f, we can follow the logs of our | |
container: | |
$ docker logs --tail 1 --follow 068 | |
Fri Feb 20 00:57:12 UTC 2015 | |
Fri Feb 20 00:57:13 UTC 2015 | |
^C | |
• This will display the last line in the log file. | |
• Then, it will continue to display the logs in real time. | |
• Use ^C to exit. | |
16. Stop our container | |
There are two ways we can terminate our detached container. | |
• Killing it using the docker kill command. | |
• Stopping it using the docker stop command. | |
The first one stops the container immediately, by using the KILL signal. | |
The second one is more graceful. It sends a TERM signal, and after 10 seconds, if the | |
container has not stopped, it sends KILL. | |
Reminder: the KILL signal cannot be intercepted, and will forcibly terminate the | |
container. | |
Background Containers | |
Docker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment