Docker basics

natnat
3 min readFeb 18, 2024
  1. What is docker? how does it differ from traditional virtualisation?
  2. Purpose and usage of docker images and containers
  3. How to build docker image from Dockerfile
  4. Difference between CMD and ENTRYPOINT
  5. Link containers in docker

What is docker? how does it differ from traditional virtualisation?

With the help of hypervisors Virtual machines provide isolation of an entire server.

Docker lets you run containers on any OS. it has everything it needs inside(network, dependencies, file system)

https://www.docker.com/blog/containers-and-vms-together/

Purpose and usage of docker images and containers

docker image is a blueprint of container

docker image is created from dockerfile. docker image has multiple layers to keep file size small, while container has its layer. in which we have read-write access.You can make changes within the container to be isolated from other containers that use the same image.

Using both of them we can:

  • scale application horizontally by running multiple instances of containers based on the same image.
  • automate CI/CD pipelines by using different images for dev, test and prod
  • tag an manage different versions of your image, so you can also roll back or specify different versions as needed

How to build docker image from Dockerfile

dockerfile form simple react app:

FROM node:alpine

WORKDIR /var/www/html

COPY . .

RUN npm install

CMD npm start

Next run command:

docker build -t myimage .

docker build

-t myimage — name docker image

. — where dockerfile is located

Difference between CMD and ENTRYPOINT

CMD

when running container from an image, this instruction sets command/commands to be executed.

syntax:

CMD [“command”, “param1”, “param2”] (exec mode)
CMD ["param1", "param2"](parameters to ENTRYPOINT)
CMD command param1 param2 (shell mode)
  • If the Dockerfile has multiple CMD instruction, it will only use the last one
  • specifies default arguments or commands to run when the container starts.

ENTRYPOINT

values you specify after ENTRYPOINT will be values that get passed to:

docker run <image>

shell form of ENTRYPOINT prevents any CMD cli arguments from being used.

  • If you want to use both of them together you should use exec mode.
  • If both of them are used, ENTRYPOINT will ignore CMD arguments unless ENTRYPOINT decides to use it

summary: ENTRYPOINT is the process that executed inside container. CMD is the default set of arguments that are supplied to ENTRYPOINT process.

Link containers in docker

https://docs.docker.com/engine/tutorials/networkingcontainers/

You can link containers by creating docker network

Docker network

container cant see what kind of network its attached to and neither if thier peers are docker workloads or not. however it can see network interface with an ip address, gateway, routing table, dns services…(unless container uses ‘none’ network driver)

you can define custom networks and join your containers there.

docker network create -d bridge my-network
docker run --network=my-network -itd --name=container3 nginx

Drivers

  • bridge — default
  • overlay — connect multiple docker daemons together
  • host — no isolation between container and host
  • none — complete isolation from container and host
  • ipvlan — full control over ipv6 and ipv4 addressing
  • macvlan — assign MAC address to container

link containers directly

  • - - network container:<name|id>

following command runs nginx container and binds it to localhost and connects nginx-cli over it.

docker run -d --name nginx my/nginx --bind 127.0.0.1
docker run --rm -it --network container:nginx my/nginx-cli -h 127.0.0.1

--

--