This section is an introduction to Docker for newcomers, and can be used as a refresher for others. We'll see how to quickly use Docker to achieve some tasks such as executing an Ubuntu container or networked webserver, sharing data with a container, building an image, and accessing a registry other than the default one.
We'll quickly manipulate Docker, so we're up and running with some basic usage.
To execute /bin/bash
in an Ubuntu container, tag 16.04 (ubuntu:16.04
). Our environment will be interactive (use -i
) and we want a pseudo-terminal to be allocated (use -t
). We want the container to be destroyed afterwards (use --rm
):
$ docker run -it --rm ubuntu:16.04 /bin/bash root@d372dba0ab90:/# hostname d372dba0ab90
We've run our first container! Now do whatever you want with it. Quitting it destroys it and its content is lost forever as we specified the --rm
option.
Nginx is officially packaged as a Docker container. We want to access port 80
from the container on port 80
of our host using the -p
option, with the latest Nginx version available:
$ docker run --rm -p 80:80 nginx
Make some HTTP requests such as a curl
:
$ curl -IL http://localhost HTTP/1.1 200 OK Server: nginx/1.11.5 […]
The logs on the Docker stdout are displaying the logs as follows:
172.17.0.1 - - [21/Nov/2016:21:21:15 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.43.0" "-"
Maybe for some reason we need to launch a specific Nginx version, like this:
$ docker run --rm -p 80:80 nginx:1.10
The HTTP headers will reflect that we're now running the current stable version:
$ curl -IL http://localhost HTTP/1.1 200 OK Server: nginx/1.10.2
We want our own content to be displayed instead of the default Nginx page. Let's create an index.html
file in the www
directory, with some custom content such as the following:
<html> <h1>Hello from Docker!</h1> </html>
Nginx is serving content by default in /usr/share/nginx/html
; let's use the -v
option to share our own directory with the container:
$ docker run --rm -p 80:80 -v ${PWD}/www:/usr/share/nginx/html nginx:1.10
Let's see our new content served:
$ curl -L http://localhost <html> <h1>Hello from Docker!</h1> </html>
Let's create our own Ubuntu 16.04 image with some utilities such as curl
, dig
, and netcat
in it, so that whatever machine we're using, we can always have our tools at hand. To build our container, we need a file named Dockerfile
, acting like a script, executed line by line, to build the final container. We know we want to start from an Ubuntu 16.04, then update the APT base, and finally install our utilities. Let's do just that using the FROM
and RUN
instructions:
FROM ubuntu:16.04 RUN apt-get -yq update RUN apt-get install -yq dnsutils curl netcat
Now build using the docker build
command, passing it the name of the container with the -t
option:
$ docker build -t utils . Step 1 : FROM ubuntu:16.04 ---> 2fa927b5cdd3 Step 2 : RUN apt-get update -yq ---> Running in 0d8f8e01bde8 [...] Step 3 : RUN apt-get install ruby -yq ---> Running in 425bfb1e8ee1 [...] Removing intermediate container 425bfb1e8ee1 Successfully built c86310e48731
We can see each line of our Dockerfile
is a step in the build process, each step being a container itself (hence the different ID each time).
Let's now execute our container to make a DNS request using dig
:
$ docker run -it --rm utils dig +short google.com 172.217.5.14
Alternatively, we may use curl
as follows:
$ docker run -it --rm utils curl -I google.com HTTP/1.1 302 Found Cache-Control: private Content-Type: text/html; charset=UTF-8 Location: http://www.google.ca/?gfe_rd=cr&ei=UgA1VMLPRUvF9gfJ_riACg Content-Length: 258 Date: Wed, 23 Nov 2016 02:34:58 GMT
When not specifying anything else than the container name, Docker is looking for it locally, then on Docker Hub (https://hub.docker.com). However, we can run our own registry or use an alternative registry such as https://quay.io/. Here's how it works: instead of specifying only the container name, or the combo username/container_name
, we prefix both by the DNS name of the registry, for example, https://quay.io/. Here, we'll launch the HTTP/2 Caddy webserver hosted in the CoreOS account on the Quay.io registry:
$ docker run -it --rm -p 80:2015 quay.io/coreos/caddy Activating privacy features... done. http://0.0.0.0:2015
Here's for this quick introduction on how to use Docker.
18.191.97.85