Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MadLittleMods/929473001ae207b1a6180b5557be364d to your computer and use it in GitHub Desktop.
Save MadLittleMods/929473001ae207b1a6180b5557be364d to your computer and use it in GitHub Desktop.

Docker container with basic nginx server that has exposed ports on Windows Guide/Tutorial

Environment

Windows 10 with Anniversary update

Using Docker Toolbox (uses boot2docker VirtualBox machines)

docker -v
Docker version 1.12.0, build 8eab29e

Instructions

  1. docker-machine create --driver virtualbox some-box or docker-machine start some-box if you already created the machine

  2. docker-machine env some-box (be sure the run the command it spits out at the end of that output to actual setup your env

  3. Setup the nginx container

    docker run -d -v //c/Users/MLM/Downloads/static-server-stuff/public:/usr/share/nginx/html:ro  -v //c/Users/MLM/Downloads/static-server-stuff/nginx.conf:/etc/nginx/nginx.conf:ro -p 5031:80 --name some-nginx nginx
    
    • -d: "Run container in background and print container ID", https://docs.docker.com/engine/reference/commandline/run/#options
    • -v //c/Users/MLM/Downloads/static-server-stuff/public:/usr/share/nginx/html:ro: Sets up a shared read-only(:ro) volume mapping your Windows host directory, C:\Users\MLM\Downloads\static-server-stuff\public, to /usr/share/nginx/html in the container
    • -v //c/Users/MLM/Downloads/static-server-stuff/nginx.conf:/etc/nginx/nginx.conf:ro: Sets up a shared read-only(:ro) volume mapping your Windows host file, C:\Users\MLM\Downloads\static-server-stuff\nginx.conf, to /etc/nginx/nginx.conf in the container
    • -p 5031:80: Maps port Docker machines port 5031 to the containers port 80 (accessible on docker-machine ip some-box, not localhost)
    • --name some-nginx: Adds a name to your container so you can better distinguish it (it can be anything)
    • nginx: The image name to use
  4. Run docker-machine ip some-box to get the IP we can access

  5. Visit the IP we just got above with the port we specified on the container, perhaps something like http://192.168.99.100:5031

Teardown

  1. docker ps --all will show you all of the containers in the machine
  2. You can stop a container by using the ID from the command above, docker stop e4926128afab
  3. You can remove a container by using the ID from the command above docker rm e4926128afab

  1. docker-machine ls will show you all of the machines
  2. To stop the Docker machine docker-machine stop some-box
  3. To remove the whole Docker machine docker-machine rm -f some-box

Troubleshooting

If you want to delete/remove the containers/machines, see the Teardown section above.

You can look at the logs of a container

# You can use the container anme
docker logs some-nginx
# Or use the container ID
docker logs 19d1cdda14a6

You can essentially SSH into the container by using the following command (use your own container ID, docker ps --all)

docker exec -it 19d1cdda14a6 bash
events {
worker_connections 1024;
}
http {
include mime.types;
sendfile on;
tcp_nopush on;
index index.html;
server {
listen 80;
root /usr/share/nginx/html;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Some static content</title>
</head>
<body>
Looks good!
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment