Dockerfile

 Dockerfile


In this article we are going to see about Dockerfile. Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. docker build command use to create image from Dockerfile.




File name should be always Dockerfile

Dockerfile Components:

FROM : For base image. This command must be on top of the Dockerfile.

RUN : To execute commands, it will  create a layer in image.

MAINTAINER : Author/Owner/Description

COPY : Copy files from local system(docker VM) we need to provide source, destination (we can’t download file from 
internet and any remote repository.

ADD : Similar to copy but, it provides a feature to download files from internet, also we extract file at docker image side.



EXPOSE : To expose/open ports such as port 8080 for tomcat, port 80 for nginx etc

WORKDIR : To set working directory for a container.

CMD : Execute commands but during container creation

ENTRYPOINT : It’s similar to CMD, but has higher priority over CMD, first commands will be executed but Entrypoint only.

ENV : Environment variables.


Docker Commands to create image from Dockerfile

Firstly create a Dockerfile on docker engine and make sure file name should be Dockerfile.

Write some code in Dockerfile as mention below:

FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y git
RUN apt-get install -y vim

After that save file and exit from editor.

To create image from Dockerfile
[user@comp]: docker build -t ons/ubuntu:v1 .

To check docker images which you created recently
[user@comp]: docker image

Difference between docker attach and docker exec:


Docker exec creates a new process in the container's environment while docker attach just connect the standard input/output of the main process inside the container to corresponding standard input/output error of current terminal. 



Docker exec is specifically for running new things in a already started container, be it a shell or some other process.

Difference between expose and publish a docker:

Basically, you have three options:

Neither specify EXPOSE nor -p
Only specify EXPOSE
Specify EXPOSE and –p

1) If you specify neither EXPOSE nor -p, the service in the container will only be accessible from inside the container itself.

2) If you EXPOSE a port, the service in the container is not accessible from outside Docker, but from inside other Docker containers. So this is good for inter-container communication.

3) If you EXPOSE and -p a port, the service in the container is accessible from anywhere, even outside Docker.




Share:

0 comments

Please leave your comments...... Thanks