Docker enables consistent and portable application deployment by packaging software with its dependencies, eliminating the reliance on pre-installed components on a machine. This ensures applications run seamlessly across different environments, streamlining development, and reducing compatibility issues.
Docker is an open-source platform that automates the deployment, scaling, and management of applications by using containers. Containers are lightweight, portable, and self-sufficient units that include an application and its dependencies, allowing it to run consistently across different environments. Docker simplifies and streamlines the process of creating, packaging, and deploying applications in containers.
Docker is needed for several reasons, including:
-
Consistency: Docker containers ensure that applications run the same way, regardless of the environment they are deployed in. This consistency helps to minimize the "it works on my machine" problem, which often occurs when software behaves differently on development, testing, and production environments.
-
Portability: Docker containers can be easily moved between different systems, as they contain all the dependencies required to run the application. This portability simplifies the deployment process and makes it easier to share and distribute applications.
-
Isolation: Docker allows for better isolation between applications, as each container runs independently from the others. This isolation reduces the risk of conflicts between different applications or dependencies, making it easier to manage and maintain multiple applications on a single system.
-
Scalability: Docker facilitates horizontal scaling by making it easy to create multiple instances of a containerized application. This scalability allows for better load balancing and handling of traffic spikes, ultimately improving application performance.
-
Efficiency: Containers are generally more resource-efficient than virtual machines, as they share the host system's kernel and do not require a separate operating system for each application. This efficiency translates into lower resource usage and faster start-up times for containerized applications.
docker version
docker run <image name>
docker run <image name> command!
docker run hello-world
docker run busybox echo hi there
docker run busybox ls
docker run hello-world echo hi there
docker run hello-world lsdocker ps
docker run busy-box ping google.com
docker ps
# kill (ctrl + c) the running docker
docker ps docker create hello-world
docker start -a hello-world
docker stop <id>
docker kill <id>docker run redis
docker exec -it <id> <command>
docker exec -it <id> redis-cli
docker exec -it <id> sh
# then, inside the shell
redis-cli
# You can exit the cli with ctrl/cmd + c
# You can exit the shell with alt/ctrl + ddocker run -it busybox sh
> ls
# in another terminal
docker run -it busybox sh
> touch hello_world
> ls
# back to the first container
> ls
# we should not see the new fileThe process when creating a docker file
- Specify a base image (compares with an OS)
- Run some commands to install all programs needed
- Specify commands to run the container
Create a new project.
Add a Dockerfile
- Specify a base image (FROM)
- Run some commands to install all programs needed (RUN)
- Specify commands to run the container (CMD)
FROM alpine
# Download and install dependencies
RUN apk add --update redis
# Tell the image what to do when it starts as a container
CMD ["redis-server"]
Then run in the terminal
docker build .
# when you get the new id of the container
docker run <id>FROM alpine
# Download and install dependencies
RUN apk add --update redis
RUN apk add --update gcc
# Tell the image what to do when it starts as a container
CMD ["redis-server"]
FROM alpine
RUN npm install
CMD ["npm", "start"]
FROM node:alpine
RUN npm install
CMD ["npm", "start"]
We get an error when running:
docker build .# ERROR
> [2/2] RUN npm install:
#6 0.740 npm ERR! Tracker "idealTree" already exists
#6 0.741
#6 0.741 npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2023-05-11T17_25_39_283Z-debug-0.logThe reason is that we don't have any package.json to install
FROM node:alpine
COPY ./ ./
RUN npm install
CMD ["npm", "start"]
docker build . works
docker run <id> works, but we cannot reach that port
docker run -p 3001:3001 <id>
FROM node:alpine
WORKDIR /usr/src/app
COPY ./ ./
RUN npm install
CMD ["npm", "start"]
FROM node:alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY ./ ./
CMD ["npm", "start"]
docker build -t <tag> .