Archive for November, 2021

Docker for Dummies

Friday, November 12th, 2021

Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

FROM node:12-alpine
RUN apk add --update --no-cache python2 g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]

Build a docker image

docker build -t <container-image> .

Scan for vulnerabilities

docker scan  <container-image>

Start a container

docker run -dp <host-port>:<container-port> --name <container-name> <container-image> 

Removing a container

docker ps

docker stop <container-id>
docker remove <container-id>

or

docker rm -f <container-id>

Stop and removing all running containers

docker stop $(docker ps -q)docker rm $(docker ps -a -q)

Execute a command inside a container

docker exec 
-t tty
-i interactive
docker exec <container-id> <cmd>
docker exec -i <container-name> <cmd>

start a shell in a container

docker exec -it <container-name> /bin/bash

Create a Persistence Volume

docker volume create <volume-name>

start a container with persistence volume – Named Volume

docker run -dp <host-port>:<container-port> -v <pv-volume-name>:<container-path> <container-image>

after docker v 17.0.6

docker run -dp <host-port>:<container-port> --mount type=<pv-volume-name>,source=<pv-volume-name>,target=<containter-path> <container-image>

create a named volume

docker create volume <volume-name>

Remove a named volume

docker volume rm <volume-name>

list persistence volume in docker – Named Volume

docker volume ls 

start a container with persistence volume – Binded Volume

docker run -dp <host-port>:<container-port> -v <host-path>:<container-path> <container-id>

after docker v 17.0.6

docker run -dp <host-port>:<container-port> --mount type=bind,source=<host-path>,target=<container-path> <container-id>

showing the console log

docker logs -f <container-id>

Create a dedicated network between app container and mysql container

docker create network <network-name>

docker run -d \
	--name <container-name> \
	--network <network-interface-name> \
	--network-alias <host-name> \
	--mount type=bind,source=<host-path>,target=<container-path> \
	-e MYSQL_ROOT_PASSWORD=<db-password> \
	-e MYSQL_DATABASE=<db-name> \
	<docker-image>

docker run -it -p 3000:3000 \
	--name <container-name> \
	--network <network-interface-name> \
	-e MYSQL_HOST=<db-host> \
	-e MYSQL_USER=<db-user> \
	-e MYSQL_PASSWORD=<db-password> \
	-e MYSQL_DB=<db-name> \
	<container-image>

Start a Network Troubleshooting container

docker run -it --network <network-interface-name> nicolaka/netshoot