Docker Tutorial. Create Docker Image From Dockerfile

Docker Tutorial. Create Docker Image From Dockerfile

 

In this docker tutorial I will show simple example how to create docker image from Dockerfile step by step. Also I will how build docker image from scratch.

If Docker already installed in your system you can skip Install Docker step.

 

Install Docker

 

Install Docker on Ubuntu

$ sudo apt-get update
$ sudo apt-get install docker.io

 

Install Docker on CentOS

$ sudo yum install yum-utils device-mapper-persistent-data lvm2
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum install docker-ce docker-ce-cli containerd.io
$ sudo systemctl start docker

 

Create Dockerfile

Now lets create MyDockerFiles directory and Dockerfile in this directory:

$ mkdir MyDockerFiles
$ cd MyDockerFiles/
$ touch Dockerfile

Now edit Dockerfile with your favorite text editor like this:

FROM ubuntu

MAINTAINER ARSTECH [email protected]

RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y apt-utils htop

CMD ["echo","It's my Docker Image "]

Dockerfile example

Then save and close Dockerfile.

Full information about Dockerfile you can found on official Dockerfile reference.

 

Build Docker Image From Dockerfile

Now we can build Docker image using docker build command. Where is testimage is new image name, 0.1 is tag, and “.” shows location where can found Dockerfile. Instead can be use absolute path to Dockerfile location folder.

$ sudo docker build -t testimage:0.1 .

Your sample output will be something like:

$ sudo docker build -t testimage:0.1 .
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM ubuntu
---> a2a15febcdf3
Step 2/5 : MAINTAINER ARSTECH [email protected]
---> Using cache
---> 78515626542f
Step 3/5 : RUN apt-get update
---> Running in d784ac0568d7
Get:1 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
....
Fetched 17.0 MB in 7s (2358 kB/s)
Reading package lists...
Removing intermediate container d784ac0568d7
---> 0d77bf90b22c
Step 4/5 : RUN apt-get install -y htop
---> Running in 947eca381460
Reading package lists...
Building dependency tree...
Reading state information...
Suggested packages:
lsof strace
The following NEW packages will be installed:
htop
0 upgraded, 1 newly installed, 0 to remove and 4 not upgraded.
Need to get 80.0 kB of archives.
After this operation, 221 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 htop amd64 2.1.0-3 [80.0 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 80.0 kB in 1s (122 kB/s)
Selecting previously unselected package htop.
(Reading database ... 4040 files and directories currently installed.)
Preparing to unpack .../htop_2.1.0-3_amd64.deb ...
Unpacking htop (2.1.0-3) ...
Setting up htop (2.1.0-3) ...
Removing intermediate container 947eca381460
---> 0bc682c54d00
Step 5/5 : CMD ["echo","It's my Docker Image "]
---> Running in 4dad7391d10e
Removing intermediate container 4dad7391d10e
---> d53e919dc2ed
Successfully built d53e919dc2ed
Successfully tagged testimage:0.1

In picture I highlighted most important output 5 steps during docker image bulding:

Create Docker Image From Dockerfile

Show Docker Images

$ sudo docker images
[sudo] password for user:
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
testimage           0.1                 d53e919dc2ed        1 hours ago        92.2MB
ubuntu              latest              a2a15febcdf3        13 days ago         64.2MB

As you see i have newly created docker image: testimage with TAG 0.1

Create Docker Image From Scratch

 

If you need build docker image for your application from scratch, not from parent images (Ubuntu, CentOS, …etc) in Docker file FROM instruction should be scratch:

FROM scratch

Then build image as any other:

$ sudo docker build -t hello .


Summary
 Create Docker Image From Dockerfile
Article Name
Create Docker Image From Dockerfile
Description
Docker Tutorial. Create Docker Image From DockerfileIn this docker tutorial I will show example how to create docker image from Dockerfile step by step
Author
Publisher Name
ARSTECH
Publisher Logo

arstech

Leave a Reply

Your email address will not be published. Required fields are marked *