Introduction to Docker
- Docker is an open-source platform that allows developers to build, package, and deploy applications as containers while ensuring that they run consistently across different environments,
- Containers are lightweight and efficient units of software that bundle together all the necessary code, libraries, and dependencies required to run an application.
- Docker works by isolating an application and its dependencies into a container, which can then be run on any machine that has the Docker platform installed.
- Docker containers are isolated from each other and from the host system, which helps to prevent conflicts between applications and ensures that the application runs consistently regardless of the underlying system.
- Docker uses a client-server architecture and comprises several components, including the Docker engine, Docker CLI, and Docker Hub,
- The Docker engine is responsible for building, running, and managing containers,
- Docker CLI provides a command-line interface for interacting with the Docker engine,
- Docker Hub is a cloud-based registry that allows developers to store and share container images, which can be used to quickly deploy applications to production environments,
Difference between Docker Containers and Virtual Machines?
VM:
- VM is a complete operating system that runs on top of a virtualization layer, such as VMware or VirtualBox, that emulates the underlying hardware.
- Each VM runs its own guest operating system and applications, which are isolated from other VMs running on the same host.
- Docker containers are a lightweight alternative to VMs that allow applications to run in an isolated environment without the overhead of a full OS.
- A Docker container consists of an application and its dependencies bundled together, which can be run in any environment that supports Docker.
- Docker containers share the host operating system kernel, which makes them much more lightweight than VMs, and they can be started and stopped much faster.
Size and speed: Docker containers are much smaller in size and start up much faster than VMs since they don't have to boot up an entire operating system.
Resource usage: VMs require more resources to run since each VM must have its own guest operating system, whereas Docker containers share the host operating system's resources.
Isolation: Both technologies provide isolation, but Docker containers are considered to be less isolated than VMs because they share the host's kernel.
Portability: Docker containers can be easily moved between different hosts and environments, making them more portable than VMs.
- A Docker image is a snapshot of an application and its dependencies at a specific point in time which includes everything needed to run an application, including the application code, libraries, and runtime environment.
- Docker images are built from Dockerfiles, which are text files that specify the instructions to build the image.
- A Docker container is a running instance of a Docker image that runs on top of the host operating system, sharing the kernel with other containers.
Dockerfile à (docker build cmd) à Docker Image à (docker run cmd) à Docker Container à (docker push cmd) à ACR (Azure Container Registry)
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=yourStrong(!)Password" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2019-latest
docker-compose.yml:
version: "3.9"
services:
sql-server-db:
image: enternalsregistry.azurecr.io/sqlrestoredb:2019-latest
container_name: sqlrestoredb_container
ports:
- "1455:1433"
environment:
SA_PASSWORD: "Hexagon@1"
ACCEPT_EULA: "Y"
Commands:
Ø
docker
build -t <ImageNameTobeCreated>:<tag> .
E.g.:
docker build -t Calcultor:Client .
Ø
docker images {list all the images}
Ø
docker run –name <container_name> -p
1433:1433 <image_name>:<tag>
{by default, latest tag will be used if you missed tagging}
{run command will look if you already have the image then it will run the
container, otherwise it will pull the image from docker-hub and then run the
container}
{-rm, we can use run command with -rm to run the container and remove it}
{-d, used to run in detached mode}
E.g:
docker run -e "ACCEPT_EULA=Y" -e
"MSSQL_SA_PASSWORD=yourStrong(!)Password" -p 1433:1433 -d
mcr.microsoft.com/mssql/server:2019-latest
docker run -i <image_name>:<tag> {to run the container in
interactive way}
Ø
docker inspect <container_name Or
initials> {detailed information of the container}
Ø
docker logs <container_name Or initials> {what
data is printing on interactive terminal mode}
Ø
docker stats <container_name Or initials>
{statistics of container}
Ø
docker ps {list all running active containers}
Ø
docker ps -a {list all active & inactive containers}
Ø
docker stop <container_name> {stop the
container}
Or,
docker stop <initials> {initials means some difference b/w containers,
like 3 letters of Container ID}
Ø
docker start <container_name> {start the
inactive container}
Or,
docker start <initials>
Ø
docker rm <container_name Or initials> {remove
only stop container}
Ø
docker rm <container_name Or initials> -f
{forcefully remove running container}
Ø
Remove all the active/inactive containers at one
go:
docker rm $(docker ps -a -q) -f
Ø
docker rmi <image_name> {remove image
having inactive containers}
Ø
Remove all the images at one go:
docker rmi $(docker images -q) {remove image having inactive containers,
otherwise use -f at last}
Ø
docker pull <image_name>:<tag> {use
to pull the image from docker hub, ACR, ..}
Ø
Verifying Files uploaded in docker container:
docker exec -it <container_name Or initials> /bin/sh
Dockerfile:
E.g 1:
FROM mcr.microsoft.com/dotnet/framework/runtime:4.8-windowsservercore-ltsc2019 AS build-env
WORKDIR /app
COPY ./bin/ .
ENTRYPOINT ["C:\\app\\Add.Client.exe"]
Note: AS <alias_name>: used in later stage, generally we use this in multistage builds
E.g 2:
FROM mcr.microsoft.com/mssql/server:2019-latest
ENV SA_PASSWORD=Hexagon@1
# USER mssql
# Copy the existing database backup file to the docker image directory
COPY ./TestDB.bak /var/opt/mssql/backup/
# Launch SQL Server, confirm startup is complete, restore the database, then terminate SQL Server.
RUN ( /opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" \
&& /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P $SA_PASSWORD -Q 'RESTORE DATABASE TestDB FROM DISK = "/var/opt/mssql/backup/TestDB.bak" WITH MOVE "TestDB" to "/var/opt/mssql/data/TestDB.mdf", MOVE "TestDB_Log" to "/var/opt/mssql/data/TestDB_log.ldf", NOUNLOAD, STATS = 5' \
&& pkill sqlservr
- docker-compose up: Build and start the containers defined in your docker-compose.yml file.
docker-compose up -d: Start the containers in the background (detached mode). - docker-compose down: Stop and remove the containers defined in your docker-compose.yml file.
- docker-compose stop: Stop the containers defined in your docker-compose.yml file.
- docker-compose start: Start the containers defined in your docker-compose.yml file.
- docker-compose restart: Restart the containers defined in your docker-compose.yml file.
- docker-compose ps: List the status of the containers defined in your docker-compose.yml file.
- docker-compose logs: View the logs of the containers defined in your docker-compose.yml file.
- docker-compose build: Build the images for the containers defined in your docker-compose.yml file.
- docker-compose up --build: Force a rebuild of the containers before starting them.
- docker-compose push: Push the images for the containers defined in your docker-compose.yml file to a Docker registry.
- docker-compose pull: Pull the images for the containers defined in your docker-compose.yml file from a Docker registry.
- docker-compose rm: Remove the containers defined in your docker-compose.yml file.
There are other commands available.
Note:
The difference between docker push and docker-compose push is that docker push is used to push a single image, while docker-compose push is used to push multiple images at the same time.
Additionally, docker push requires you to specify the image name and tag, while docker-compose push automatically uses the image tags defined in your docker-compose.yml file.
Docker Registry:
Push images to ACR:
- Create Container Registry and enable Admin user from the Access Keys to use in Azure DevOps pipelines,
- Pull/Create your docker image,
Login to ACR:
docker login <sever_name_ACR> -u <user_name> -p <password> {login to ACR}- Before pushing to ACR (or DockerHub), we have to tag our image w.r.t our Container Registry.
docker tag <image_name_including_tag> <server_name_ACR>/<image_name_including_tag> - Push the docker image to ACR
docker push <tagged_image_name>
- Process of creating a new image from the current state of a container,
- When you make changes to a running container, those changes are only saved within the container's storage layer and are not saved as part of the container image itself,
- However, by using the "docker commit" command, you can create a new image that includes those changes, making it easy to create a new container that starts with those changes already applied.
docker commit <container's name or ID> my-new-image
- After running this command, you can use the new image to create new containers that include the changes made to the original container.
Note: committing a container creates a new image that includes all of the changes made to that container, including any new files, modifications to existing files, and changes to the container's configuration.