Docker for Beginners
Container
- Container is a running environment for IMAGE
- port binded: talk to application running inside of container,
Port 5000
- virtual file system
- contains application image: postgres, redis, mongo
- in order to start the application, you will need a container so that the user can connect to the image
Basic comands
docker images
prints out all images on the machine
docker run <IMAGE>
runs the IMAGE, e.g. redis
1 | ~ docker run redis |
docker ps
prints out the running container
1 | ~ docker ps |
ctrl + c
stops the container from running
docker run -d <IMAGE>
runs IMAGE in detached mode. ctrl + c
does not stop the IMAGE.
docker stop <CONTAINER_ID>
ContainerID is returned by docker ps
docker start <CONTAINER_ID>
starts the specific container
docker ps -a
shows all the containers running or not running, so you can restart it
docker run <IMAGE>:<VERSION>
if the image is not on local machine, this command pulls image and starts container
docker run -d -p <HOST_PORT>:<CONTAINER_PORT> --name <NAME> <IMAGE:VERSION>
e.g. docker run -d -p 6000:6379 --name redis-latest redis:4.0
names the containers
1 | ~ docker ps |
docker rmi <IMAGE>
docker rm <CONTAINER>
must remove container first to remove image
Container Port vs Host Port
- Multiple containers can run on your host machine
- Your laptop has only certain ports available
- Conflict when same port on host machine, but you can have the same container ports
- After binding host port to container port, use the port of the host. The host will forward the request to container.
Binding ports
docker run -p <HOST_PORT>:<CONTAINER_PORT> <IMAGE:TAG>
e.g. docker run -p 6000:6379 redis:4.0
Trouble Shoots
docker logs <CONTAINER_ID>
or docker logs <CONTAINER_NAME>
docker exec -it <CONTAINER_ID> /bin/bash
or docker exec -it <CONTAINER_NAME> /bin/bash
or docker exec -it <CONTAINER_NAME> /bin/sh
gets the container terminal as root user
exit
exits the terminal
Docker workflow
Demo Javascript, Node, MongoDB, MongoExpress project Local development
docker pull mongo
docker pull mongo-express
Docker network
MongoDb and Mongo Express UI are in the Isolated Docker Network. They can communicate through just the container name without localhost or port number. Application run outside of the server, such as node.js, is going to communicate through the port number.
Later if we want to deploy our application, we need to package them into the same container.
docker network ls
1 | ~ docker network ls |
docker network create <NETWORK_NAME>
e.g. docker network create mongo-network
1 | ~ docker network create mongo-network |
Run Mongo Containers
docker run -p 27017:27017 -d \ -e MONGO_INITDB_ROOT_USERNAME=admin \ -e MONGO_INITDB_ROOT_PASSWORD=password \ --name mongodb \ --net mongo-network mongo
1 | ~ docker run -p 27017:27017 -d \ |
docker logs e1c2847f51a26490b9dcc1d068d94c7c74fca35bff95f730fb307c61ad280b29
to see if the container runs successfully
docker ps
gets the MONGODB_CONTAINER_NAME under NAMES, which is mongodb
1 | ~ docker ps |
docker run -d \ -p 8081:8081 \ -e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \ -e ME_CONFIG_MONGODB_ADMINPASSWORD=password \ -e ME_CONFIG_MONGODB_SERVER=<MONGODB_CONTAINER_NAME> \ --net mongo-network \ --name mongo-express \ mongo-express
1 | ~ docker run -d \ |
Go to localhost://8081
Or use MongodbCompass
Connect to mongodb://admin:password@localhost:27017
Create Database
Connect Node with Mongo
Check logs
docker logs <MONGO_CONTAINER_ID> | tail
docker logs <MONGO_CONTAINER_ID> -f
streams the logs. You can put a line in terminal and do some operations. Logs after the line are printed for the new opertion.
Make the docker process easier
Docker compose
Docker compose takes care of creating a common network. We don’t need to worry about the --net
option.
1 | # docker-compose.yaml |
docker-compose -f <DOCKER_COMPOSE_FILENAME> up
1 | docker-compose -f docker-compose.yaml up |
After docker container is restarted, all of the data are gone. There is no data consistency with docker containers.
docker-compose -f <DOCKER_COMPOSE_FILENAME> down
shuts up all the containers and remove the network.
Build Docker Image with Node
Dockerfile
1 | FROM node:13-alpine #image name |
docker build -t my-app:1.0 .
1 | techworld-js-docker-demo-app git:(master) ✗ docker build -t my-app:1.0 . |
docker run my-app:1.0
Demo Javascript, Node, MongoDB, MongoExpress project, Push to Docker Registry AWS ECR
Image Naming in Docker Registries
tag the image so docker push
push to aws instead of docker hub
docker tag my-app:1.0 <REMOTE_REPOSITORY>/my-app:1.0
Create a repository
Use web or use aws-cli
aws ecr create-repository --repository-name my-app
aws ecr get-login-password
docker login -u AWS -p <password> https://<accountid>.dkr.ecr.<your-default-region>.amazonaws.com
Use the output from the previous command.
Push
docker push <REMOTE_REPOSITORY>/my-app:1.0
If you changed something
docker build -t my-app:1.1
docker tag my-app:1.1 <REMOTE_REPOSITORY>/my-app:1.1
docker push my-app:1.1 <REMOTE_REPOSITORY>/my-app:1.1
Deploy
Add new container to docker-compose.yaml
1 | # docker-compose.yaml on development server |
Change the code to connect node to mongodb
Run the remote container at local machine
docker-compose -f docker-compose.yaml
Docker Volumes
If container restarts, the data is gone. We need docker volume to solve that.
Folder in physical host file system is mounted into the virtual file system of Docker.
3 Volume Types
docker run -v /home/mount/data:/var/lib/mysql/data
mount host/home/mount/data
to container. You decide where on the host file system the reference is made.docker run -v /var/lib/mysql/data
just specify container directory. Host file automatically created by Docker, which is called anonymous volumes.docker run -v <NAME>:/var/lib/mysql/data
Named Volumes. You can reference the volume by name.
Use Docker Volume in Docker Compose
1 | version: '3' |
Docker Volume Locations
linux: /var/lib/docker/volumes
mac:: /var/lib/docker/volumes
Docker for Mac creates a Linux virtual machine and stores all the Docker data there
ls /var/lib/docker
returns “No such file or directory”
screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty
runs the Linux vm
ls /var/lib/docker/volumes
again, you see can see the volumes
ctrl + a+ k
then press y
you can exit the screen
Source
Docker Tutorial for Beginners [Full Course in 3 Hours]
赞赏一下