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 🐳.
- Docker 🐳 basics
- Familarity with command
- Nothing Else ❌
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.
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
-t
flag is used to specify name of image.image_name
is obiously value for-t
flag - the name you want to give to your imagedir_name
is name of directory 📁 in which docker is going to look 👀 for a file namedDockerfile
🤔. It's our point of interest 🤔 to get to know how to creating a docker 🐳 image.
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
.
FROM <base_image>
ADD <src> <dest>
COPY <src> <dest>
EXPOSE <port>
ENTRYPOINT <cmd>
CMD <cmd>
Let's take a look 👀 at these commands
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.ADD
- It copies files 🗃 from source in host system 💻 to the destination of image to be created.COPY
- It also copies files 🗃 from source in host system 💻 to the destination of image to be created.EXPOSE
- This instruction is used to tell docker 🐳 to open a port inside container running ▶ this image.ENTRYPOINT
- This specify command to be run ▶, when image is used to run ▶ a container.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
Consider we want to dockerize our Hack The Number
static site. Which looks like something this
For the project create a new directory with the following command and move to it
```
mkdir hackdir
cd hackdir
```
-
Create Dockerfile in directory
nano Dockerfile
-
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
FROM nginx
- This line tells docker 🐳 to use Nginx 🆖 as the official image as a base image for our new image.ADD src/* /usr/share/nginx/html/
- This line copies files 🗃 ofsrc
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.EXPOSE 80
- This line tells docker 🐳 to open port 80 inside the docker 🐳 container so that we can communicate with the nginx 🆖 server inside.CMD ["nginx","-g","daemon off;"]
- This last cline starts Nginx 🆖 server and runs ▶ every time the docker 🐳 container start.
-
Download the src folder 📂 from this repo to your directory 📁
-
Now your hackdir will look 👀 like following
hackdir/ Dockerfile src/ index.html
-
Now run ▶ the following command
docker build -t hackthenumber .
-
To test your image run ▶ the following commands
docker run -dp 80:80 hackthenumber
-
Now visit the localhost. If everything done correctly you will see following output
- 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
- It involve Three 3️⃣ step
- Create your application
- Create Dockerfile according to your need
- Run ▶ the docker build command
- 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.
- Try outout
Hack The Number
game from here 👉 Hack The Number. - Got stuck at syntax errors? Try some Dockerfile validator like FROM latest