Deploy an App with Docker

Lance Watanabe
Don’t Leave Me Out in the Code
2 min readJul 29, 2022

--

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:

  1. Create a Dockerfile in the root of your project.
  2. 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.
  3. Create (mkdir) a source folder
  4. Set the working directory to the source folder
  5. Copy the package.json file
  6. 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.
  7. Copy the entire app to the working directory
  8. Specify the port you want the app to be exposed to.
  9. Specify the command to run your app as an array of strings.

Deploy app to container

  1. Build image: docker build . -t <appname>
  2. 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

--

--