first container

Start a container running a webserver
docker run -d -P training/webapp python app.py

This runs the training/webapp-image in detached (-d) mode, exposing all ports (-P), calling python inside the container with one argument app.py, which is a script inside the container.

About detached and foreground mode
If you don’t specify a mode, by default, a freshly started container will run in foreground mode. This means that the terminal will attach to the process running in the container and that you will see it’s output. You cannot use your terminal window anymore, until the process in the container closes. By specifying the -d flag the container will run in the background and the run command will return the container id. Read more about the differences between detached and foreground mode here.
List the details of the newest container

The last command will have started a new container, which runs a webserver on port 5000. However, if you go to http://localhost:5000, you will notice that the site will not load. This is because the container port 5000 isn’t mapped to port 5000 on your host machine. The -P flag will have mapped a random port on your machine to the 5000 port of the container.

About exposing ports
By exposing ports you ensure that the process running in the container is reachable on a port on the host machine. These ports do not need to be the same. For example, if you have a container that runs a webserver on port 8080, you can expose the webserver on port 80 of your host machine. If you want to find out more about exposing ports read the Docker documentation.
Figure out which port it is available on by running the command below.
docker ps --latest
Find your container ID in the output

docker ps

Check out the website!
Go to http://localhost:32769 (replace the port with the port number left of the -> in the docker ps command output)

NOTE: If you are using Docker Toolbox, you can't use localhost, but have to use the IP of your Docker virtual machine. Run `docker-machine ls` to find out the name of your Docker Machine. Run `docker-machine ip <machine name>` to find the IP. Then go to http://<ip>:<port>.

The site should say Hello world!.

1. Inspecting the logs

After you’ve seen the Hello world-message you might want to inspect the logs of the docker container with the logs command.

Show the logs
docker logs <container id or name>

You should see GET commands on the root (/) of your webserver.

2. Configuring containers through environment variables

When using containers you’ll want to use the same version of the container across all environments. This maximizes portability and predictability. But, a container running on a test environment should probably use different resources and dependencies than a container running on production.

Environment variables are an excellent way to inject this configuration. Most containers are configurable using environment variables that you can pass in at startup with the -e flag.

Environment variables are a set of dynamic, named values that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. Wikipedia
Change the welcome message displayed on the website

We’ll use the container’s PROVIDER environment variable to pass in your name so it will be shown when you visit the website.

Start a container with the PROVIDER environment variable
docker run -d -P -e PROVIDER="Your name" training/webapp python app.py

Find out the port for this new container using the command below and visit it again to see your name displayed.

Find the mapped port for exposed port (5000) on the container
docker port <container id returned by previous docker run command> 5000

3. Run more containers

Execute the run command a couple more times, followed by docker ps
docker run -d -P training/webapp python app.py
docker ps

Notice that every container has a different port assigned on the host, but all map to 5000 port on the container. This is handled by the -P parameter.

-P, --publish-all     Publish all exposed ports to random ports

Of course, at times you may want to have more control over the ports you are running on.

Figure out how to start another training/webapp container mapping a specific host port (80) to the exposed port of the container (5000). Afterwards you should be able to see the Hello-message on http://localhost:80.

If you get stuck mapping the 5000 port of the container to port 80 of the host, start by checking the documentation of the run command.