DEV Community

Cover image for Beginners Introduction to Dockerfile
Rohith V
Rohith V

Posted on

Beginners Introduction to Dockerfile

What is 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.

A very basic Dockerfile will look like

# Use an existing docker image as a base
FROM alpine


# Download and install the dependency
RUN apk add --update redis


# Tell the image what to do when it starts as a container
CMD ["redis-server"]
Enter fullscreen mode Exit fullscreen mode

It generally have 3 steps :

  • FROM command which is to specify a base image

  • RUN command to download and install the dependancies

  • CMD command to tell the image what to do after starting as a container.

Explanation using a simple scenario

Consider a case where you are given a computer with no operating system in it and the you are asked to install google chrome into it. So how can we proceed and install the google chrome in it.

Well, the most basic steps we move forward is with

  • Install an operating system because without operating system, the computer will say I don't know what to do, help me with an OS. So the first basic step we do is install the operating system. So when we look to the Dockerfile we can see this step resembles with FROM command which specifies a base image.

  • After installing OS, we will continue with the default browser we have and navigate to the respective site of google chrome to download the installer file. After downloading the installer file, we go to the file location inside the computer and installs the chrome.exe file. Now again we can compare it with Dockerfile that I provided above. This step resembles with the RUN command as it will install all the dependencies. Here we download the installer which needs to be then installed to make our google chrome to start.

  • Now the last step, we execute the installed chrome.exe file and the chrome starts. This is the CMD command from the Dockerfile where we tell the image what to do after starting as a container.

I also created a docker github repository with the basic docker commands, do have a look and be free to correct me.

GitHub logo Rohithv07 / Docker

My Workplay on Docker and Kubernetes. Ref : https://github.com/Rohithv07/DockerCasts

Docker and Kubernetes

My Workplay on docker

Commands to remember :

  • docker run :- runs a command in a new container . docker run = docker create + docker start

  • docker run -p <localhostport>:<containerport> <imagename/id> :- running on ports

  • docker ps :- to list all the running containers

  • docker ps --all :- list all the container ever created

  • docker system prune :- to delete all the containers ever created along with some other properties

  • docker logs <container-id> :- to get the logs

  • docker start :- start stopped container

  • docker stop :- stop the container - gets a sigterm message - terminate signal

  • docker kill :- kills the container or stops the container instantly

  • docker exec -it <container id> <command> :- Execute an additional command in container. -it makes us to provide the input. -it equivalent to -i -t

  • docker exec -it <container id> sh :- provides access to the terminal…

Top comments (0)