Docker
What is Docker?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure to deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker's shipping, testing, and deploying code methodologies, you can significantly reduce the delay between writing and running code in production.
What is a container?
A way to package an application with all the necessary dependencies and configuration. Portable artifacts are easily shared and moved around.
Container vs Image
The container is running env for an image. The container contains the file system, environment config, and application image.
Where do containers live?
Container repository
Important Commands:
docker pull <image name> → to pull the app image locally.
docker run <image name> → to run the docker image and pull from the repo if it is not present in the local
docker ps → to see all the running containers
docker images → Check all the existing images on the local
docker run -d <image name> → return the container ID as output and run the container in detached mode
docker stop <container_id>
docker start <container_id>
docker ps -a → Show all the containers that are running or not running
The container and host port are different
docker run -p<host_port>:<container_port> -d <image_name>
docker logs <container_id>/<container_name> → To see the logs
docker run -p<host_port>:<container_port> --name <container_name> -d <image_name> → To name your container
docker exec -it <container_id>/<container_name> /bin/bash → to login to bash of container
docker network ls → To show the docker network
docker network create <network-name> → To create docker network
Image :
Its an artifact
#commands
##create a docker network
docker network create mongo-network
##start MongoDB
docker run -d \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
--net mongo-network \
--name mongodb \
mongo
## start mongo-express
docker run -d \
-p 8081:8081 \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password \
-e ME_CONFIG_MONGODB_SERVER=mongodb \
--net mongo-network \
--name mongo-express \
mongo-express
Recommended by LinkedIn
What is a docker-compose.yml file?
A docker-compose file is a YAML file that allows us to deploy multiple Docker containers at the same time. It allows to deploy, combine, and configure multiple docker containers at the same time. The Docker "rule" is to outsource every single process to its own Docker container.
Take for example a simple web application: You need a server, a database, and PHP. So you can set three docker containers with Apache2, PHP, and MySQL.
The advantage of Docker Compose is easy configuration. You don't have to write a big bunch of commands into Bash. You can predefine it in the docker-compose.yml
E.g.
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_DB: DB_name
POSTGRES_USER: DB_user
POSTGRES_PASSWORD: DB_password
volumes:
- db-data:/var/lib/postgresql/data
- db
What is Dockerfile?
Docker can build images automatically by reading the instructions from a Docker file. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
#DockerFile Commands
ADD → Add local or remote files and directories.
ARG → Use build-time variables.
CMD → Specify default commands.
COPY → Copy files and directories.
ENTRYPOINT → Specify the default executable.
ENV → Set environment variables.
EXPOSE → Describe which ports your application is listening on.
FROM → Create a new build stage from a base image.
HEALTHCHECK → Check a container's health on startup.
LABEL →Add metadata to an image.
MAINTAINER → Specify the author of an image.
ONBUILD → Specify instructions for when the image is used in a build.
RUN → Execute build commands.
SHELL → Set the default shell of an image.
STOPSIGNAL → Specify the system call signal for exiting a container.
USER → Set the user and group ID.
VOLUME → Create volume mounts.
WORKDIR → Change the working directory.
Docker Volumes :
Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker
Types of docker volume
1. Host Volume - docker run
-v /home/mount/data:/var/lib/mysql/data
2. Annonymous Volume - docker run
-v /var/lib/mysql/data
3. Named Volumes - docker run
-v name:/var/lib/mysql/data
Dockerfile vs DockerCompose
Dockerfile is a script of commands used to assemble a Docker image, while compose helps define and run multi-container applications.