Tuesday, March 7, 2017

Docker : Static Website

Before moving forward make sure, you have up and running Docker instance

1) Run a static website in a container

docker run -d seqvence/static-site

So, what happens when you run this command?Since the image doesn't exist on your Docker host, the Docker daemon first fetches it from the registry and then runs it as a containerThe -d flag enables detached mode, which detaches the running container from the terminal/shell and returns your prompt after the container starts

2) Verify if container in running state

docker ps

CONTAINER ID    IMAGE    COMMAND    CREATED   STATUS    PORTS   NAME

Sb3b30fab0bc2        seqvence/static-site   "/bin/sh -c 'cd /u..."   5 seconds ago       Up 4 seconds        80/tcp, 443/tcp     wizardly_hypatia

3) But running container like in step 1) will not expose ports running in container to host machine. So, we need mention following flags for mapping/publish container ports on Docker Host

  • -d will create a container with the process detached from our terminal
  • -P will publish all the exposed container ports to random ports on the Docker host
  • -e is how you pass environment variables to the container
  • --name allows you to specify a container name
  • AUTHOR is the environment variable name and Your Name is the value that you can pass
docker run --name static-site -e AUTHOR="Your Name" -d -P seqvence/static-site

4) Now run below command to see how ports are published on docker host

docker port static-site443/tcp -> 0.0.0.0:3276880/tcp -> 0.0.0.0:32769

5) Try running http://docker-host:32768

6) If we need to map/publish custom host port to the container web server. Try running site 2 at the same time with custom port

docker run --name static-site-2 -e AUTHOR="Manoj Kumar" -d -p 8888:80 seqvence/static-site


docker ps

CONTAINER ID    IMAGE     COMMAND    CREATED       STATUS         PORTS          NAMES
c5d9d6bc66d1        seqvence/static-site   "/bin/sh -c 'cd /u..."   3 seconds ago       Up 3 seconds        443/tcp, 0.0.0.0:8888->80/tcp                   static-site-2

7) Try running http://docker-host:8888

NOTE: If you have stopped container and trying to start container again with different environment variable, if will through conflict as container already created and in stopped state. If you want to pass more/updated environment variables, 1st remove container and start again