Sign inSign up

sydrawat/nginx-website

By sydrawat

Updated about 7 years ago

Sample webpage with NGINX to play around with docker!

Image
0

162

sydrawat/nginx-website repository overview

Playing with Docker:

Install Docker from the official website: https://www.docker.com/

After successful installation of Docker, you can try out some of the basic commands from the terminal:

  • docker
  • docker version
  • docker info

Let’s create an nginx docker and run it on our localhost!

  1. Run the following command: docker run -it -p 80:80 nginx Here the -it is for interactive, -p is for port, 80:80 are the local and nginx ports, nginx is the image name. If the image is not available in local system, docker will pull it from docker hub automatically.
  2. The above same command can be run in a similar way if we have a docker image already pulled into our local system docker pull nginx [docker pull pulls a new image into local system from docker hub] docker container run -d -p 8080:80 --name myFirstContainerNginx nginx [-d is for detach, —name is to give container a name]

The only difference between the first and second approach is that -it and -d.

  • The -it flag is for interactive mode, so all the activities of the docker will be logged onto the console/terminal.
  • The -d flag is for detached mode, where the logging will be disabled in the console/terminal.
Check the docker images and containers:
  • To check which docker images are installed on your local system: docker images
  • To remove the above image, run: docker image rm <IMAGE_ID> (3 characters of the ID field will work here!)
  • To check the docker containers that are present in the local system that are currently running: docker container ls [docker ps also works!]
  • To check the docker containers that are created, but not currently being used: docker container ls -a [docker ps -a also works!]
  • To remove a docker container: docker container rm <CONTAINER_ID> (3 characters from the ID field will work here!)

Setting environment variables when creating and running the docker:

  • Go to docker hub and search the official image that you are going to use. Scroll down in the documentation of the docker image to the environment variables section. Run the docker command with the —env flag and set its value there: docker container run -d -p 3306:3306 --name mysql_container --env MYSQL_ROOT_PASSWORD=12345 mysql
To stop the containers that are currently running, you can give the command:
  • docker container stop <container_name> This command will stop the container. Even though it is not running, it is still present on the local system. You can use docker ps -a command to check the list of all the containers.
To remove a container that is currently running:
  • docker container rm <container_name> -f We have to use -f [force] flag since the docker is currently running.
Enter your container [bash into nginx container]:
  • docker container exec -it <container_name> bash This command will take you to the bash inside of the container, where you can “ls” and check the folders.
  • For nginx, the index.html file is present in /usr/share/nginx/html/index.html path. We can obviously change it here[not a good idea!], but we can create a volume to create an area in our local file system.
  • Exit out of the container with exit.

Check out the GIST that has all the basic commands that we will use in docker.

BIND MOUNT:

-v flag : map a local system path to the file path on the container. docker container run -d -p 8080:80 -v $(pwd):/usr/share/nginx/html --name nginx-website nginx

Bind mount is created from local file system to inside the container through the above command. This means we have access to the webroot inside the container via the $(pwd) directory.

If we create a file in the $(pwd), say index.html and update it with an "h1" tag.

Now, if we go back to localhost:8080, we can see “Hello World!” being displayed instead of the default Nginx webpage. This is because now we have access to the webfoot of nginx directory in the docker “nginx-website”.

Create a new file about.html with a different "h1" tag.

Now, if we access localhost:8080/about.html, we can see the message “About page!!” being displayed.

CREATE AN IMAGE FROM THIS APPLICATION NOW:

  • Create a Dockerfile:
FROM nginx:latest
WORKDIR /usr/share/nginx/html/
COPY . .
  • Build the image based on the above docker file: docker image build -t sydrawat/ngnix-website . This command will build the image based on the Dockerfile that we have created in the local repository. To push it to docker hub from our profile, we need to use the -t flag and provide our /. This will push it to the docker hub after it is successfully built. The ‘.’ at the end of the command refers to the docker file that is present in the $(pwd).
  • Once the command returns build success, we can check the image using the command ‘docker images’ which will have the name as /. We can now create containers from this image.
  • docker container run -d -p 8082:80 sydrawat/nginx-website => this will create the docker container. You can access the localhost:8082 to check if it is working. Also check localhost:8082/about.html.

PUSH DOCKER IMAGE TO YOUR DOCKER HUB REPOSITORY:

  • docker push sydrawat/nginx-website
  • In case there is authentication error while pushing, run docker login to authenticate.
  • Access your docker image from docker hub!

[Check out the vide from Traversy Media for a video description of this document]

Tag summary

Content type

Image

Digest

Size

42.7 MB

Last updated

about 7 years ago

docker pull sydrawat/nginx-website