GithubHelp home page GithubHelp logo

sandeep-source / creating_first_docker_image Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 0.0 41 KB

This repository provides basic guide for creating your very first docker image.

License: GNU General Public License v3.0

HTML 97.20% Dockerfile 2.80%
docker dockerfile devops-tools docker-container docker-image dockerfiles learning collaborate learn

creating_first_docker_image's Introduction

Creating_First_Docker_Image

Introduction

Containers are amazing 🀩 and fun πŸŽ‰. Creating a container and running β–Ά your website 🌐 inside it just take few steps, But did πŸ€” you know 🧠 how to create a custom container image. Creating a custom container has some more benifits over using existing one i.e you can show off 😎, you can share your project πŸ“ or website which does not require any extra configutation βš™ setup. In this repo i'm going πŸƒβ€β™‚οΈ to talk how to create your first container image with one of the very famous container docker 🐳.

Pre-requisists

  1. Docker 🐳 basics
  2. Familarity with command
  3. Nothing Else ❌

First Step

Very first 1️⃣ step πŸšΆβ€β™€οΈ to create a docker 🐳 image to install πŸ›  docker 🐳 . Most probably you had already done πŸ‘ this if you are reading this article, But if not checkout installation guide πŸ“– Here.

How to create one

For creating images docker 🐳 provides docker build command. The syntax of docker 🐳 command as following

docker build -t image_name dir_name

Let's break down this command further

  1. -t flag is used to specify name of image.
  2. image_name is obiously value for -t flag - the name you want to give to your image
  3. dir_name is name of directory πŸ“ in which docker is going to look πŸ‘€ for a file named Dockerfile πŸ€”. It's our point of interest πŸ€” to get to know how to creating a docker 🐳 image.

What is Dockerfile πŸ€”

Docker 🐳 reads instructions from Dockerfile to create an image. Basically Dockerfile specifies the step πŸšΆβ€β™‚οΈ to take to create βš’ a image according to your need. if you need more than one 1️⃣ file πŸ“„ for distict cases then use format <word_of_choice>.Dockerfile.

Creating Dockefile

FROM <base_image>
ADD <src> <dest>
COPY <src> <dest>
EXPOSE <port>
ENTRYPOINT <cmd>
CMD <cmd>

Let's take a look πŸ‘€ at these commands

  1. FROM - It specifies the base image to be used. It forms base for our image. Our customization πŸ”§ will be placed on the top of this image. Base image can one the images available on dockerhub 🐳 which is bestfit for your usecase.
  2. ADD - It copies files πŸ—ƒ from source in host system πŸ’» to the destination of image to be created.
  3. COPY - It also copies files πŸ—ƒ from source in host system πŸ’» to the destination of image to be created.
  4. EXPOSE - This instruction is used to tell docker 🐳 to open a port inside container running β–Ά this image.
  5. ENTRYPOINT - This specify command to be run β–Ά, when image is used to run β–Ά a container.
  6. CMD - This is also specifies command to run β–Ά every time ⏲ container run β–Ά. CMD can be overriden when running β–Ά image.
Note: Final command to be run determined by ENTRYPOINT + CMD

All of the mentioned instructions for the Dockerfile is not mendatory. The use of these depends on the use cases. For references to available command visit Here

Let's take an Example

Scaenario

Consider we want to dockerize our Hack The Number static site. Which looks like something this

Hack the number home screen

Create a directory

For the project create a new directory with the following command and move to it 
```
mkdir hackdir
cd hackdir
```

Create Dockerfile

  1. Create Dockerfile in directory

    nano Dockerfile
    
  2. Paste the following code πŸ‘©β€πŸ’» inside file πŸ“„

    FROM nginx
    ADD src/* /usr/share/nginx/html/
    EXPOSE 80
    CMD ["nginx","-g","daemon off;"]

    Let's break πŸ”¨ docker the following commands

    1. FROM nginx - This line tells docker 🐳 to use Nginx πŸ†– as the official image as a base image for our new image.
    2. ADD src/* /usr/share/nginx/html/ - This line copies files πŸ—ƒ of src folder inside the /usr/share/nginx/html/ folder of image. /usr/share/nginx/html/ folder is used by the nginx πŸ†– server as entry point of website.
    3. EXPOSE 80 - This line tells docker 🐳 to open port 80 inside the docker 🐳 container so that we can communicate with the nginx πŸ†– server inside.
    4. CMD ["nginx","-g","daemon off;"] - This last cline starts Nginx πŸ†– server and runs β–Ά every time the docker 🐳 container start.
  3. Download the src folder πŸ“‚ from this repo to your directory πŸ“

  4. Now your hackdir will look πŸ‘€ like following

    hackdir/
       Dockerfile
       src/
          index.html
    
  5. Now run β–Ά the following command

    docker build -t hackthenumber .
    
  6. To test your image run β–Ά the following commands

    docker run -dp 80:80 hackthenumber
    
  7. Now visit the localhost. If everything done correctly you will see following output

    Hack the number home screen

Conclusion

  1. Building a container image can feel a litle bit anoying at very first 0️⃣ but after understanding some basic it will be piece of cake
  2. It involve Three 3️⃣ step
    1. Create your application
    2. Create Dockerfile according to your need
    3. Run β–Ά the docker build command

Quick tools

  1. If you don't have docker 🐳 installed βš™ or don't want to do, then try it from Here. Just login with dockerhub credentials and you are good to go.
  2. Try outout Hack The Number game from here πŸ‘‰ Hack The Number.
  3. Got stuck at syntax errors? Try some Dockerfile validator like FROM latest

creating_first_docker_image's People

Contributors

sandeep-source avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.