Docker Introduction
Nathan Luong | April 15, 2024 |10
Glossary
Image vs Container
Image | Container |
---|---|
Docker image | Docker Container |
The Actual Package | Actually start the application |
The artifact that can be moved around | The Container is a running environment for Image |
Images usually form layers | |
For example postgres Image is built on top of base alpine |
📝
This is new Data
All data will be lost when a container is destroyed. Need to use volume to persist data
Ports
Container Port needs to be bind onto a port of the host
Commands
Docker Run
Create and run a new container
docker run redis
: Starts the Redis image in containerdocker run -p 6000:6379 redis
: Bind Redis container port 6379 onto port 6000 of hostdocker run -d -p 6000:6794 redis
: Run Redis on port 6000 in detach modedocker run -d -p 6000:6794 redis --name redis-two redis
: Run Redis image on port 6000 in detach mode, with the container name ofredis-two
instead of the container ID
Docker Exec
docker exec -it redis-two /bin/bash
: Launch the interactive terminal with bash inside of the containerexit
: Exit the docker terminal
Docker Run
## create 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 MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
--net mongo-network \
--name mongo-express \
mongo-express
- Can be created all at once with Docker Compose and
yaml
file
General
-
docker pull redis
: Pulls the latest Docker Images of Redis from Docker Hub -
docker pull redis:123
: Pull Redis version 123 -
docker start <containerID>
: Start the container -
docker stop <containerID>
: Stop the container -
docker logs <cid>
: print the logs of the container -
docker images
: Show all Docker images -
docker ps
: List all running Docker container -
docker ps -a
: List all Docker container running and not running -
docker network create mongo-network
: Create a new network called mongo-network -
docker network ls
: List all docker networks -
docker build -t my-app:1.0 .
: Build a Docker Image call my-app with the Dockerfile located in the current directory
Docker Workflow
Simple Workflow
- Developing the application
- Push to GitHub
- Jenkins pickup the code and build the Docker Image
- The Docker pushed to a private Docker Repository
- The Docker Image got deployed onto a dev server
- Tester can test the app on Dev Server
Dockerfile
- A blueprint for building docker image
## Install node
FROM node
## Set the environment variable
ENV MONGO_DB_USERNAME=admion \
MONGO_DB_PWD=password
## Execute any linux command with RUN
## Create a directory name home/app in the Docker Container
## We can have multiple run commands but only one CMD
RUN mkdir -p /home/app
## Copy a file from the host into the docker container
COPY . /home/app
## Entry Linux command to run in the container
## Start the app with "node server.js"
CMD ["node", "server.js"]
FROM alpline:3.10
ENV NODE_VERSION 13.1.0
RUN ...
COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["node"]
RUN
vs CMD
RUN
can execute any Linux commands and there can be multiple RUN commandsCMD
is an entry command which every container can only have 1 of
Docker Volumes
Problems
- Containers runs on host and have a virtual file system
- The data will be gone when the container got destroyed, restarted
Solutions
- Create a physical file system on the host machine
- Folder in physical host file is mounted into the virtual file system of Docker
- The data that is in the Virtual File System will be duplicated on the Host File System
3 Volume Types
Host Volume
- Developer decide which folder from the host to get mounted onto the container
## Mount host /home/mount/data onto virtual /var/lib/mysql/data
docker run -v /home/mount/data:/var/lib/mysql/data
Anonymous Volumes
- For each container a folder is generated that gets mounted
## Pass in the container folder path,
## the volumes on the host is automatiacally generated
docker run -v /var/lib/mysql/data
Named Volumes
- For each container a folder on host is generated that gets mounted
- The folder will be named whatever, for example
lmao
- The volume can be referenced by its name
📝
Share Data
- A volume, with the same reference can be mounted on multiple containers if they need to share data.
🎉
Should be used in production
- There are benefits for Docker to manage the Volumes for developers.
- These Names can be reference inside a Docker Compose file
## Pass in the container folder path,
## the volumes on the host is automatiacally generated
docker run -v lmao:/var/lib/mysql/data