Deploy an App with Docker
Here is a quick guide to deploying an app to Docker. To get started, create a simple Node (React, Vue, etc) application and download Docker Desktop (https://www.docker.com/products/docker-desktop/)
Docker: Container management software
Image: Instructions to build a container. Each image has multiple layers (Ubuntu, filesystem, dependencies).
Container: A container runs an instance of an image.
DockerHub: Collection of Docker images
Volumes: Data that persists when you stop your container
Create a Dockerfile:
- Create a Dockerfile in the root of your project.
- All Dockerfiles need a “FROM” command to determine the linux distribution and engine. In the example below, we are creating a React app that runs on node 16 using the Bullseye distribution.
- Create (mkdir) a source folder
- Set the working directory to the source folder
- Copy the package.json file
- Install dependencies/packages. Note, Docker will build your apps dependencies based on what is configured in your package.json file so you can delete the node_modules and build folder.
- Copy the entire app to the working directory
- Specify the port you want the app to be exposed to.
- Specify the command to run your app as an array of strings.
Deploy app to container
- Build image: docker build . -t <appname>
- Run image in container: docker run --rm -it -p 3000:3000/tcp <appname>:latest
Other Docker commands:
docker images //show images
docker ps //show containers running
docker build . //build image from Dockerfile in current directory
docker build . -t <appname> //build image with name
docker run --rm -it -p 3000:3000/tcp <appname>:latest. //run image in a container on port 3000
Docker compose: Docker can run multiple containers together and create a network. You must create a docker-compose.yml and configure multiple “services.”
Docker compose commands:
docker-compose up -d: //run multiple containers together as configured in docker-compose.yml file
docker-compose down: //stop container
Push image to repository: To push an image to your repository:
docker login -u <username>
docker tag <image id> <dockerhub-username>/<dockerhub-repository-name>:latest
docker push <dockerhub-username>/<dockerhub-repository-name>:latest