https://www.docker.com/101-tutorial
docker ps (= docker container ls)
docker images (= docker image ls)
vi Dockerfile
docker build -t my-app
docker tag original-name new-name
// docker -d -p host:container image-name
docker -d -p 80:80 my-app
// login to docker
docker login
// push to remote
docker push user-name/repo-name
// get container ids
docker ps
// stop container, remove container
docker stop container-id
docker rm container-id
(= docker rm -f container-id)
docker logs -f container-id
At the root of the app proeject, create a file named Dockerfile
FROM node:10-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "/app/src/index.js"]
Host(Volume) <-Data-> Container
docker volume create volume-name
docker run -dp 3000:3000 -v volume-name:/host/volume/path image-name
docker volume inspect volume-name
In general, each container should do one thing and do it well.
Node.js Container + MySQL Container => App
If two containers are on the same network, they can talk to each other. If they aren't, they can't.
Node.js Container <-Network-> MySQL Container
(remember each container has its own IP address. To figure it out, we're going to make use of the nicolaka/netshoot container, which ships with a lot of tools that are useful for troubleshooting or debugging networking issues.)
(--network-alias=[] : Add network-scoped alias for the container)
docker run -d \
--network app-name --network-alias network-alias \
-v app-mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DATABASE=myapp \
mysql:5.7
docker exec -it <mysql-container-id> mysql -p
connect nicolaka/netshoot
container to network-name
network
docker run -it --network network-name nicolaka/netshoot
Inside the container, We're going to look up the IP address for the hostname mysql
dig mysql
In the "ANSWER SECTION", you will see an A record for mysql that resolves to
mysql. 600 IN A XXX.XXX.XXX.XXX
docker run -dp 3000:3000 \
-w /app -v $PWD:/app \
--network network-name \
-e MYSQL_HOST=network-alias \
-e MYSQL_USER=root \
-e MYSQL_PASSWORD=secret \
-e MYSQL_DB=myapp \
node:10-alpine \
sh -c "yarn install && yarn run dev"
docker exec -it <mysql-container-id> mysql -p myapp
Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.
At the root of the app project, create a file named docker-compose.yml
version: "3.7"
services:
app:
image: node:10-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_DB: myapp
mysql:
image: mysql:5.7
volumes:
- volume-name:/host/volume/path
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: myapp
volumes:
todo-mysql-data:
docker-compose up -d
docker-compose down