Skip to main content

Command Palette

Search for a command to run...

Docker

Published
3 min read

Firstly we have to start the Docker service

systemctl start docker

To check the docker version

docker --version

To get the image from the registry

docker pull <<image_name>>

To get the image from local if available if not get image from registry and create a container.

docker run <<image_name>>

To check available images in our machine

docker images

To check all the containers available

docker ps -a

To check running containers

docker ps

To check exited containers

docker ps --filter "status=exited"

To remove an image

docker rmi image_name

To remove multiple images

docker rmi image1,image2

To remove all images

docker rmi $(docker images)

To create a container from the image

docker run --name container_name image_name

--name tag is used to give a name to the container

To create a container in interactive and detached mode

docker run -itd --name container_name image_name

To start a container

docker start container_name

To stop a container

docker stop container_name

To remove a container

docker rm container_name

To create a docker image from an available container

docker commit container_name image_name

To allocate memory and CPU to the container when we create it

--memory and --cpus

docker run --name container_name --memory=100M --cpus=0.25 image_name

Dockerfile

  • Dockerfile is a text file that contains set of instructions

  • Using Dockerfile we can automate image creation.

How it works

  • First, we need to create a Dockerfile.

  • By Building Dockerfile we'll get an image.

  • Create a container using that image.

Dockerfile Components

  1. FROM: it is used to define base image ex: ubuntu, MySQL, java, httpd, nginx etc.

  2. LABEL: it is used to specify the maintainer details.

  3. RUN: it is used to run commands when we build the image(creating an image).

  4. COPY: it is used to copy files from the server to the container.

  5. ADD: it is used to copy files from the server to the container and also we can download them from the internet.

  6. WORKDIR: it creates a directory and we directly move into that directory.

  7. EXPOSE: publish the port numbers.

  8. CMD: it is used to execute commands while we run the image(creating the container).

  9. ENTRYPOINT: it is used to execute commands while we run the image(creating the container).

Diff bw RUN and CMD

RUN --> commands executed while creating image.

CMD --> commands executed while creating container.

Diff bw CMD and ENTRYPOINT

Both are executed at run time but entrypoint takes the highest priority and we can also pass values to the command at runtime