What are docker image and docker containers?
Docker Images are Templates used to create Docker Container
Container is a running instance of an image
- Version
docker -v
- List of Images
docker images
- List of commands for images
docker images --help
- Pull an image
docker pull ubuntu
- Filtering images
docker images -f "dangling=true"
Dangling images are images which are associated with any containers
- Running an image
docker run --name MyUbuntu -it ubuntu bash
- Inspecting an image
docker inspect ubuntu
- Remove an Image
docker rmi ubuntu
- Show the list of running containers
docker ps
- Show the list of all containers
docker ps -a
- Start a container
docker start <container name/id>
- Stop a container
docker stop <container name/id>
- Pause a container
docker pause <container name/id>
- Resume a container
docker unpause <container name/id>
- Displaying stats
docker stats <container name/id>
- Remove a container
docker rm <container name/id>
- Get the history of an image
docker history <image name/id>
What is a dockerfile?
A Dockerfile is a simple text file with instruction to build images
Automation of docker image creation
How to create a Dockerfile?
-
Step 1 : Create a file named Dockerfile
-
Step 2 : Add instructions in Dockerfile
-
Step 3 : Build Dockerfile to create image
-
Step 4 : Run image to create container
FROM ubuntu
MAINTAINER ramanuj das <ramanujds9@gmail.com>
RUN apt-get update
CMD ["echo", "Hello World...! Image Running.."]
How to build image from Dockerfile?
docker build -t myUbuntu:1.0 .
#t flag is used for tagging an image
Running the image:
docker run <image id>
Docker volumes are used for decoupling container from storage
Command to create a docker volume:
docker volume create myVolume1
Running jenkins with the volume:
docker run --name myJenkins1 -v myVolume1:/var/jenkins_home -p 9000:8080 -p 50000:50000 jenkins
docker pull mysql
docker run -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=mydb -e MYSQL_USER=docker -e MYSQL_PASSWORD=password -p 3308:3306 --name mysql mysql
-
Creating image through Spring Boot -
mvn spring-boot:build-image
-
Running docker image -
-
Link Based Communication -
docker container run -p 5000:5000 -e RDS_HOSTNAME=mysql -e RDS_PORT=3306 --link=mysql --name book-store book-store-server:0.0.1-SNAPSHOT
-
Host Based Communication -
docker container run -p 5000:5000 --network=host book-store-server:0.0.1-SNAPSHOT
-
Custom Network
- Creating a Custom Network :
docker network create book-store-mysql-network
- Running Mysql Container on that network -
docker run -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=mydb -e MYSQL_USER=docker -e MYSQL_PASSWORD=password -p 3308:3306 --name mysql --network=book-store-mysql-network mysql
- Running book-store on that network -
docker container run -p 5000:5000 -e RDS_HOSTNAME=mysql -e RDS_PORT=3306 --network=book-store-mysql-network -d --name book-store book-store-server:0.0.1-SNAPSHOT
docker run -d -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=mydb -e MYSQL_USER=docker -e MYSQL_PASSWORD=password -p 3308:3306 --name mysql --volume mysql-db-volume:/var/lib/mysql --network=book-store-mysql-network mysql
How to run an Angular Application in Docker?
- Build the Angular App
ng build [app-name] --prod=true
- Create a Dockerfile in the root directory
FROM nginx
COPY dist/[app-name] /usr/share/nginx/html
- Build the image
docker build -t [image-name] .
- Run the image
docker run -p 80:80 [image-name]
What is Docker compose?
- Tool for defining and running musti-container Docker application
- It uses yaml file to configure application services(docker-compose.yml)
- Can start all services with a single command
- Can scale up selected services when required
- Step 1 : Create the docker compose file (docker-compose.yml)
- Step 2 : Check the validity of the file using command:
docker-compose config
- Step 3 : Run docker-compose yml file using command:
docker-compose up
- Step 4 : Stop docker-compose using command:
docker-compose down
docker-compose.yml exmaple:
version: '3'
services:
web:
image: nginx
ports:
- 9090:80
database:
image: redis
How to use scaling?
docker-compose up -d --scale database=4
version: '3.7'
services:
book-store-frontend:
image: book-store-client-app
#build:
#context: .
#dockerfile: Dockerfile
ports:
- "4200:80"
restart: always
depends_on: # Start the depends_on first
- book-store-spring-boot
networks:
- book-store-app-network
book-store-spring-boot:
image: book-store-server:0.0.1-SNAPSHOT
#build:
#context: .
#dockerfile: Dockerfile
ports:
- "5000:5000"
restart: always
# depends_on: # Start the depends_on first
# - mysql
environment:
RDS_HOSTNAME: mysql
RDS_PORT: 3306
RDS_DB_NAME: mydb
RDS_USERNAME: docker
RDS_PASSWORD: password
networks:
- book-store-app-network
mysql:
image: mysql
command: --default-authentication-plugin=mysql_native_password
ports:
- "3308:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_USER: docker
MYSQL_PASSWORD: password
MYSQL_DATABASE: mydb
MYSQL_ROOT_HOST: 192.168.0.48/255.255.255.248
# volumes:
# - mysql-db-volume:/var/lib/mysql
networks:
- book-store-app-network
# network:
# ipv4_address: 172.20.0.2
# Volumes
# volumes:
# mysql-db-volume:
networks:
book-store-app-network:
- Docker-compose Configuration
docker-compose config
- Docker-Compose Images Used
docker-compose images
- Docker-Compose Container Used
docker-compose ps
- Docker-Compose Pause, Resume, Stop, Kill
docker-compose pause
docker-compose unpause
docker-compose stop
docker-compose kill
- Docker-Compose Events
docker-compose events
- Build Images from Dockerfile
docker-compose build