Docker
Concepts

Basic Concepts

Tags

tags of same image could be different for example Nginx have “1.11.0 , 1.11 and latest” all are same.

To create own image and upload to hub

docker image tag --help
# | {username}/{name of image you want set}
docker image tag nginx hamidmehmood21/nginx
docker login
docker image push hamidmehmood21/nginx
# | :testing means next version or tag
docker image tag nginx hamidmehmood21/nginx:testing
docker logou t

for private repository you have to manual create repository first and mark it private.


Container Lifetime & Persistent Data

Container are usually unchangeable and short-live

but what about database or unique data?

docker give us features to ensure these “separation of concerns” known as persistent data

Two ways : Volumes and bind Mounts Volumes : make special location outside of container UFS | volumes need manually deletion | there is extra step

Bind Mounts: link container path to host path

Volume :

  • 4 use cases of volume
    1. Mount a local directory to provide files to the container. i.e providing source code to container during development or deployment. (allow live editing)
    2. Share data between multiple containers using the same volume.
    3. Include pre-existing data in a container by mounting directories or files.
    4. Persist container data to the host machine for data retention.
  • naming when you create container through Mysql image then MySQL image include VOLUME command for data then it give name to volume By default which is hard. So if we want to give easy name in this way we able to use DB data on another container again and again.
# env is required for mysql | -v {name of volume}/{VOLUME command present in sql}
docker container run -d --name mysqlhamid -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql

Bind Mounts

Maps a host file or directory to a container file or directory. Basically just two locations pointing to the same file(s). Can’t use in docker file, must be at container run

# pwd means print working directory (it is a shell thing not docker thing)
docker container run -d --name nginxhamid -p 80:80 -v ${pwd}:/usr/share/nginx/html nginx

Docker Compose

one dockerfile only fetch and run one image so we need docker compose.

configure relationship between containers

  • versions benefits Version 2 of Docker Compose provides benefits such as custom networks and named volumes for improved network configuration and persistent data storage. Version 3 introduces service scaling and deploy constraints, allowing high availability and control over service placement in Docker Swarm. Version 3.1 offers enhancements like deploy update configurations for seamless rolling updates and customizable healthchecks for monitoring container health.
# if you need more feature you will add 2,3,3.1
version:"3.1" # if no version is specificed then v1 is assumed. Recommend v2 minimum
 
services: # containers. same as docker run
 servicename: # a friendly name. this is also DNS name inside network | same as --name
  image: #Optional if you use build:
  command: # Optional, replace the ddefault CMD specified by the image
  environment: # Optional, same as -e in docker run
  volumes: # Optional, same as -v in docker run
 servicename2:
 
volumes: # Optional, same as docker volume create
 
networks: #Optional same as docker network create

examples:

# same as
# docker run -p 80:4000 -v $(pwd):/site nginx
 
services:
	hamidi:
		image: nginx
		volumes:
			- .:/site
		ports:
			- "80:4000"
 

Popular command

docker compose up
# also delete volumn
docker compose down -v