Skip to content
Docker

icon picker
Docker File

A Docker file is a text document that contains all the commands a user could call on the command line to assemble an image.

Build a Docker Image from Docker file

docker build -f <path_of_docker_file>

Docker File Instructions

From
FROM <image>[:<tag>]
Run (execute commands on top of current image)
Example:
RUN apt-get update
RUN apt-get install -y curl
Cmd (Run the software contained in the image, basically executing the image in the container)
There can be only one cmd instruction in the docker file
CMD ["executable","param1","param2"]
Expose (Indicates the port on which the container is listening for connections)
Expose <port>
Env (To set environment variable for the application)
Add
Volume
Workdir

Building a custom docker image using docker file

Docker file
# Each instruction in this file generates a new layer that gets pushed to your local image cache
# The line below states we will base our new image on the Latest Official Ubuntu
FROM ubuntu:latest

# Identify the maintainer of an image
LABEL version="0.0.1"
LABEL maitainer="anshulc55@gmail.com"

# Update the image to the latest packages
RUN apt-get update && apt-get upgrade -y

# Install NGINX to test.
RUN apt-get install nginx -y

# Expose port 80
EXPOSE 80

# Last is the actual command to start up NGINX within our Container
CMD [ "nginx", "-g", "daemon off;"]
To execute this docker file to create a custom image
docker image build -t customnginx:0.0.1(Image name with tag) .(directory of dockerfile)
To run the built custom docker image in a container
docker run -d -p 8080:80 customnginx
# This will make nginx accessible to outside world on port 8080. We have used port 80 because in docker file we have exposed port 80 for listening.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.