What is Docker ?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.
Docker provides the ability to package and run an application in a loosely isolated environment called a container.
Setting Up
Windows Subsystem for Linux v2 https://github.com/microsoft/WSL/releases/download/2.3.26/wsl.2.3.26.0.x64.msi
Check status of WSL
wsl --status
Download Docker Desktop from https://www.docker.com
Download Git https://git-scm.com/downloads
Install Docker Desktop
Sign-Up and sign-In to dockerhub https://hub.docker.com/ (optional only if you need to publish your docker image)
Cloning Repository
Clone git repository for sample app
git clone https://github.com/piperhuntress/postIT-app
cd postIT-app
Use vscode to open the app
code .
Writing a Dockerfile for the client
Create a file named Dockerfile inside the client folder with the package.json file
# define your base image
FROM node:20-alpine
# define the working directory by using the WORKDIR instruction. This will specify where future commands will run and the directory files will be copied inside the countainer image
WORKDIR /app
# Copy the package.json and package-locl.json files to the working directory
COPY package*.json ./
# Run the npm install command to install the node_modules and ensure to get the latest version
RUN npm install && npm install -g npm@latest
# Copy all the files from the current directory to the working directory
COPY . .
# Run the start script
CMD ["npm","start"]
Build and Run the Docker Image for the client (Individual Image)
Open a terminal window in the same directory as your Dockerfile and run the following commands:
# Build the Docker image
docker build -t myclientapp .
# Run the Docker Container
docker run -p 3000:3000 myclientapp
Duplicate the Dockerfile
Copy the Dockerfile from the client folder and paste it inside the server folder
Build and Run the Docker Image for the server (Individual Image)
Open a terminal window in the same directory as your Dockerfile and run the following commands:
# Build the Docker image
docker build -t myserverapp .
# Run the Docker Container
docker run -p 3001:3001 myserverapp
Docker Compose (Running multiple images)
Docker Compose is a tool that helps you define and share multi-container applications. With Compose, you can create a YAML file to define the services.
Create a compose.yml file
name: postitapp
services:
mongo:
image: mongo:7.0.12
volumes:
- mongo_data:/data/db
ports:
- 27017:27017
networks:
- postitnet
mydb:
image: mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
ports:
- "3306:33060"
networks:
- postitnet
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8080:80
environment:
- PMA_ARBITRARY=1
- PMA_HOST=mydb
- PMA_PORT=3306
networks:
- postitnet
depends_on:
- mydb
server:
build:
context: ./server
dockerfile: Dockerfile
ports:
- 3001:3001
volumes:
- ./server:/app
- /app/node_modules
networks:
- postitnet
depends_on:
- mongo
client:
build:
context: ./client
dockerfile: Dockerfile
ports:
- 3000:3000
volumes:
- ./client:/app
- /app/node_modules
networks:
- postitnet
depends_on:
- server
volumes:
mongo_data:
networks:
postitnet:
driver: bridge
Starting the application stack
type the command
docker compose up -d
-d flag run everything in the background
[+] Running 6/6
✓ Network postitapp_postitnet Created
✓ Container postitapp-mongo-1 Started
✓ Container postitapp-mydb-1 Started
✓ Container postitapp-server-1 Started
✓ Container postitapp-client-1 Started
Check currently running containers
docker ps
Stoping the container
docker compose down
This command will stop the containers and remove any network that has been created
Build and Push the image
cd inside your project where the Dockerfile is located
build the project by running the following commands
docker build -t DOCKER_USERNAME/my-postit-app .
Verify if the image exists locally with the following commands:
docker image ls
You will see output similar to the following:
REPOSITORY TAG IMAGE ID CREATED SIZE
DOCKER_USERNAME/my-postit-app latest 1543565c92590 2 minutes ago 1.12GB
Pushing the image to docker hub. Input the followin commands:
docker push DOCKER_USERNAME/my-postit-app