Keep track of docker commands. Additional Tips:
- When met with <container-id>, you only need to input the three first characters in the container id.
- cd /directory/to/Dockerfile/
- docker build -t <image-name> .
Arguments:
- -d - Detach
- -p 0-9{2-5}:0-9{2-5} - Port on host to port in container
- -w - Working directory, for example /app
- -v - Bind mount (link) the host's present getting-started/app directory to the container's /app directory. Note: Docker requires absolute paths for binding mounts, so in this example we use pwd for printing the absolute path of the working directory, i.e. the app directory, instead of typing it manually.
- Can also run commands after <image-name>, for example: sh -c "yarn install && yarn run dev"
- -e - Environment variables, example: -e MYSQL_PASSWORD=password
- --network - Attach to network
- --network-alias - Name for container in network
- -i - Keep STDIN open even if not attached
- -t - Allocate a pseudo-tty
docker run -dp ####:#### <image-name>
docker start <container-id>
Arguments:
- -a - List all containers docker ps
docker images
- docker stop ###
- docker rm ### or
- docker rm -f ###
- docker exec ### <command>
First you need to create a volume, for persistency. Then you bind it to container when r.
- docker volume create <volume-name>
- docker run -dp ####:#### -v <volume-name>:</path/to/mount/folder> <image-name> Example: docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started You can also create a named volume when running 'docker run':
- docker run -dp ####:#### -v <volume-name-that-does-not-exist>:</path/to/mount/folder> <image-name>
Read logs in realtime:
- docker logs -f <container-id>
Create a network for attachment later:
- docker network create todo-app
List all the lines that an image contains:
- docker image history <image>
Do a scan and show vulnerabilities in the supported package managers:
- docker scan <image>
- docker scan --no-trunc <image>
Key words:
- services - Define service
- <name> - Name of service
- <image> - The image to use in the service named <name>
- command - Command(s) to run
- ports - Specify host port and port inside container, example: ports: \n - 3000:3000
- working_dir - Specify working directory, example: working_dir: /app
- volumes - Specify volumes, example: volumes: \n - ./:/app \n <named-volume>:/var/lib/mysql
- environment - Set environment variables
- <name> - Name of service
To mount named volumes you have to define the volumes at the top level of the docker-compose.yml file.
- volumes: \n <name>:
- docker compose up -d
- docker compose logs -f
- docker compose down
- docker compose down --volumes
Reference: NodeJS Docker Guide
Example of how to use caching to improve build times when making changes in the code (which will change how COPY . . works):
FROM node:18-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --production
COPY . .
CMD ["node", "src/index.js"]
Works like .gitignore, for when building containers
Tutorial won't really go through this topic, fill in more later. There is an example of how to ship a Java application without the unnecessary build tools:
FROM maven AS build
WORKDIR /app
COPY . .
RUN mvn package
FROM tomcat
COPY --from=build /app/target/file.war /usr/local/tomcat/webapps
An example of a NodeJS application with Multi-Stage Builds:
FROM node:18 AS build
WORKDIR /app
COPY package* yarn.lock ./
RUN yarn install
COPY public ./public
COPY src ./src
RUN yarn run build
FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html