Post Thumbnail

Guide to Dockerfile, Image Creation, Docker Compose, and Essential Docker Commands


Dockerfile:

 

With the help of Dockerfile we can create our own docker image Its contains instruction to build an image. In the simple word we can create our own docker image using Dockerfile instructions.

 

Let’s create our first docker image. Create file called Dockerfile and follow bellow instruction.

 

 

  FROM ubuntu:16.04

  MAINTAINER rasel.laravel@gmail.com

  RUN apt-get update && apt-get -y upgrade && apt-get install -y nginx

  EXPOSE 80

  CMD [“nginx”, “-g”, “daemon off”]



FROM ubuntu:16.04 is an existing base image. We have to remember that FROM command is first line of any Dockerfile. Using this commend we are going to use Ubuntu existing image to build new image.

 

MAINTAINER: provide image maintainer email address who are going to maintain this image.

 

Run: we are installing dependency with nginx.

 

Expose: we know nginx default port is 80. That’s way we are exposing port 80 from docker image.

 

CMD: Running nginx in background.

 

Let’s build image now:


 

   Docker build -t RaselFirstImage:1.0 .

   # here RaselFirstImage is our image name and . is current directory where image is going to build.



Now need to run image with docker container



    Docker run –name testNginxContainer -p 80:80 RaselFirstImage:1.0

    # here testNginxContainer is our container name –p 80:80 exposing container 80 port for outside access and RaselFirstImage is our created own image name.



Docker compose: docker compose is tool for define and running multiple container, it’s a yaml file allow to build, run and configure multiple containers with one single command. With the help of Dockerfile we can execute command or build new image but we can’t run multiple container. In the real world application, we have to run multiple container at once. Suppose we are creation website using docker here we need php, mysql, redis, nginx etc container which is very difficult to handle with Dockerfile that’s why we need docker-compose.yaml to handle multiple containers.


Usefull docker Commends:

 

docker version

# showing current version of docker

docker info

# showing all the information about docker

docker

# see all the available comment

docker containers

# see the container related command list

docker image

#see image related comment list

docker pull <image name>: version

# pull image from dockerhub. Here version is optional

# example: docker pull nginx

docker run -p 8080:80 –name nginx-container -d <nginx>

# run comment make a container and run image

# first 8080 is local pc port which can be any port and second 80 is container port

# suppose nginx image port is 80 so 8080:80 -> localhost:8080 can access

docker logs <container id>

# see all logs related to specific container

docker ps

# showing all the running container

docker ps -a

# showing all the running and not running container

docker stop <container id or container name>

# stop specific docker container

docker start <container id or container name>

# start docker container

docker rm -f <container id or name>

# remove docker container completely

docker start/stop container1 container2

#multiple container start and stop

docker stop $(docker ps -q)

# all the running container will stop

docker start $(docker ps -q -a)

# all the stoped container will start

docker rm -f $(docker ps -q -a)

# remove all container at a time

docker top <container id or name>

#show the running process or PID inside this container

docker inspect <container id or name>

#see the details of container

docker stats<container id or name>

# see the live performance of a container.

# If we not pass container id or name it shows all running container live performance

docker logs <container or name>

# see the log information about container

alias dp=”docker ps”

# making an allies of any docker comment we can call dp instead of docker ps

docker run -it --name=nginx-cont nginx /bin/bash

# run container and go to container machine and do whatever

you want it's called interactive mode


docker exec -it nginx-cont /bin/bash

# running container bash access this comment will not work in gitbash

docker images

# view all images

docker images <image name>

# view specific image

docker image rmi <image id>

#remove image from machine

docker image rmi <image id1 image id2>

# remove multiple image from machine


docker image rmi -f <image id image id>

# remove multiple image from machine with force

docker image rmi -f $(docker images -q)

# remove all images

Read more about docker:

Docker Images, Containers, Architecture and Their Role in Modern Development


Rasel Hasan

Rasel Hasan

I am a seasoned Senior Software Engineer with over 6 years of expertise, currently contributing my skills at Bijoytech IT Ltd. My proficiency spans a diverse range of domains, including e-commerce, POS, ERP, custom software, payment solutions, and more...

Know More