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.
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.
docker ps --latest
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.
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.
We’ll use the container’s PROVIDER
environment variable to pass in your name so it will be shown when you visit the website.
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.
docker port <container id returned by previous docker run command> 5000
3. Run more containers
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. |