Create a Docker Compose
- In the root folder, create a file with the name docker-compose.yaml
- Type this code inside the docker-compose.yaml file.
version: "4.28"
services:
api:
build: ./api
container_name: api_container
ports:
- '4000:4000'
volumes:
- ./api:/app
- /app/node_modules
- Version: "4.28" is the version of the Docker desktop currently downloaded on your computer.
- build: ./api is the path of your project.
- container_name: api_container is the name of the container in your api project folder.
- ports: - '4000:4000' are ports of both your local and container. The left side port is for the local port, and the right side is for the container port.
- - ./api:/app ./api is the directory path of your local project, and /app is the directory path of your project inside the container.
- - ./app/node_modules Adding the second volume path is to ignore the directory path if the user accidentally deletes the file.
Run the docker-compose.yaml
This code create a container
docker compose up
Stop the container
docker compose down --rmi all -v
- --rmi all to remove the all the images created from the docker compose
- -v to delete all the volumes created from the docker compose.
Create a Docker image file with Layer Cache
- Create a file named DockerFile and copy the code attached below inside the DockerFile file
FROM node:17-alpine
RUN npm install -g nodemon
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 4000
# required for docker desktop port mapping
CMD ["npm", "run", "dev"]
Explanation:
1. WORKDIR
WORKDIR is code that will create a directory inside the image. So based on the code example above, inside the Docker image, the source code will be stored in the app directory.
2. EXPOSE
EXPOSE is a code to indicate a port number inside the container. So based on the code example above, in the container, the port number of your application is 4000.
Note: This code is optional. This can be used as a reference in Docker Desktop when this DockerFile is created
Create a Docker ignore
- To make Docker ignore a file, create a file and name it .dockerignore
- Inside the .dockerignore file, In each line, indicate the name of the directory or file that the user wanted to ignore.
Explanation:
In the current application, if there are a lot of directories that are optional to copy inside the Docker, you need to ignore them because they will become slow when creating a Docker image.
These are the following example files that are not necessary to move in the Docker:
- node_modules
- *.md
Docker Image Terminal Commands
Create a Docker image.
- In the terminal, type this command: docker build -t myapp .
- -t is the tag that will be shown in the Docker Desktop
- myapp is the name of the Docker image.
Create a docker image with version 1 as a tag.
docker build -t myapp:v1 .
Run the Docker image and automatically create a container.
docker run --name myapp_c2 myapp
- myapp_c2 is the name of a container that has not been created yet.
- myapp is the name of the Docker image you want to run.
Create and Run the Docker image, add a port for both local and container, and automatically create a container.
docker run --name myapp_container -p 4000:4000 myapp
- myapp_container is the name of a container that has not been created yet.
- myapp is the name of the Docker image you want to run.
- The port number on the left is for the local port.
- The port number on the right is for the container port.
Create and Run a container with a specific version of an image
docker run --name myapp_container -p 4000:4000 myapp:v1
- myapp_container is the name of a container that has not been created yet.
- The port number on the left is for the local port.
- The port number on the right is for the container port.
- myapp is the name of the Docker image you want to run.
- v1 is the tag to be written in the Tag Column.
Create and Run a container with a docker volume feature
Note: The docker volume is to map the source code files on local and in the docker image. If there are changes in the source code in the local file, the source code in the Docker image will also change.
docker run --name myapp_container -p 4000:4000 --rm -v /Users/ikuhironakagawa/Projects/docker-crash-course/api:/app -v /app/node_modules myapp:nodemon
- -v /Users/ikuhironakagawa/Projects/docker-crash-course/api is the local path of the sourcode.
- :/app is the image directory path of the source code inside.
- -v /app/node_modules is to ignore the files if there are changes in the local, and it will not refer to the Docker image.
Show all the docker images
docker images
Delete a docker image
Note: You cannot delete a Docker image that is attached to or used in a container. So, destroy the container first, then the Docker image.
docker image rm mypp
Docker Container Terminal Commands
Show all containers and docker images
docker ps -a
Show all running containers
docker ps
Run the existing container
- docker ps -a
//display all containers first to get the container name
- docker start myapp_container
Stop the running container
- docker ps -a
//Display all the running containers first to get the container name.
- docker stop myapp_container
Delete a container
- docker ps -a
//Display all the running containers first to get the container name.
- docker rm myapp_container
Delete multiple container
- docker ps -a
//Display all the running containers first to get the container name.
- docker rm myapp_container myapp2_container
Delete all containers, images and volumes
docker system prune -a
Top comments (0)