Skip to content

Running your first app

In this section, we will run some pre-existing Docker containers that are based on public Docker images hosted on Docker Hub.

Note

In place of all the curl commands below, you can use a web browser to access the service in the container.

Hello World

We can use the docker run command to run containers from Docker images. Let's start with a simple "hello world" container.

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

Images for containers are automatically pulled, if they don't exist locally.

Ubuntu in a container

We can run a Ubuntu Linux distribution in a container. The following command pulls the Ubuntu Linux container and runs the uname -a command in it, which prints the current system information.

$ docker run ubuntu:20.04 uname -a
Linux 4dd6fed5a8a9 4.19.8-200.fc28.x86_64 #1 SMP Mon Dec 10 15:43:40 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

We can also run the container in a command-line mode (i.e. open a shell) using the -i (interactive) and -t (tty) flags.

$ docker run -it ubuntu:18.04
root@fad321cefd0a:/# ls
bin   dev  home  lib32  libx32  mnt  proc  run   srv  tmp  var
boot  etc  lib   lib64  media   opt  root  sbin  sys  usr
root@fad321cefd0a:/# exit

While in the interactive shell in the container, type exit or press they keyboard combination Ctrl+D to return back to the host command-line interface.

Web server

Let's run a long-running process like a web server. We can use NGINX for that.

$ docker run -p 8080:80 nginx

NGINX opens port 80 for HTTP in the container. We can use the -p option to map the port to a port in our host computer like 8080.

We can test the container by opening URL http://localhost:8080/ in a web browser or with the curl command in another terminal window. Curl creates an HTTP request to the target URL and writes the output to the terminal window.

$ curl http://localhost:8080/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...

You should see NGINX write logs to the terminal where you launched NGINX.

172.17.0.1 - - [01/Feb/2019:13:12:58 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0" "-"
2019/02/01 13:12:58 [error] 6#6: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8080"
172.17.0.1 - - [01/Feb/2019:13:12:58 +0000] "GET /favicon.ico HTTP/1.1" 404 153 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0" "-"
172.17.0.1 - - [01/Feb/2019:13:14:33 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.59.0" "-"

To stop the container and return back to the command-line, press the keyboard combination Ctrl+C.

Containers can also be run detached from the terminal with the -d flag. Let's also give the container a name we can refer to later.

$ docker run -d --name web -p 8080:80 nginx
624a8853afe59b69a7a4ba3f35fa274ba28247bd931cef1f505a9129aad8fa5b

The printed ID is the container ID, which we will look into later. Check that the web server still responds using your web browser or curl.

$ curl http://localhost:8080/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...

Freestyle: Find and run a Docker image

Spend some time browsing Docker images from Docker Hub, and try to get one running with your instructor's help.

Next

Now that we know how to run containers, we can next check out how we can manage them.