Running arbitrary Linux commands inside a Docker container is fun, but let's do something more useful.
Pull down the nginx
Docker image from the Docker Hub. This Docker image uses the Nginx webserver to serve a static HTML website.
Start a new container from the nginx
image that exposes port 80 from the container to port 8000 on your host. You will need to use the -p
flag with the docker run command.
NOTE: Mapping ports between your host machine and your containers can get confusing. Here is the syntax you will use::
docker run -p 8000:80 nginx
The trick is to remember that the host port always goes on the left, and the container port always goes on the right. Remember it as traffic coming from the host, to the container.
Open a web browser and go to port 8000 on your host. The exact address will depend on how you're running Docker today:
- Native Linux - http://localhost:8000
- Cloud server - Use the hostname for your server. Ex: http://ec2-54-69-126-146.us-west-2.compute.amazonaws.com:8000
If you see a webpage saying "Welcome to nginx!" then you're done!
If you look at the console output from docker, you see nginx producing a line of text for each time a browser hits the webpage:
sofus@Praq-Sof:~/git/docker-exercises$ docker run -p 8000:80 nginx
172.17.0.1 - - [31/May/2017:11:52:48 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0" "-
Press control + c in your terminal window to stop your container.
When running a webserver like nginx, it's pretty useful that you do not have to have an open session into the server at all times to run it.
We need to make it run in the background, freeing up our terminal for other things.
Docker provides this with the -d
parameter for run.
docker run -p 8000:80 -d nginx
sofus@Praq-Sof:~/git/docker-exercises$ docker run -p 8000:80 -d nginx
78c943461b49584ebdf841f36d113567540ae460387bbd7b2f885343e7ad7554
Docker prints out the container-id, and returns to the terminal.
If you want to go into the container again to execute something you have two options
attach
Attach to the specified process running on the container. In our example it is the nginx server.exec
Executing another process inside the container. This could be a shell, or a script of some sort.
NOTE: When you attach to an already started container, you cannot exit without killing the container unless you issue the attach command like this:
docker attach --sig-proxy=false CONTAINER
If you issue theexec
command, you can stop it byCtrl+d
or detach yourself byCtrl+p Ctrl+c
Try to attach to the container. Exit it, and browse the webpage again to acknowledge it is gone.
Start the container once again, and execute a bash inside the container:
sofus@Praq-Sof:~/git/docker-exercises$ docker exec -it CONTAINER bash
root@78c943461b49:/# ping 127.0.0.1 -c 100 > /tmp/ping
Ten detach from the container with Ctrl+p Ctrl+c
and run the following
docker exec -it container bash
tail -f /tmp/ping
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.156 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.183 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.158 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.139 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.156 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.139 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=64 time=0.118 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=64 time=0.155 ms
64 bytes from 127.0.0.1: icmp_seq=10 ttl=64 time=0.149 ms
64 bytes from 127.0.0.1: icmp_seq=11 ttl=64 time=0.202 ms
64 bytes from 127.0.0.1: icmp_seq=12 ttl=64 time=0.154 ms
64 bytes from 127.0.0.1: icmp_seq=13 ttl=64 time=0.151 ms
64 bytes from 127.0.0.1: icmp_seq=14 ttl=64 time=0.144 ms
64 bytes from 127.0.0.1: icmp_seq=15 ttl=64 time=0.148 ms
64 bytes from 127.0.0.1: icmp_seq=16 ttl=64 time=0.163 ms
64 bytes from 127.0.0.1: icmp_seq=17 ttl=64 time=0.146 ms
64 bytes from 127.0.0.1: icmp_seq=18 ttl=64 time=0.156 ms
64 bytes from 127.0.0.1: icmp_seq=19 ttl=64 time=0.151 ms
64 bytes from 127.0.0.1: icmp_seq=20 ttl=64 time=0.147 ms
64 bytes from 127.0.0.1: icmp_seq=21 ttl=64 time=0.165 ms
64 bytes from 127.0.0.1: icmp_seq=22 ttl=64 time=0.156 ms
64 bytes from 127.0.0.1: icmp_seq=23 ttl=64 time=0.042 ms
64 bytes from 127.0.0.1: icmp_seq=24 ttl=64 time=0.139 ms
64 bytes from 127.0.0.1: icmp_seq=25 ttl=64 time=0.153 ms
64 bytes from 127.0.0.1: icmp_seq=26 ttl=64 time=0.158 ms
64 bytes from 127.0.0.1: icmp_seq=27 ttl=64 time=0.198 ms
Here you see that the ping process started in another shell is still running and producing this logfile.
Just stop the process with a Ctrl+d
.
Before you go on, use the Docker command line interface <https://docs.docker.com/reference/commandline/cli/>
_ documentation to try a few more commands:
- Start the same container exposing the same port, but run the container in "detached" mode, so that it doesn't tie up your command line
- While your detached container is running, use the
docker ps
command to see what silly name Docker gave your container. This is one command you're going to use often! - While your detached container is still running, look at its logs. Try following its logs and refreshing your browser.
- Stop your detached container, and confirm that it is stopped with the
ps
command. - Start it again, wait 10 seconds for it to fire up, and stop it again.
- Then delete that container from your system.
NOTE: When running most docker commands, you only need to specify the first few characters of a container's ID. For example, if a container has the ID
df4fd19392ba
, you can stop it withdocker stop df4
. You can also use the silly names Docker provides containers by default, such asboring_bardeen
.
Now that you are familiar with starting, stopping and port forwarding containers, let us head over to exercise 4 to learn how to build your own docker files.